用Delphi产生一个最小的可执行程序曾经在网上看到有人说Delphi能够产生大小只有16k的Win32应用程序,而我自己曾经编写过的这种可执行文件大小则是在17k左右,因而我一度猜想Delphi恐怕也只能将代码优化到这种程度了。最近由于测试的目的重新把这个程序写了一遍,才发现利用一些技巧,还能够将文件的大小进一步缩减到8.5k。这个程序也能够显示Delphi作为类似于Visual C++的、非RAD工具的另一个侧面。如果你对此感兴趣的话,请看我是如何做到这一点的。用Delphi生成一个默认的项目,然后用工具栏上的Remove file from PRoject按钮,将唯一的窗体(Form1)从项目中删除。然后选择View->Project Source命令,打开项目文件,并编辑代码如下所示:program MiniApp;uses Windows, Messages;// {$R *.res}const szAppName : PChar = 'MiniApp';function WndProc(AWnd:HWND; message:UINT; wp:WPARAM; lp:LPARAM):LRESULT;stdcall;begin Result := 0; case message of WM_DESTROY: PostQuitMessage(0); else Result := DefWindowProc(AWnd, message, wp, lp); end;end;var wc : WNDCLASS; HMainWnd : HWND; AMsg : MSG;begin with wc do begin style := CS_VREDRAW or CS_HREDRAW; lpfnWndProc := @WndProc; cbClsExtra := 0; cbWndExtra := 0; hIcon := LoadIcon(0, IDI_application); hCursor := LoadCursor(0, IDC_ARROW); hbrBackground := GetSysColorBrush(COLOR_WINDOW); hInstance := HInstance; lpszMenuName := nil; lpszClassName := szAppName; end; RegisterClass(wc); HMainWnd := CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW, Integer(CW_USEDEFAULT), Integer(CW_USEDEFAULT), Integer(CW_USEDEFAULT), Integer(CW_USEDEFAULT), HWND_DESKTOP, 0, HInstance, nil); ShowWindow(HMainWnd, CmdShow); UpdateWindow(HMainWnd); while GetMessage(AMsg, 0, 0, 0) do begin TranslateMessage(AMsg); DispatchMessage(AMsg); end;end.