找到視窗(部份)名稱並關閉該程式 |
|
flyup
資深會員 發表:280 回覆:508 積分:385 註冊:2002-04-15 發送簡訊給我 |
How to find a window by its (partial) title and close it How can I close an unwanted popup window using its partial title? I have tried destroywindow, however, it cannot close the window I want to close.
-------------------------------------------------------------------- Well, first you need to get a handle to the window. The following code can find a window by window title (even a partial title, and is case insensisitive) . function HWndGet( partialTitle: string ): hWnd;
var
hWndTemp: hWnd;
iLenText: Integer;
cTitletemp: array [0..254] of Char;
sTitleTemp: string;
begin
hWndTemp := FindWindow(nil, nil);
{Find first window and loop through all subsequent windows in the master window list}
while hWndTemp <> 0 do
begin
{Retrieve caption text from current window}
iLenText := GetWindowText(hWndTemp, cTitletemp, 255);
sTitleTemp := cTitletemp;
sTitleTemp := UpperCase(copy( sTitleTemp, 1, iLenText));
{Clean up the return string, preparing for case insensitive comparison. Use appropriate
method to determine if the current window's caption either starts with or contains passed string}
partialTitle := UpperCase( partialTitle );
if pos( partialTitle, sTitleTemp ) <> 0 then
break;
{Get next window in master window list and continue}
hWndTemp := GetWindow(hWndTemp, GW_HWNDNEXT);
end;
result := hWndTemp;
end;
Once you have the handle you then send the window a WM_CLOSE or WM_QUIT message.
For example:
var
theHandle: THandle;
begin
theHandle := HWndGet('Apps Title');
if theHandle <> 0 then
SendMessage(theHandle, WM_CLOSE, 0, 0);
{ or SendMessage(theHandle, WM_QUIT, 0, 0); }
end; 發表人 - flyup 於 2003/01/14 23:55:00 發表人 - flyup 於 2003/01/14 23:57:30
|
00156
高階會員 發表:45 回覆:195 積分:112 註冊:2002-06-01 發送簡訊給我 |
很有用的功能...我把它改成Delphi...
function HWndGet(PartTitleText:string):HWND; var h:HWND; WndText:array[0..254] of char; WTLength:integer; begin PartTitleText:=UpperCase(PartTitleText); h:=FindWindow(nil,nil); while h<>0 do begin WTLength:=GetWindowText(h,WndText,255); if Pos(PartTitleText,UpperCase(String(WndText)))>0 then break; h:=GetWindow(h,GW_HWNDNEXT); end; Result:=h; end; procedure TForm1.Button1Click(Sender: TObject); var h:HWND; begin h:=HWndGet('Apps Title'); if h<>0 then SendMessage(h,WM_CLOSE,0,0); end;發表人 - 00156 於 2003/01/15 21:28:14 發表人 - 00156 於 2003/01/15 21:29:29 |
chfamy
中階會員 發表:87 回覆:161 積分:54 註冊:2002-05-03 發送簡訊給我 |
|
pceyes
尊榮會員 發表:70 回覆:657 積分:1140 註冊:2003-03-13 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |