S
shiningstar
Unregistered / Unconfirmed
GUEST, unregistred user!
代码如下:没用VCL,全是windowsapi。编译出来才17.5k,怎么一执行就占用1.2M<br><br>program project1;<br><br>uses<br> Windows,<br> Messages,<br> ShellAPI;<br>// sysutils;<br><br>{$R *.RES}<br><br>const<br> AppName = 'DeskTop Hide';<br><br>var<br> x: integer;<br> tid: TNotifyIconData;<br> WndClass: array[0..50] of char;<br><br> <br><br>{这个就是处理菜单消息的过程}<br><br>procedure HandleCommand (Wnd: hWnd; Cmd: Word);<br>begin<br> case Cmd of<br> Ord ('A'): MessageBox (0, 'Freeware brian.slack@strath.ac.uk 1997',<br> AppName, mb_ok);<br> Ord ('E'): PostMessage (Wnd, wm_Close, 0, 0);<br> end;<br><br>end;<br><br><br>procedure Panic (szMessage: PChar);<br>begin<br> if szMessage <> Nil then<br> MessageBox (0, szMessage, AppName, mb_ok);<br> Halt (0);<br>end;<br><br><br>function DummyWindowProc (Wnd: hWnd; Msg, wParam: Word; lParam: LongInt)<br>: LongInt; stdcall; {注意这里有一个 stdcall;定义了回调函数}<br><br>var<br> TrayHandle: THandle;<br> dc: hDC;<br>// i: Integer;<br> pm: HMenu;<br> pt: TPoint;<br>begin<br> DummyWindowProc := 0;<br>{下面两句是找到 Win95 任务栏的句柄}<br>// StrPCopy(@WndClass[0], 'Progman');<br> WndClass[0]:='P';<br> WndClass[1]:='r';<br> WndClass[2]:='o';<br> WndClass[3]:='g';<br> WndClass[4]:='m';<br> WndClass[5]:='a';<br> WndClass[6]:='n';<br> WndClass[7]:=#0;<br> TrayHandle := FindWindow(@WndClass[0], nil);<br> {下面开始处理消息}<br> case Msg of<br><br>{收到窗口创建消息 - 在任务栏上显示一个图标}<br> wm_Create: // Program initialisation - just set up a tray icon<br> begin<br> tid.cbSize := sizeof (tid);<br> tid.Wnd := Wnd;<br> tid.uID := 1;<br> tid.uFlags := nif_Message or nif_Icon or nif_Tip;<br> tid.uCallBackMessage := wm_User;<br> tid.hIcon := LoadIcon (hInstance, 'MAINICON');<br> lstrcpy (tid.szTip,'Desktop is on');<br> Shell_NotifyIcon (nim_Add, @tid);<br> end;<br> wm_Destroy: {收到关闭窗口消息时的处理}<br> begin<br> Shell_NotifyIcon (nim_Delete, @tid);<br> PostQuitMessage (0);<br> ShowWindow(TrayHandle, SW_RESTORE);<br> end;<br>{收到菜单消息时调用 HandleCommand 过程,并退出函数}<br> wm_Command: // Command notification<br> begin<br> HandleCommand (Wnd, LoWord (wParam));<br> Exit;<br> end;<br><br>{收到其他用户消息时的处理}<br><br> wm_User: // Had a tray notification - see what to do<br>{如果单击了鼠标左键, 则打开或关闭桌面}<br> if (lParam = wm_LButtonDown) then<br> begin<br> if x = 0 then<br> begin<br> ShowWindow(TrayHandle, SW_HIDE);<br> tid.hIcon := LoadIcon (hInstance, 'OFFICON');<br> lstrcpy (tid.szTip,'Desktop is off');<br> Shell_NotifyIcon (NIM_MODIFY, @tid);<br> x:=1<br> end else<br> begin<br> ShowWindow(TrayHandle, SW_RESTORE);<br> tid.hIcon := LoadIcon (hInstance, 'ONICON');<br> lstrcpy (tid.szTip,'Desktop is on');<br> Shell_NotifyIcon (NIM_MODIFY, @tid);<br> x:= 0;<br> end; {end of if}<br> end else<br><br>{如果是鼠标右键,则动态生成一个弹出式菜单}<br> if (lParam = wm_RButtonDown) then<br> begin<br> GetCursorPos (pt);<br> pm := CreatePopupMenu;<br> AppendMenu (pm, 0, Ord ('A'), 'About DeskTop Hide...');<br> AppendMenu (pm, mf_Separator, 0, Nil);<br> AppendMenu (pm, 0, Ord ('E'), 'Exit DeskTop Hide');<br> SetForegroundWindow (Wnd);<br> dc := GetDC (0);<br> if TrackPopupMenu (pm, tpm_BottomAlign or tpm_RightAlign,<br> pt.x,GetDeviceCaps(dc,HORZRES){pt.y}, 0, Wnd, Nil)<br> then SetForegroundWindow (Wnd);<br> DestroyMenu (pm)<br> end; {end of if}<br> end; {end of case}<br><br>{在处理过消息之后,还要调用默认函数,以完成标准的Windows程序应该执行的任务,所<br><br>以这一句非常重<br><br>要}<br><br> DummyWindowProc := DefWindowProc (Wnd, Msg, wParam, lParam);<br><br>end;<br><br><br>procedure WinMain;<br>var<br> Wnd: hWnd; {声明窗口句柄(Handle)变量}<br> Msg: TMsg; {声明消息变量}<br> cls: TWndClass; {窗口类变量}<br>begin<br>{ Previous instance running ? If so, exit }<br><br>{ 检查是否程序已经运行,如果已经运行则调用Panic过程退出 }<br>if FindWindow (AppName, Nil) <> 0 then<br> Panic (AppName + ' is already running.');<br>{ Register the window class }<br>{ 这里的注册窗口类程序是例行公事,照抄即可}<br> FillChar (cls, sizeof (cls), 0); {用这一句将窗口类变量cls清零)}<br><br> cls.lpfnWndProc := @DummyWindowProc; {取回调函数DummyWindowProc的地址}<br> cls.hInstance := hInstance; {实例句柄}<br> cls.lpszClassName := AppName; {窗口类名}<br> if RegisterClass (cls) = 0 then exit; {注册窗口类cls}<br><br><br>{ 现在可以创建程序的主窗口了-在本程序中是个虚拟窗口}<br><br>{ Now create the dummy window }<br>{ Wnd := CreateWindow('a', 'a', 65536,<br> 0,0,600,600,0,<br> 0,hInstance, nil);<br>}<br> Wnd:= CreateWindow(AppName, AppName, WS_OVERLAPPEDWINDOW,<br> { CW_USEDEFAULT, CW_USEDEFAULT,<br> CW_USEDEFAULT, CW_USEDEFAULT,}<br> 0,0,600,600,<br> 0, 0, HInstance, nil);<br> x:= 0; {变量X其实是个开关变量,记录现在是否已经隐藏了桌面}<br><br>{ 如果窗口创建成功,则显示窗口,并进入消息循环 }<br><br> if Wnd <> 0 then<br> begin<br> ShowWindow (Wnd, sw_Hide{SW_SHOWNORMAL});{本例中窗口是隐藏的}<br><br>{ 下面进入消息循环,该循环将不断运行直到 GetMessage返回0 }<br><br> while GetMessage (Msg, 0, 0, 0) do<br> begin<br> TranslateMessage (Msg);<br> DispatchMessage (Msg);<br> end;<br><br> end;<br><br>end;<br><br>begin<br> WinMain;<br>end.<br><br><br>