Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to find a window by its (partial) title and close it Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
04-Oct-02
Category
System
Language
Delphi 2.x
Views
114
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			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;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC