作者tropical72 (蓝影)
站内Programming
标题Re: 自动按键的 win32 程
时间Fri Dec 3 17:07:36 2010
原文恕删..
我写了一个简单的范例, 假设你要关闭的对话框长这样..
http://ppt.cc/oyLR
接着都在调用 ::FindWindow, ::FindWindowEx, ::SendMessage,
::PostMessage, ::ShowWindow, ::DestroyWindow... etc 函式,
实做简单范例码连结:
http://nopaste.csie.org/d9e6e
简单的范例码如下:
#include <windows.h>
#include <stdio.h>
#include <stdio.h>
#define BUFFER_LEN 200
int main()
{
const char *WindowName = "MFC_T";
const char *ButtonName = "Button1";
char WindowClassName[BUFFER_LEN];
char ButtonClassName[BUFFER_LEN];
HWND WindowWnd = NULL;
HWND ButtonWnd = NULL;
// ================================================
// Window
WindowWnd=::FindWindow(NULL, WindowName);
if(WindowWnd==NULL) {
printf("can't find %s window.\n", WindowName);
return EXIT_FAILURE;
}
GetClassName(WindowWnd,WindowClassName ,200);
printf("Windows Class:%s\n", WindowClassName);
// hide 1 sec, and show it
::SetForegroundWindow(WindowWnd); // set active window
printf("window hide and show...\n");
Sleep(1000),
::ShowWindow(WindowWnd, SW_HIDE);
Sleep(1000);
::ShowWindow(WindowWnd, SW_SHOW);
// ================================================
// Button
ButtonWnd = ::FindWindowEx(WindowWnd, NULL, NULL, ButtonName);
if(WindowWnd==NULL) {
printf("can't find %s window.\n", WindowName);
return EXIT_FAILURE;
}
GetClassName(ButtonWnd,ButtonClassName ,200);
printf("Button Class:%s\n", ButtonClassName);
// we'll destroy the window
// once you destroy any object, you can't use that any more.
UINT msg = WM_CLOSE;
Sleep(1000), printf("Close Now..\n");
::SendMessage(WindowWnd,msg, NULL, NULL);
printf("Have Close...\n");
return EXIT_SUCCESS;
}
上面要注意的是, 如果你的视窗还没跑完
(或已画出来,但实际上还没初始化完之类问题)
你用 ::FindWindow 会找不到,
所以根据你的例子比较建议用
while(WindowWnd!=NULL){
WindowWnd = ::FindWindow(NULL, WindowName);
};
当然比较聪明的做法或许是用 CALLBACK 方式就不会被绑死.
使用前请先评估这是不是真的是你唯一解决的方式,
我自己测是觉得抓失败的机会还是有的,
常常 SendMessage, PostMessage, Sleep(xxx) 在那里调来调去.
参考看看吧.
--
YouLoveMe() ? LetItBe() : LetMeFree();
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 180.177.76.142
※ 编辑: tropical72 来自: 180.177.76.142 (12/03 17:09)
※ 编辑: tropical72 来自: 180.177.76.142 (12/03 17:11)