这个问题找了好久都没有满意的答复,最后实在没法,想到按键精灵中有一个函数findpic就是用来在屏幕上查找某个指定的图形的,而正好包含这个函数的dll(QMDispatch.dll)是一个com组件,再找了好久弄到了在VC中调用com组件的办法,这下好了,都解决了。【题外话:如果是在VB中使用这个函数更简单,只要注册这个com组件,然后在VB中创建相应的对象就可以了CreateObject("QMDispatch.QMFunction") 】
下面先把我在VC中使用这个组件的代码贴一下,后面再跟上一篇《在VC中调用COM组件的方法》:
1、CQMFunction.h文件(汗,忘了怎么做出这个头文件的了,复习一下刚才这篇《在VC中调用COM组件的方法》):
//==========================================================
// 从类型库向导中用“添加类”创建的计算机生成的 IDispatch 包装类
#import "C://windows//QMDispatch.dll" no_namespace
// CQMFunction 包装类
class CQMFunction : public COleDispatchDriver
{
public:
CQMFunction(){} // 调用 COleDispatchDriver 默认构造函数
CQMFunction(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {}
CQMFunction(const CQMFunction& dispatchSrc) : COleDispatchDriver(dispatchSrc) {}
// 属性
public:
// 操作
public:
// IQMFunction 方法
public:
CString InputBox(LPCTSTR strPrompt)
{
CString result;
static BYTE parms[] = VTS_BSTR ;
InvokeHelper(0x1, DISPATCH_METHOD, VT_BSTR, (void*)&result, parms, strPrompt);
return result;
}
long GetCursorPos()
{
long result;
InvokeHelper(0x2, DISPATCH_METHOD, VT_I4, (void*)&result, NULL);
return result;
}
void SetCursorPos(long iCursorPosX, long iCursorPosY)
{
static BYTE parms[] = VTS_I4 VTS_I4 ;
InvokeHelper(0x3, DISPATCH_METHOD, VT_EMPTY, NULL, parms, iCursorPosX, iCursorPosY);
}
CString GetPixelColor(long iCursorPosX, long iCursorPosY)
{
CString result;
static BYTE parms[] = VTS_I4 VTS_I4 ;
InvokeHelper(0x4, DISPATCH_METHOD, VT_BSTR, (void*)&result, parms, iCursorPosX, iCursorPosY);
return result;
}
long FindColor(long iLeft, long iTop, long iRight, long iBottom, LPCTSTR szColor, long iFindType, float fComp)
{
long result;
static BYTE parms[] = VTS_I4 VTS_I4 VTS_I4 VTS_I4 VTS_BSTR VTS_I4 VTS_R4 ;
InvokeHelper(0x5, DISPATCH_METHOD, VT_I4, (void*)&result, parms, iLeft, iTop, iRight, iBottom, szColor, iFindType, fComp);
return result;
}
long GetScreenResolution()
{
long result;
InvokeHelper(0x6, DISPATCH_METHOD, VT_I4, (void*)&result, NULL);
return result;
}
unsigned long ReadMemory(unsigned long dwAddress, long iType)
{
unsigned long result;
static BYTE parms[] = VTS_UI4 VTS_I4 ;
InvokeHelper(0x7, DISPATCH_METHOD, VT_UI4, (void*)&result, parms, dwAddress, iType);
return result;
}
long FindPic(long iLeft, long iTop, long iRight, long iBottom, LPCTSTR bstrFileName, float fComparability)
{
long result;
static BYTE parms[] = VTS_I4 VTS_I4 VTS_I4 VTS_I4 VTS_BSTR VTS_R4 ;
InvokeHelper(0x8, DISPATCH_METHOD, VT_I4, (void*)&result, parms, iLeft, iTop, iRight, iBottom, bstrFileName, fComparability);
return result;
}
void SleepMS(long iMSecond)
{
static BYTE parms[] = VTS_I4 ;
InvokeHelper(0x9, DISPATCH_METHOD, VT_EMPTY, NULL, parms, iMSecond);
}
void MessageBox(LPCTSTR bstrMessage)
{
static BYTE parms[] = VTS_BSTR ;
InvokeHelper(0xa, DISPATCH_METHOD, VT_EMPTY, NULL, parms, bstrMessage);
}
void RunApp(LPCTSTR bstrFile)
{
static BYTE parms[] = VTS_BSTR ;
InvokeHelper(0xb, DISPATCH_METHOD, VT_EMPTY, NULL, parms, bstrFile);
}
unsigned long GetCursorShape(long iType)
{
unsigned long result;
static BYTE parms[] = VTS_I4 ;
InvokeHelper(0xc, DISPATCH_METHOD, VT_UI4, (void*)&result, parms, iType);
return result;
}
// IQMFunction 属性
public:
};
//==============================================================
2、初始化com组件:
//==============================================================
//初始化CQMFunction组件并创建实例
void CFreeRXJHDlg::QMFunInitializtion()
{
::CoInitialize(NULL);//com组件初始化,必须执行一次才能正常使用com组件
//*disp.CreateDispatch(_T("QMDispatch.QMFunction"));
if( ! ::obj.CreateDispatch(_T("QMDispatch.QMFunction")) )//定义了一个全局变量CQMFunction obj;
{
TCHAR path[1024];
::GetCurrentDirectory(1024,path);
CString str(path);
str += _T("//QMDispatch.dll");
HMODULE hMod=::LoadLibrary(str);
if(hMod==NULL)
{
MessageBox(_T("加载QMDispatch.dll失败,/n请确认当前目录下包含有QMDispatch.dll文件"));
OnOK();
}
// DllRegisterServer
typedef HRESULT (STDAPICALLTYPE *DLLREGISTERSERVER)(void);
// DllUnregisterServer
//typedef HRESULT (STDAPICALLTYPE *DLLUNREGISTERSERVER)(void);
BOOL bRet=FALSE;
DLLREGISTERSERVER DllRegisterServerFunc=
(DLLREGISTERSERVER)GetProcAddress(hMod,"DllRegisterServer");
if(DllRegisterServerFunc)
bRet=(DllRegisterServerFunc() >= 0);
FreeLibrary(hMod);
if(bRet==FALSE)
{
MessageBox(_T("注册QMDispatch.dll失败,/n请确认当前目录下包含有QMDispatch.dll文件"));
OnOK();
}
else if( ! ::obj.CreateDispatch(_T("QMDispatch.QMFunction")) )
{
MessageBox(_T("创建QMDispatch.QMFunction对象失败"));
OnOK();
}
}/**/
}
//==============================================================
3、com组件对象已经创建出来了,剩下的就随你怎么处置了,我这里只使用其findpic函数:
int x,y;
x=