S
sy0116
Unregistered / Unconfirmed
GUEST, unregistred user!
我想要写一个系统托盘程序,只需要显示一个小图标,然后如果用右键点击一下就弹出一个菜单,找到了一段这样的代码,我试验过后发现这样确实可以实现我想要的功能,但是程序运行一、两个小时后右键菜单就显示不出来了,在显示不出右键菜单时TrackPopupMenu函数虽然返回True,但是我通过GetLastError函数发现有错误ERROR_INVALID_HANDLE,而前面CreatePopupMenu却没有任何错误。代码中的MyDll.dll只需要LoadLibrary就会开启两个线程用TextOutA函数在屏幕上不断的输出文字。<br>program Shell;<br><br>uses<br> Windows,ShellAPI,Messages,SysUtils;<br><br>{$R ShellCPU.res}<br>const<br> ClassName='myownclassname';<br><br>var<br> msg:TMsg;<br> WndClass:TWndClassA;<br> hWnd,hPop:THandle;<br> hMap:THandle;<br> pBoolointer;<br> ShowUsage:BOOL;<br> WM_TASKBARCREATED:Cardinal;<br> PopTextChar;<br> NotifyData: NOTIFYICONDATA;<br> bInit:BOOL;<br>procedure Shell_AddICON(Add:Boolean=True);<br>begin<br> with NotifyData do<br> begin<br> cbSize:=SizeOf(NotifyData);<br> Wnd:=hWnd;<br> uID:= 0;<br> uFlags:=NIF_MESSAGE or NIF_ICON;<br> uCallbackMessage:=WM_USER+888;<br> end;<br> if Add then<br> Shell_NotifyIcon(NIM_ADD,@NotifyData)<br> else<br> Shell_NotifyIcon(NIM_DELETE,@NotifyData);<br>end;<br>procedure InitShare;<br>begin<br> hMap:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(Bool),'myinformation');<br> pBool:=MapViewOfFile(hMap,FILE_MAP_ALL_ACCESS,0,0,0);<br> CopyMemory(pBool,@ShowUsage,SizeOf(BOOL));<br>end;<br><br>procedure WriteShare;<br>begin<br> CopyMemory(pBool,@ShowUsage,SizeOf(BOOL));<br>end; <br><br>function MainWndProc(HWin: THandle; MsgID: UINT; wParam, lParam: Integer): LRESULT; stdcall;<br>var<br> iButton:Integer;<br> hdcS:HDC;<br> dwErrWORD;<br> tpCurrentPoint:TPoint;<br>begin<br> case MsgID of<br> WM_CREATE:<br> begin<br> WM_TASKBARCREATED:=RegisterWindowMessage('TaskbarCreated');<br> end; <br> WM_CLOSE:<br> begin<br> UnmapViewOfFile(pBool);<br> CloseHandle(hMap);<br> DestroyWindow(hWnd);<br> end;<br> WM_DESTROY:<br> begin<br> PostQuitMessage(0);<br> end;<br> WM_USER+888:<br> begin<br><br> if ((wParam=0) and (lParam=$201)) then<br> begin<br> ShowUsage:=not ShowUsage;<br> WriteShare;<br> end;<br> if ((wParam=0) and (lParam=$205)) then//在图标上按了右键<br> begin<br> hPop:=CreatePopupMenu;<br> AppendMenu(hPop,MF_STRING,1,'退出');<br> AppendMenu(hPop,MF_SEPARATOR,9,nil);<br> SetForegroundWindow(HWin);<br> GetCursorPos(tpCurrentPoint);<br> if ShowUsage=True then<br> PopText:=PChar('不显示')<br> else<br> PopText:=PChar('显示');<br> AppendMenu(hPop,MF_CHANGE or MF_STRING,2,PopText);<br> iButton:=Integer(TrackPopupMenu(hPop,<br> TPM_LEFTALIGN or TPM_LEFTBUTTON,<br> tpCurrentPoint.X,<br> tpCurrentPoint.y,<br> 0,<br> hWnd,<br> nil));<br> DestroyMenu(hPop);<br> end;<br> end;<br> end;<br> if MsgID= WM_COMMAND then<br> begin<br> iButton:=wParam;<br> if iButton=1 then<br> begin<br> Shell_AddICON(False);<br> ExitProcess(0);<br> end<br> else if iButton=2 then<br> begin<br> ShowUsage:=not ShowUsage;<br> WriteShare;<br> if ShowUsage then<br> NotifyData.hIcon:=LoadIcon(HInstance,'TRAYH')<br> else<br> NotifyData.hIcon:=LoadIcon(HInstance,'TRAYS');<br> end;<br> Shell_NotifyIcon(NIM_MODIFY,@NotifyData);<br> end;<br> if MsgID=WM_TASKBARCREATED then<br> begin<br> Shell_AddICON;<br> end; <br> Result:=DefWindowProc(HWin,MsgID,wParam,lParam);<br>end;<br><br>begin<br> if ParamStr(1)='f' then<br> begin<br> ShowUsage:=False;<br> NotifyData.hIcon:=LoadIcon(HInstance,'TRAYS');<br> end<br><br> else<br> begin<br> ShowUsage:=True;<br> NotifyData.hIcon:=LoadIcon(HInstance,'TRAYH');<br> end; <br><br> with WndClass do<br> begin<br> style:=CS_HREDRAW or CS_VREDRAW;<br> lpfnWndProc:=@MainWndProc;<br> hInstance:= sysinit.HInstance;<br> hIcon:=LoadIcon(0,PChar(IDI_APPLICATION));<br> hCursor:=LoadCursor(0,IDC_ARROW);<br> hbrBackground:=GetStockObject(WHITE_BRUSH);<br> lpszClassName:=ClassName;<br> end;<br> if RegisterClass(WndClass)=0 then<br> ExitProcess(0);<br> hWnd:=CreateWindowEx(0,PChar(ClassName),'Shell',WS_OVERLAPPED,0,0,100,100,0,0,hInstance,nil);<br> if hWnd=0 then<br> begin<br> MessageBox(0,'无法创建窗口!','错误',0);<br> ExitProcess(0);<br> end;<br> InitShare;<br> Shell_AddICON;<br> LoadLibrary('MyDll.dll');<br> while GetMessage(msg,0,0,0) do<br> begin<br> TranslateMessage(msg);<br> DispatchMessage(msg);<br> end;<br>end.