ÔÚ±¾È˵ġ¶×Ô¶¯¹Ø±ÕÐÅÏ¢Ìáʾ´°ÌåµÄʵÏÖ¡·ÖÐʵÏÖÁËÀûÓÃÏß³ÌÀ´¹Ø±Õ²»ÐèÒªµÄ´°Ìå¼¼Êõ£¬ËüËùʵÏÖµÄÊǹرÕͬһ³ÌÐòÖеĴ°Ì壬¶øÓÐʱÊÇÐèÒª¹Ø±Õ²»ÔÚÒ»¸ö³ÌÐòÖеĴ°ÌåµÄ¡£±ÈÈ磬ÏÖÔÚËù¼ûµ½µÄ“¹ã¸æÉ±ÊÖ”£¨×Ô¶¯¹Ø±Õ¹ã¸æ´°ÌåµÄÒ»¸ö³ÌÐò£©¡£ ʵÏֹرմ°ÌåµÄ¹Ø¼üÔÚÓÚÕÒµ½¸Ã´°ÌåµÄ¾ä±ú£¬Ö®ºó¾Í¿ÉÒÔ·¢ËÍWM_CLOSE¸ø¸Ã´°ÌåʵÏÖ´°ÌåµÄ¹Ø±Õ¡£ÏÂÃæ¾ÍÊÇÒ»¸öµäÐ͵ÄÀý×ÓÓÃÀ´¹Ø±ÕÖ¸¶¨´°Ìå±êÌâÒÔ¼°´°ÌåÀàÃû³ÆµÄÒ»¸öº¯Êý¡£ Procedure TForm1.Button1Click(Sender: TObject); var hCurrentWindow: HWnd; szText: array[0..254] of char; begin hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST); while hCurrentWindow <> 0 do begin if GetWindowText(hCurrentWindow, @szText, 255)>0 then begin if StrPas(@szText))=’´°Ìå±êÌâ’ then if GetClassName(hCurrentWindow, @szText, 255)>0 then if StrPas(@szText))=’´°ÌåÀàÃû³Æ’ then break; end; hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT); end; ʵ¼ÊÉÏÓÐʱÐèÒªÔÚ¹¤ÈËỊ̈߳¨MFC½«Ï̷߳ÖÀàΪ¹¤ÈË»òÕßÓû§½Ó¿Ú¡£Ö÷ÒªµÄ²î±ðÊÇÓû§½Ó¿ÚÏ߳̿ÉÒÔ½ÓÊÕÏûÏ¢£¬¶ø¹¤ÈËÏ̲߳»ÄÜ¡£Ò»°ãµØ£¬Ó¦¸ÃʹÓù¤ÈËÏß³Ì×÷Ϊ²»ÐèÒªÓû§¸ÉÔ¤µÄºǫ́Ị̈߳¬±ÈÈçµç×Ó±í¸ñµÄÖØÐ¼ÆËã¡¢´òÓ¡²Ù×÷»òÕ߯´Ð´¼ì²é£©ÖÐÀ´ÊµÏÖÕâÑùµÄ¹¦ÄÜ£¬¶øÉÏÃæµÄ³ÌÐòÔò²»ÄÜʵÏָù¦ÄÜ£¬ÒòΪÆäÖÐʹÓõÄHandle£¬¸**andleÒ²¾ÍÊÇSelf.Handle£¨´°ÌåµÄ¾ä±ú£©¡£ Èç¹ûÄܹ»ÕÒµ½Ò»¸öÓÐÒâÒåµÄ´°¿Ú¾ä±úÀ´Ìæ´ú¸**andleµÄ»°£¬¾Í¿ÉÒÔʵÏÖÁË¡£ ²éÕÒWindwos°ïÖú£¬Ïà¹ØµÄº¯Êý°üÀ¨ HWND GetWindow(HWND hWnd, INT uCmd);»ñȡһ¸ö´°ÌåµÄÏà¹Ø´°Ìå HWND GetNextWindow(HWND hWnd, UINT wCmd);»ñÈ¡ÏÂÒ»¸ö´°Ìå HWND GetTopWindow(HWND hWnd);»ñÈ¡µ±Ç°µÄ¶¥´°Ìå µ«ÕâÈý¸öº¯Êý¶¼ÐèÒªÊÂÏÈÖ¸¶¨hWnd£» ÁíÒ»¸öº¯ÊýHWND GetActiveWindow(VOID);·µ»ØµÄÊÇÏß³ÌÏà¹ØµÄ»î¶¯µÄ´°Ì壬µ«¹¤ÈËÏß³ÌÖÐûÓд°Ìå¡£ ½øÒ»²½²éÕÒ¿ÉÒÔ·¢ÏÖHWND FindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName); ¿ÉÒÔÂú×ãÎÒÃǵÄÒªÇ󡣯äÖеIJÎÊýlpClassNameÊÇÖ¸ÏòÀàÃû³Æ×Ö·û´®µÄÖ¸Õ룬¶ølpWindowNameÊÇÖ¸Ïò´°ÌåÃû³ÆµÄÖ¸Õ룬Èç¹û¸Ã²ÎÊýΪNULL£¬ÄÇôËùÓеĴ°Ì嶼ƥÅä¡£ Ëü¹Ø¼üµÄÐÔÖÊ“¶ølpWindowNameÊÇÖ¸Ïò´°ÌåÃû³ÆµÄÖ¸Õ룬Èç¹û¸Ã²ÎÊýΪNULL£¬ÄÇôËùÓеĴ°Ì嶼ƥÅä¡£”ËùÒÔ¿ÉÒÔÀûÓôËÌØÐÔ£¬ÕÒµ½ÈκÎÒ»¸öÓÐÒâÒåµÄ´°Ìå¾ä±ú£¬ÔÚÀûÓô˾ä±ú×÷ΪGetWindowµÄ²ÎÊý£¬×îÖÕ²éÕÒµ½ÎÒÃÇËùÐèÒªµÄ´°Ìå¾ä±ú¡£¾ßÌåʵÏÖÈçÏÂËùʾ£º unit UWindowKiller; interface uses Classes, Windows, Messages, SysUtils, Dialogs; type WindowKiller = class(TThread) protected procedure Execute; override; end; implementation var aKiller: WindowKiller; { WindowKiller } procedure WindowKiller.Execute; var Handle: THandle; hCurrentWindow: HWnd; szText: array[0..254] of char; begin Handle := FindWindow(nil, nil); // ÏÈÕÒµ½ÈÎÒâÒ»¸öÓÐÒâÒåµÄ´°Ìå¾ä±ú hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST); while hCurrentWindow <> 0 do begin if GetWindowText(hCurrentWindow, @szText, 255)>0 then begin if StrPas(@szText)='ÎҵĵçÄÔ' then // Æ¥Åä´°Ìå±êÌâ break; end; hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT); end; If hCurrentWindow<>0 then PostMessage(hCurrentWindow, WM_CLOSE, 0, 0); //½«´°Ì幨±Õ end;
initialization aKiller := WindowKiller.Create(False); // ×Ô¶¯´´½¨¸ÃỊ̈߳¬ÔÚ³ÌÐòÆô¶¯Ê±¾ÍÖ´ÐÐ end.
½áºÏÉÏÃæµÄ³ÌÐò¿ÉÒÔ¸ø³ö“¹ã¸æÉ±ÊÖ”µÄαʵÏÖ¹ý³Ì£º procedure WindowKiller.Execute; var i: integer; str: string; slWindow: TStrings; Handle, hCurrentWindow: HWnd; szText: array[0..254] of char; begin slWindow := TStringList.Create; try while not Terminated do begin slWindow.Clear; Handle := FindWindow(nil, nil); hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST); while hCurrentWindow <> 0 do begin if GetWindowText(hCurrentWindow, @szText, 255)>0 then begin str := StrPas(@szText); if str ·ûºÏ¹ã¸æ´°Ìå±êÌâµÄÌØÕ÷then slWindow.Add(IntToStr(hCurrentWindow)); // ÆäËû¿ÉÄܵĸù¾Ý´°ÌåÊôÐÔµÄÅÐ¶Ï end; hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT); end; for i:=0 to slWindow.Count-1 do PostMessage(HWnd(StrToInt(slWindow[i])), WM_CLOSE, 0, 0); Sleep(1000); end; // end-while finally slWindow.Free; end; end; 
|