怎样编写只有几十k的程序,最好给出例程!(200分)

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

huangle

Unregistered / Unconfirmed
GUEST, unregistred user!
C++可以编出只有十多k的程序,不知道delphi可不可以!
 
下面是163.com的贴字,参考一下吧!<br>作 者: teleme(PassWord) 2001-02-09 22:28:56 :0 :0 &nbsp; &nbsp;<br>[回复] [打包] [转贴] &nbsp;<br>&nbsp; 象C语言一样,用Delphi也能写出只有几十K、十几K、甚至只有几K的小程序,本文将以一个能将Win95桌面藏起来的仅有38K的小程序为例教会读者这一技巧,同时本文还将涉及Win95 TrayIcon的显示。 &nbsp; <br>&nbsp; 本程序能写得很小的诀窍是:根本没有用任何的 Form 。也就是说,源程序只有一个Desktop.dpr 文件,程序完全用标准的 WINAPI 写成,由于用到的资源很少,所以程序的体积也很小。当然,用这样的方法编程时不能使用 Delphi的所见即所得的编程方式。} &nbsp; <br><br>{首先看看程序头的写法:} &nbsp; <br><br>program DeskPop; &nbsp; <br><br>uses Windows, Messages, ShellAPI, sysutils; &nbsp; <br><br>{$R *.RES} &nbsp; <br><br>{可以看出本程序比普通的 Delphi 程序用到的 Unit 少的多。 下面声明了全局常量和变量,暂时可以不管他们。} &nbsp; <br><br>const &nbsp; <br><br>AppName = 'DeskTop Hide'; &nbsp; <br><br>var &nbsp; <br><br>x: integer; &nbsp; <br><br>tid: TNotifyIconData; &nbsp; <br><br>WndClass: array[0..50] of char; &nbsp; <br><br><br><br>&nbsp; {现在进入程序的主要部分,首先是定义了一批过程,为了能让读者更好地理解,我们先把这些过程跳过去,先说主程序。主程序位于程序的最后,这样做的好处是可以直接使用程序中定义的过程。主程序十分简单:} &nbsp; <br><br>begin &nbsp; <br><br>WinMain; &nbsp; <br><br>end. &nbsp; <br><br>{看来所有的工作都由 WinMain 完成了。这个 WinMain 使用标准的 WinAPI 函数进行编程,主要步骤是:先声明一个窗口类,然后创建一个主窗口,最后进入消息循环,直到程序结束。} &nbsp; <br><br><br><br>procedure WinMain; &nbsp; <br><br>var &nbsp; <br><br>Wnd: hWnd; {声明窗口句柄(Handle)变量} &nbsp; <br><br>Msg: TMsg; {声明消息变量} &nbsp; <br><br>cls: TWndClass; {窗口类变量} &nbsp; <br><br>begin &nbsp; <br><br>{ Previous instance running ? If so, exit } &nbsp; <br><br>{ 检查是否程序已经运行,如果已经运行则调用Panic过程退出 } &nbsp; <br><br>if FindWindow (AppName, Nil) &lt;&gt; 0 then &nbsp; <br><br>Panic (AppName + ' is already running.'); &nbsp; <br><br><br><br>{ Register the window class } &nbsp; <br><br>{ 这里的注册窗口类程序是例行公事,照抄即可} &nbsp; <br><br>FillChar (cls, sizeof (cls), 0); {用这一句将窗口类变量cls清零) &nbsp; <br><br>cls.lpfnWndProc := @DummyWindowProc; {取回调函数DummyWindowProc的地址} &nbsp; <br><br>cls.hInstance := hInstance; {实例句柄} &nbsp; <br><br>cls.lpszClassName := AppName; {窗口类名} &nbsp; <br><br>RegisterClass (cls); {注册窗口类cls} &nbsp; <br><br><br><br>{ 现在可以创建程序的主窗口了-在本程序中是个虚拟窗口} &nbsp; <br><br>{ Now create the dummy window } &nbsp; <br><br>Wnd := CreateWindow (AppName, AppName, ws_OverlappedWindow, &nbsp; <br><br>cw_UseDefault, cw_UseDefault, cw_UseDefault, cw_UseDefault, &nbsp; <br><br>0, 0, hInstance, Nil); &nbsp; <br><br>x:= 0; {变量X其实是个开关变量,记录现在是否已经隐藏了桌面} &nbsp; <br><br><br><br>{ 如果窗口创建成功,则显示窗口,并进入消息循环 } &nbsp; <br><br>if Wnd &lt;&gt; 0 then &nbsp; <br><br>begin &nbsp; <br><br>ShowWindow (Wnd, sw_Hide);{本例中窗口是隐藏的} &nbsp; <br><br>{ 下面进入消息循环,该循环将不断运行直到 GetMessage返回0 } &nbsp; <br><br>while GetMessage (Msg, 0, 0, 0) do &nbsp; <br><br>begin &nbsp; <br><br>TranslateMessage (Msg); &nbsp; <br><br>DispatchMessage (Msg); &nbsp; <br><br>end; &nbsp; <br><br>end; &nbsp; <br><br>end; &nbsp; <br><br><br><br>{现在看来,程序的主框架很明了,但是它还不能完成任何任务。过程 Panic将显示一个对话框后退出程序,它在 Winmain 过程的开始部分被调用,其实 Panic的功能很简单,之所以要写成一个函数的原因恐怕一方面是结构化编程的需要,另一方面借此避开了 String 和 PChar 的转换。} &nbsp; <br><br>procedure Panic (szMessage: PChar); &nbsp; <br><br>begin &nbsp; <br><br>if szMessage &lt;&gt; Nil then &nbsp; <br><br>MessageBox (0, szMessage, AppName, mb_ok); &nbsp; <br><br>Halt (0); &nbsp; <br><br>end; &nbsp; <br><br><br><br>{下面是回调(Callback)函数 DummyWindowProc,如果说 Winmain 过程是本程序-或者说是本应用或实例的生命,那么这个回调函数可以说是主窗口的灵魂。每一个标准的或者说是规范的Windows窗口都有一个回调函数,以处理发给该窗口的消息。所谓“回调”的意思是这个函数不是由程序直接调用的,而是由Windows 系统调用(还记得我们在窗口类中给lpfnWndProc赋过值吗), 这就是事件驱动编程。} &nbsp; <br><br>function DummyWindowProc (Wnd: hWnd; Msg, wParam: Word; lParam: LongInt) &nbsp; <br><br>: LongInt; stdcall; {注意这里有一个 stdcall;定义了回调函数} &nbsp; <br><br>var &nbsp; <br><br>TrayHandle: THandle; &nbsp; <br><br>dc: hDC; &nbsp; <br><br>i: Integer; &nbsp; <br><br>pm: HMenu; &nbsp; <br><br>pt: TPoint; &nbsp; <br><br>begin &nbsp; <br><br>DummyWindowProc := 0; &nbsp; <br><br>{下面两句是找到 Win95 任务栏的句柄} &nbsp; <br><br>StrPCopy(@WndClass[0], 'Progman'); &nbsp; <br><br>TrayHandle := FindWindow(@WndClass[0], nil); &nbsp; <br><br>{下面开始处理消息} &nbsp; <br><br>case Msg of &nbsp; <br><br>{收到窗口创建消息 - 在任务栏上显示一个图标} &nbsp; <br><br>wm_Create: // Program initialisation - just set up a tray icon &nbsp; <br><br>begin &nbsp; <br><br><br>tid.cbSize := sizeof (tid); &nbsp; <br><br>tid.Wnd := Wnd; &nbsp; <br><br>tid.uID := 1; &nbsp; <br><br>tid.uFlags := nif_Message or nif_Icon or nif_Tip; &nbsp; <br><br>tid.uCallBackMessage := wm_User; &nbsp; <br><br>tid.hIcon := LoadIcon (hInstance, 'MAINICON'); &nbsp; <br><br>lstrcpy (tid.szTip,'Desktop is on'); &nbsp; <br><br>Shell_NotifyIcon (nim_Add, @tid); &nbsp; <br><br>end; &nbsp; <br><br><br><br>wm_Destroy: {收到关闭窗口消息时的处理} &nbsp; <br><br>begin &nbsp; <br><br>Shell_NotifyIcon (nim_Delete, @tid); &nbsp; <br><br>PostQuitMessage (0); &nbsp; <br><br>ShowWindow(TrayHandle, SW_RESTORE); &nbsp; <br><br>end; &nbsp; <br><br>{收到菜单消息时调用 HandleCommand 过程,并退出函数} &nbsp; <br><br>wm_Command: // Command notification &nbsp; <br><br>begin &nbsp; <br><br>HandleCommand (Wnd, LoWord (wParam)); &nbsp; <br><br>Exit; &nbsp; <br><br>end; &nbsp; <br><br>{收到其他用户消息时的处理} &nbsp; <br><br>wm_User: // Had a tray notification - see what to do &nbsp; <br><br>{如果单击了鼠标左键, 则打开或关闭桌面} &nbsp; <br><br>if (lParam = wm_LButtonDown) then &nbsp; <br><br>begin &nbsp; <br><br>if x = 0 then &nbsp; <br><br>begin &nbsp; <br><br>ShowWindow(TrayHandle, SW_HIDE); &nbsp; <br><br>tid.hIcon := LoadIcon (hInstance, 'offICON'); &nbsp; <br><br>lstrcpy (tid.szTip,'Desktop is off'); &nbsp; <br><br>Shell_NotifyIcon (NIM_MODIFY, @tid); &nbsp; <br><br>x:=1 &nbsp; <br><br>end else &nbsp; <br><br>begin &nbsp; <br><br>ShowWindow(TrayHandle, SW_RESTORE); &nbsp; <br><br>tid.hIcon := LoadIcon (hInstance, 'ONICON'); &nbsp; <br><br>lstrcpy (tid.szTip,'Desktop is on'); &nbsp; <br><br>Shell_NotifyIcon (NIM_MODIFY, @tid); &nbsp; <br><br>x:= 0; &nbsp; <br><br>end; {end of if} &nbsp; <br><br>end else &nbsp; <br><br>{如果是鼠标右键,则动态生成一个弹出式菜单} &nbsp; <br><br>if (lParam = wm_RButtonDown) then &nbsp; <br><br>begin &nbsp; <br><br>GetCursorPos (pt); &nbsp; <br><br>pm := CreatePopupMenu; &nbsp; <br><br>AppendMenu (pm, 0, Ord ('A'), 'About DeskTop Hide...'); &nbsp; <br><br>AppendMenu (pm, mf_Separator, 0, Nil); &nbsp; <br><br>AppendMenu (pm, 0, Ord ('E'), 'Exit DeskTop Hide'); &nbsp; <br><br>SetForegroundWindow (Wnd); &nbsp; <br><br>dc := GetDC (0); &nbsp; <br><br>if TrackPopupMenu (pm, tpm_BottomAlign or tpm_RightAlign, &nbsp; <br><br>pt.x,GetDeviceCaps(dc,HORZRES){pt.y}, 0, Wnd, Nil) &nbsp; <br><br>then SetForegroundWindow (Wnd); &nbsp; <br><br>DestroyMenu (pm) &nbsp; <br><br>end; {end of if} &nbsp; <br><br>end; {end of case} &nbsp; <br><br>{在处理过消息之后,还要调用默认函数,以完成标准的Windows程序应该执行的任务,所 &nbsp; <br><br>以这一句非常重 &nbsp; <br><br>要} &nbsp; <br><br>DummyWindowProc := DefWindowProc (Wnd, Msg, wParam, lParam); &nbsp; <br><br>end; &nbsp; <br><br><br><br>{这个就是处理菜单消息的过程} &nbsp; <br><br>procedure HandleCommand (Wnd: hWnd; Cmd: Word); &nbsp; <br><br>begin &nbsp; <br><br>case Cmd of &nbsp; <br><br>Ord ('A'): MessageBox (0, 'Freeware brian.slack@strath.ac.uk 1997', &nbsp; <br><br>AppName, mb_ok); &nbsp; <br><br>Ord ('E'): PostMessage (Wnd, wm_Close, 0, 0); &nbsp; <br><br>end; &nbsp; <br><br>end; &nbsp; <br><br>&nbsp; &nbsp; 至此我们已经完成了这个只有38K的能将Win95桌面隐藏起来的程序,只要将本文中所有的函数和过程的顺序倒置,并将主程序放到最后,即可编译通过 <br>
 
Delphi能编的最小的基于Windows窗体的程序只有10多K,C++ Builder却永远做不到!<br>要这样做的必须条件是不能用VCL控件,全用API!实际上就是Windows SDK编程,你喜欢吗?
 
如果使用修改过的System.dcu,用Delphi编写出的最小程序体积可以达到8.5K左右。<br>不过我很奇怪,有些标准的windows程序竟然可以达到3K左右的大小,难道使用了汇编?
 
多人接受答案了。
 
后退
顶部