为何运行占用1.2M内存?(100分)

  • 主题发起人 主题发起人 shiningstar
  • 开始时间 开始时间
S

shiningstar

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