Re: 自動按鍵的 win32 程

看板Programming作者 (藍影)時間15年前 (2010/12/03 17:07), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串5/7 (看更多)
原文恕刪.. 我寫了一個簡單的範例, 假設你要關閉的對話框長這樣.. 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)
文章代碼(AID): #1C-BDRAC (Programming)
文章代碼(AID): #1C-BDRAC (Programming)