全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1670
推到 Plurk!
推到 Facebook!

找到視窗(部份)名稱並關閉該程式

 
flyup
資深會員


發表:280
回覆:508
積分:385
註冊:2002-04-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-01-14 22:53:18 IP:61.216.xxx.xxx 未訂閱
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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-01-15 21:17:11 IP:61.56.xxx.xxx 未訂閱
很有用的功能...我把它改成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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-01-15 22:34:18 IP:61.219.xxx.xxx 未訂閱
是否僅有以Timer定時得知執行程式再將它停止? 是否有方法在有新程式被執行時立即得知它?
pceyes
尊榮會員


發表:70
回覆:657
積分:1140
註冊:2003-03-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-04-30 21:48:30 IP:211.76.xxx.xxx 未訂閱
同學!同學! 這以前我作過,我的方法比較笨, 但可以用用看 用Timer將前一次的windowsTitle存放到一個暫存的Stringlists,  然後在下一次存到另一個Strintlist, 兩相比較, 就知道誰是新的WindowTitle 了, 這樣回答還滿意嗎? 努力會更接近成功
------
努力會更接近成功
系統時間:2024-05-05 17:06:45
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!