Author: Jonas Bilinkevicius
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.
Answer:
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) .
1
2 function HWndGet(partialTitle: string): hWnd;
3 var
4 hWndTemp: hWnd;
5 iLenText: Integer;
6 cTitletemp: array[0..254] of Char;
7 sTitleTemp: string;
8 begin
9 hWndTemp := FindWindow(nil, nil);
10 {Find first window and loop through all subsequent windows in the master window
11 list}
12 while hWndTemp <> 0 do
13 begin
14 {Retrieve caption text from current window}
15 iLenText := GetWindowText(hWndTemp, cTitletemp, 255);
16 sTitleTemp := cTitletemp;
17 sTitleTemp := UpperCase(copy(sTitleTemp, 1, iLenText));
18 {Clean up the return string, preparing for case insensitive comparison.
19 Use appropriate method to determine if the current window's caption either
20 starts with or contains passed string}
21 partialTitle := UpperCase(partialTitle);
22 if pos(partialTitle, sTitleTemp) <> 0 then
23 break;
24 {Get next window in master window list and continue}
25 hWndTemp := GetWindow(hWndTemp, GW_HWNDNEXT);
26 end;
27 result := hWndTemp;
28 end;
Once you have the handle you then send the window a WM_CLOSE or WM_QUIT message.
For example:
29 var
30 theHandle: THandle;
31 begin
32 theHandle := HWndGet('Apps Title');
33 if theHandle <> 0 then
34 SendMessage(theHandle, WM_CLOSE, 0, 0);
35 { or SendMessage(theHandle, WM_QUIT, 0, 0); }
36 end;
|