Skip to content

Commit

Permalink
更新SendString接口,依赖项修改
Browse files Browse the repository at this point in the history
  • Loading branch information
WallBreaker2 committed Feb 2, 2019
1 parent f190df3 commit cfb496a
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 63 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ bld/
[Oo]bj/
[Ll]og/

Tool/


# Visual Studio 2015 cache/options directory
.vs/
Expand Down Expand Up @@ -272,4 +274,6 @@ __pycache__/
/MFCApp
/PythonTest

Tool



102 changes: 71 additions & 31 deletions PythonTest/PythonTest.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,77 @@

from win32com.client import Dispatch
#创建com对象
op=Dispatch("op.opsoft");
#输出插件版本号
print("op ver:",op.Ver());
#测试窗口接口
hwnd=op.FindWindow("","op_test.txt - 记事本");
#ocr-设置字库
r=op.SetDict(0,"test.dict");
print("SetDict:",r);
r=0;
if hwnd:
r=op.BindWindow(hwnd,"gdi","normal","windows",0);
if r:
print("bind ok.");
r=op.Sleep(1000);
print("try screencap");
r=op.Capture(0,0,100,100,"screen.bmp");
r,x,y=op.FindColor(0,0,121,159,"000000-050505",0.9,1);
print(r,x,y);
if r:
op.MoveTo(x,y);
op.LeftClick();
print("ocr:",op.Ocr(0,0,100,100,"000000",1.0));
print(op.GetColor(165,164));
print("wait key 65");
op.WaitKey(65,5000);
op.UnBindWindow();
else:
print("bind false.");
else:
print("invalid window.");
class Demo:
def __init__(self):
#创建com对象
self.op=op=Dispatch("op.opsoft");
self.hwnd=0;
self.send_hwnd=0;

def test_base(self):
#输出插件版本号
print("op ver:",self.op.Ver());
r=self.op.WinExec("notepad",1);
print("Exec notepad:",r);



def test_window_api(self):
#测试窗口接口

self.hwnd = self.op.FindWindow("","无标题 - 记事本");
print("parent hwnd:",self.hwnd);
if self.hwnd:
self.send_hwnd=self.op.FindWindowEx(self.hwnd,"Edit","");
print("child hwnd:",self.send_hwnd);
return 0;

def test_bkmode(self):
r=self.op.BindWindow(self.hwnd,"gdi","normal","windows",0);
if r == 0:
print("bind false");
return r;

def test_bkmouse_bkkeypad(self):
self.op.MoveTo(200,200);
self.op.Sleep(200);
self.op.LeftClick();
self.op.Sleep(1000);
r=self.op.SendString(self.send_hwnd,"Hello World!");
print("SendString ret:",r);
self.op.Sleep(2000);
return 0;

def test_bkimage(self):
self.op.GetColor(30,30);
self.op.Capture(0,0,100,100,"bkimgae.bmp");
return 0;

def test_ocr(self):
#ocr-设置字库
r=self.op.SetDict(0,"test.dict");
print("SetDict:",r);
s=self.op.OcrAuto(0,0,100,100,1.0);
print("ocr:",s);
s=self.op.OcrEx(0,0,100,100,"000000-020202",1.0);
print("OcrEx:",s);
return 0;


def test_all():
demo=Demo();
demo.test_base();
demo.test_window_api();
if demo.test_bkmode() == 0:
return 0;
demo.test_bkmouse_bkkeypad();
demo.test_bkimage();
demo.test_ocr();

return 0;

#run all test
print("test begin");
test_all();
print("test end");


2 changes: 1 addition & 1 deletion Tool/Dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <vector>
#include <string>
#include <math.h>
//
//e
#define SET_BIT(x, idx) x |= 1u << (idx)

#define GET_BIT(x, idx) (((x )>> (idx)) & 1u)
Expand Down
Binary file modified doc/op接口说明.chm
Binary file not shown.
Binary file modified doc/op接口说明.chm.bak
Binary file not shown.
13 changes: 13 additions & 0 deletions op.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ConsoleTest", "ConsoleTest\
EndProject
Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "PythonTest", "PythonTest\PythonTest.pyproj", "{70EE008E-A047-4299-84AA-7C0079CFF4CE}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Tool", "Tool\Tool.vcxproj", "{B12702AD-ABFB-343A-A199-8E24837244A3}"
ProjectSection(ProjectDependencies) = postProject
{EA90DCB4-3591-4FA3-9C10-08FEE47A4BFA} = {EA90DCB4-3591-4FA3-9C10-08FEE47A4BFA}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -56,6 +61,14 @@ Global
{70EE008E-A047-4299-84AA-7C0079CFF4CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70EE008E-A047-4299-84AA-7C0079CFF4CE}.Release|x64.ActiveCfg = Release|Any CPU
{70EE008E-A047-4299-84AA-7C0079CFF4CE}.Release|x86.ActiveCfg = Release|Any CPU
{B12702AD-ABFB-343A-A199-8E24837244A3}.Debug|Any CPU.ActiveCfg = Debug|x64
{B12702AD-ABFB-343A-A199-8E24837244A3}.Debug|x64.ActiveCfg = Debug|x64
{B12702AD-ABFB-343A-A199-8E24837244A3}.Debug|x64.Build.0 = Debug|x64
{B12702AD-ABFB-343A-A199-8E24837244A3}.Debug|x86.ActiveCfg = Debug|x64
{B12702AD-ABFB-343A-A199-8E24837244A3}.Release|Any CPU.ActiveCfg = Release|x64
{B12702AD-ABFB-343A-A199-8E24837244A3}.Release|x64.ActiveCfg = Release|x64
{B12702AD-ABFB-343A-A199-8E24837244A3}.Release|x64.Build.0 = Release|x64
{B12702AD-ABFB-343A-A199-8E24837244A3}.Release|x86.ActiveCfg = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 3 additions & 2 deletions op/Bkgdi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,14 @@ long bkgdi::cap_release() {

if (_hbmpscreen)DeleteObject(_hbmpscreen); _hbmpscreen = NULL;
if (_holdbmp)DeleteObject(_holdbmp); _holdbmp = NULL;
Tool::setlog(L"cap_release");
//Tool::setlog(L"cap_release");
bind_release();
return 0;
}

long bkgdi::cap_image() {
Tool::setlog("bkgdi::cap_image()");

//Tool::setlog("bkgdi::cap_image()");
if (!IsWindow(_hwnd)) { _is_cap = 0; return 0; }
//对指定的源设备环境区域中的像素进行位块(bit_block)转换
if (_mode == BACKTYPE::NORMAL)
Expand Down
3 changes: 1 addition & 2 deletions op/Color/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#define __COLOR_H_
#include <string>
#include <opencv2/core.hpp>
#include <boost/format.hpp>
#include "../Common.h"
#include "../Tool.h"
//ÑÕÉ«½á¹¹
struct color_t {
uchar b, g, r, alpha;
Expand Down
5 changes: 2 additions & 3 deletions op/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#ifndef __COMMON_H_
#define __COMMON_H_
#include <string>
#include <boost/locale.hpp>
#include <fstream>
#include <vector>

Expand All @@ -14,8 +13,8 @@
#define SAFE_DELETE_ARRAY(ptr) if(ptr)delete[] ptr;ptr=nullptr


#define _sto_wstring(s) boost::locale::conv::to_utf<wchar_t>(s, "GBK")
#define _wsto_string(s) boost::locale::conv::from_utf(s,"GBK")
//#define _sto_wstring(s) boost::locale::conv::to_utf<wchar_t>(s, "GBK")
//#define _wsto_string(s) boost::locale::conv::from_utf(s,"GBK")

using std::wstring;
using std::string;
Expand Down
34 changes: 27 additions & 7 deletions op/OpInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ STDMETHODIMP OpInterface::GetPath(BSTR* path) {
return S_OK;
}

STDMETHODIMP OpInterface::GetBasePath(BSTR* path){
CComBSTR bstr;
wchar_t basepath[256];
::GetModuleFileName(gInstance, basepath, 256);
bstr.Append(basepath);
bstr.CopyTo(path);
return S_OK;
}

STDMETHODIMP OpInterface::WinExec(BSTR cmdline,LONG cmdshow, LONG* ret){
auto str = _wsto_string(cmdline);
*ret = ::WinExec(str.c_str(), cmdshow) > 31 ? 1 : 0;
return S_OK;
}

STDMETHODIMP OpInterface::Sleep(LONG millseconds, LONG* ret) {
::Sleep(millseconds);
*ret = 1;
Expand Down Expand Up @@ -113,7 +128,7 @@ STDMETHODIMP OpInterface::ClientToScreen(LONG ClientToScreen, VARIANT* x, VARIAN
STDMETHODIMP OpInterface::FindWindow(BSTR class_name, BSTR title, LONG* rethwnd)
{
// TODO: 在此添加实现代码
_winapi.FindWindow(class_name, title, *rethwnd);
*rethwnd = _winapi.FindWindow(class_name, title);
return S_OK;
}

Expand All @@ -134,7 +149,7 @@ STDMETHODIMP OpInterface::FindWindowByProcessId(LONG process_id, BSTR class_name
STDMETHODIMP OpInterface::FindWindowEx(LONG parent, BSTR class_name, BSTR title, LONG* rethwnd)
{
// TODO: 在此添加实现代码
_winapi.FindWindow(class_name, title, *rethwnd, parent);
*rethwnd = _winapi.FindWindowEx(parent,class_name, title);
return S_OK;
}

Expand Down Expand Up @@ -322,11 +337,6 @@ STDMETHODIMP OpInterface::SendPaste(LONG hwnd, LONG* nret)
return S_OK;
}

STDMETHODIMP OpInterface::SendString(LONG hwnd, BSTR str, LONG* ret){
*ret = _winapi.SendString((HWND)hwnd, str);
return S_OK;
}

STDMETHODIMP OpInterface::SetClientSize(LONG hwnd, LONG width, LONG hight, LONG* nret)
{
// TODO: 在此添加实现代码
Expand Down Expand Up @@ -363,6 +373,16 @@ STDMETHODIMP OpInterface::SetWindowTransparent(LONG hwnd, LONG trans, LONG* nret
return S_OK;
}

STDMETHODIMP OpInterface::SendString(LONG hwnd, BSTR str, LONG* ret) {
*ret = _winapi.SendString((HWND)hwnd, str);
return S_OK;
}

STDMETHODIMP OpInterface::SendStringIme(LONG hwnd, BSTR str, LONG* ret) {
*ret = _winapi.SendStringIme((HWND)hwnd, str);
return S_OK;
}

STDMETHODIMP OpInterface::ExcuteCmd(BSTR cmd, LONG millseconds, BSTR* retstr) {
CComBSTR bstr;
auto strcmd = _wsto_string(cmd);
Expand Down
20 changes: 14 additions & 6 deletions op/OpInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,18 @@ END_COM_MAP()
//image process
ImageProc _image_proc;
public:
//---------------方法-------------------
//---------------基本设置/属性-------------------

//1.版本号Version
STDMETHOD(Ver)(BSTR* ret);
//set curr path
//设置目录
STDMETHOD(SetPath)(BSTR path, LONG* ret);
//get curr path
//获取目录
STDMETHOD(GetPath)(BSTR*path);
//获取插件目录
STDMETHOD(GetBasePath)(BSTR* path);
//运行可执行文件
STDMETHOD(WinExec)(BSTR cmdline,LONG cmdshow, LONG* ret);
//sleep
STDMETHOD(Sleep)(LONG millseconds, LONG* ret);
//Process
Expand Down Expand Up @@ -128,8 +132,6 @@ END_COM_MAP()
STDMETHOD(ScreenToClient)(LONG hwnd, VARIANT* x, VARIANT* y, LONG* nret);
//向指定窗口发送粘贴命令
STDMETHOD(SendPaste)(LONG hwnd, LONG* nret);
//向指定窗口发送文本数据
STDMETHOD(SendString)(LONG hwnd, BSTR str, LONG* ret);
//设置窗口客户区域的宽度和高度
STDMETHOD(SetClientSize)(LONG hwnd, LONG width, LONG hight, LONG* nret);
//设置窗口的状态
Expand All @@ -140,8 +142,12 @@ END_COM_MAP()
STDMETHOD(SetWindowText)(LONG hwnd, BSTR title, LONG* nret);
//设置窗口的透明度
STDMETHOD(SetWindowTransparent)(LONG hwnd, LONG trans, LONG* nret);
//向指定窗口发送文本数据
STDMETHOD(SendString)(LONG hwnd, BSTR str, LONG* ret);
//向指定窗口发送文本数据-输入法
STDMETHOD(SendStringIme)(LONG hwnd, BSTR str, LONG* ret);

//cmd
//运行命令行并返回结果
STDMETHOD(ExcuteCmd)(BSTR cmd,LONG millseconds, BSTR* retstr);

//--------------------Background -----------------------
Expand Down Expand Up @@ -194,6 +200,8 @@ END_COM_MAP()
STDMETHOD(KeyUpChar)(BSTR vk_code, LONG* ret);
//等待指定的按键按下 (前台,不是后台)
STDMETHOD(WaitKey)(LONG vk_code,LONG time_out, LONG* ret);
//发送字符串
//STDMETHOD(SendString)(LONG HWND)

//--------------------image and color-----------------------
//抓取指定区域(x1, y1, x2, y2)的图像, 保存为file
Expand Down
26 changes: 26 additions & 0 deletions op/Tool.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
#include "stdafx.h"
#include "Tool.h"
#include <algorithm>
std::wstring _sto_wstring(const std::string&s) {
std::string strLocale = setlocale(LC_ALL, "");
const char* chSrc = s.c_str();
size_t nDestSize = mbstowcs(NULL, chSrc, 0) + 1;
wchar_t* wchDest = new wchar_t[nDestSize];
wmemset(wchDest, 0, nDestSize);
mbstowcs(wchDest, chSrc, nDestSize);
std::wstring wstrResult = wchDest;
delete[]wchDest;
setlocale(LC_ALL, strLocale.c_str());
return wstrResult;
}

std::string _wsto_string(const std::wstring&ws) {
std::string strLocale = setlocale(LC_ALL, "");
const wchar_t* wchSrc = ws.c_str();
size_t nDestSize = wcstombs(NULL, wchSrc, 0) + 1;
char *chDest = new char[nDestSize];
memset(chDest, 0, nDestSize);
wcstombs(chDest, wchSrc, nDestSize);
std::string strResult = chDest;
delete[]chDest;
setlocale(LC_ALL, strLocale.c_str());
return strResult;
}

long Tool::setlog(const wchar_t* format, ...) {
va_list args;
Expand Down
2 changes: 2 additions & 0 deletions op/Tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#ifndef __TOOL_H_
#define __TOOL_H_
#include "Common.h"
std::wstring _sto_wstring(const std::string&s);
std::string _wsto_string(const std::wstring&s);
namespace Tool {
//for debug
long setlog(const wchar_t* format, ...);
Expand Down
Loading

0 comments on commit cfb496a

Please sign in to comment.