为什么我的程序不能退出(关于NotifyIcon)(100分)

  • 主题发起人 主题发起人 hermit
  • 开始时间 开始时间
H

hermit

Unregistered / Unconfirmed
GUEST, unregistred user!
高手请看下面的程序,不加循环延时可以正常退出,加上了就不行,为什么?<br><br>program Project_2;<br><br>uses<br><br>&nbsp; Sysutils,<br>&nbsp; Windows,<br>&nbsp; Messages,<br>&nbsp; ShellApi;<br><br>{$R theIcon.res}<br><br>Const<br>&nbsp; WM_MyCallback = WM_User+1000;<br>&nbsp; TrayIconTip = 'This is the notifyIcon procedure';<br><br>var<br>&nbsp; MainWindow : HWND;<br>&nbsp; IconData : TNotifyIconData;<br><br>procedure DeleteTrayIcon;<br>begin<br>&nbsp; Shell_NotifyIcon(NIM_DELETE,@IconData);<br>end;<br><br>Function WndProc(Window : hWnd; Msg,WParam,LParam : Integer): Integer; StdCall;<br>var i,j,k : integer;<br>&nbsp;Begin<br>&nbsp; Result := 0;<br>&nbsp;Case Msg of<br>&nbsp;wm_NCCreate : Result := 1;<br>&nbsp;wm_Destroy : PostQuitMessage(0);<br><br>&nbsp;wm_MyCallback :<br>&nbsp; Begin<br>&nbsp; &nbsp;MessageBeep(0);<br>&nbsp; // 不加下面的循环,程序可以正常退出,加了则不行,why?<br>&nbsp; &nbsp;for i:=0 to 10000 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; for j:=0 to 10000 do<br>&nbsp; &nbsp; &nbsp; &nbsp; k:=i+j;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp;PostMessage(MainWindow,WM_Destroy,0,0);<br>&nbsp; &nbsp;End;<br><br>&nbsp;Else Result := DefWindowProc(MainWindow,Msg,WParam,LParam);<br><br>&nbsp;End;<br>End;<br><br><br>Procedure CreateWindow;<br>Var<br>&nbsp;WC : TWndClass;<br><br>Begin<br>&nbsp;With WC do Begin<br>&nbsp; &nbsp;Style := 0;<br>&nbsp; &nbsp;lpfnWndProc := @WndProc;<br>&nbsp; &nbsp;cbClsExtra := 0;<br>&nbsp; &nbsp;cbWndExtra := 0;<br>&nbsp; &nbsp;hIcon := 0;<br>&nbsp; &nbsp;hCursor := 0;<br>&nbsp; &nbsp;hbrBackground := 0;<br>&nbsp; &nbsp;lpszMenuName := nil;<br>&nbsp; &nbsp;lpszClassName := 'MyTrayIconClass';<br>&nbsp; &nbsp;hInstance := SysInit.hInstance;<br>&nbsp;end;<br>&nbsp;RegisterClass(WC);<br>&nbsp;MainWindow := Windows.CreateWindow('MyTrayIconClass','MyVeryOwnTrayIconWindow',<br>&nbsp; &nbsp; &nbsp; ws_OverlappedWindow,0,0,0,0,0,0,hInstance,nil);<br>&nbsp;ShowWindow(MainWindow,sw_Hide);<br>&nbsp;UpdateWindow(MainWindow);<br><br>End;<br>Procedure AddTrayIcon;<br>Begin<br>&nbsp;With IconData do Begin<br>&nbsp; cbSize := SizeOf(IconData);<br>&nbsp; Wnd := MainWindow;<br>&nbsp; uID := 0;<br>&nbsp; uFlags := nif_Icon Or nif_Message Or nif_Tip;<br>&nbsp; uCallBackMessage := wm_MyCallBack;<br>&nbsp; hIcon := LoadIcon(hInstance,'MYICON');<br>&nbsp; StrCopy(szTip,PChar(TrayIconTip));<br>&nbsp;End;<br>&nbsp;Shell_NotifyIcon(NIM_Add,@IconData);<br>End;<br><br><br>Procedure WinMain;<br>&nbsp;Var Msg : TMsg;<br>Begin<br>&nbsp;CreateWindow;<br>&nbsp;AddTrayIcon;<br>&nbsp;While GetMessage(Msg,0,0,0) do Begin<br>&nbsp; TranslateMessage(Msg);<br>&nbsp; DispatchMessage(Msg);<br><br>&nbsp;End;<br>&nbsp;DeleteTrayIcon;<br>&nbsp;End; // end of RunTrayApplication<br><br><br>begin<br>&nbsp;WinMain;<br><br><br>end.
 
在循环中加入<br>application.processmessage;<br>
 
循环量i,j的范围取的小一点看看,若能推出,则逐步加大再看看
 
WM_DESTROY为什么不被处理,因而就不能退出程序,每次都跳到wm_MyCallback<br>分支执行。也即过一阵就响铃一声,一直不停。<br>我试了一下,循环次数少时可以正常退出,但这是为什么呢?
 
在循环后再加句beep看看循环有没有正常结束
 
我知道了,主要是因为我没有足够的耐心,多等一会儿会正常退出的。<br>当循环次数较多时,在这期间windows已经向程序的消息队列发送了好多条通知消息,因而WM_DESTROY的处理很靠后,要响铃好多声。<br>如果循环次数较少,则很快就执行到PostMessage了,windows还没有将通知消息<br>放入消息队列,因此Post的WM_DESTROY在消息队列中很靠前,一般只响铃一声就<br>退出了。<br><br><br>&nbsp;
 
谢谢各位的提示
 
多人接受答案了。
 
后退
顶部