看過幾篇關於VC和Delphi比較的文章,自己也有心寫寫代碼試試,我在VC6下新建了一個工程,叫WinTest。代碼如下:
#include <Windows.h>
HWND hwndButton;
int cx, cy;
LRESULT CALLBACK MainWndProc (HWND hWindow, UINT nMsg, WPARAM wPrm, LPARAM lPrm)
{
HDC dc;
PAINTSTRUCT ps;
RECT rc;
switch (nMsg)
{
case WM_CREATE:
{
TEXTMETRIC tm;
dc = GetDC (hWindow);
SelectObject (dc, GetStockObject (SYSTEM_FIXED_FONT));
GetTextMetrics (dc, &tm);
cx = tm.tmAveCharWidth * 30;
cy = (tm.tmHeight + tm.tmExternalLeading) * 2;
ReleaseDC (hWindow, dc);
hwndButton = CreateWindow (
"button",
"Click Here",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, cx, cy,
hWindow,
(HMENU) 1,
((LPCREATESTRUCT) lPrm)->hInstance,
NULL
);
return 0;
break;
}
case WM_DESTROY:
{
PostQuitMessage (0);
return 0;
break;
}
case WM_PAINT:
{
dc = BeginPaint (hWindow, &ps);
GetClIEntRect (hWindow, &rc);
rc.bottom = rc.bottom / 2;
DrawText (dc, "Hello, World!", -1, &rc,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint (hWindow, &ps);
return 0;
break;
}
case WM_SIZE:
{
if (hwndButton && (wPrm == SIZEFULLSCREEN ||wPrm == SIZENORMAL))
{
rc.left = (LOWord(lPrm) - cx) / 2;
rc.top = HIWord(lPrm) * 3 / 4 - cy / 2;
MoveWindow (hwndButton,rc.left, rc.top, cx, cy, TRUE);
}
return 0;
break;
}
case WM_COMMAND:
{
if (LOWORD(wPrm) == 1 && HIWord(wPrm) == BN_CLICKED &&
(HWND) lPrm == hwndButton)
{
DestroyWindow (hWindow);
}
return 0;
break;
}
}
return DefWindowProc (hWindow, nMsg, wPrm, lPrm);
}
//winmain
int __stdcall WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
HWND hWindowMain;
MSG MyMsg;
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)MainWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInst;
wcex.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor (NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszClassName = "WinTestWin";
wcex.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
RegisterClassEx (&wcex);
hWindowMain = CreateWindow (
"WinTestWin",
"Hello",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
hInst,
NULL
);
ShowWindow (hWindowMain, nShow);
UpdateWindow (hWindowMain);
while (GetMessage (&MyMsg, 0, 0, 0))
{
TranslateMessage (&MyMsg);
DispatchMessage (&MyMsg);
}
return MyMsg.wParam;
}
其中選擇Release方式編譯,打開VC6的最小代碼優化,編譯生成的執行碼為36.0KB,
然後將其翻譯成Delphi代碼,如下:
program WinTest;
uses
Windows,Messages;
var
hwndButton:HWND;
cx,cy:Integer;
function MainWndProc (hWindow:HWND;nMsg:UINT;wPrm:WPARAM;lPrm:LPARAM):LRESULT;stdcall;
var
dc:HDC;
ps:PAINTSTRUCT;
rc:TRect;
tm:TEXTMETRIC;
pctst:PCREATESTRUCT;
begin
case nMsg of
WM_CREATE:
begin
dc := GetDC (hWindow);
SelectObject (dc, GetStockObject (SYSTEM_FIXED_FONT));
GetTextMetrics (dc, tm);
cx := tm.tmAveCharWidth * 30;
cy := (tm.tmHeight + tm.tmExternalLeading) * 2;
ReleaseDC (hWindow, dc);
pctst:= PCREATESTRUCT(lPrm);
hwndButton := CreateWindow(
'button',
'Click Here',
WS_CHILD or WS_VISIBLE or BS_PUSHBUTTON,
0, 0, cx, cy,
hWindow,
HMENU(1),
pctst^.hInstance,
nil
);
Result:=0;
Exit;
end;
WM_DESTROY:
begin
PostQuitMessage(0);
Result:=0;
Exit;
end;
WM_PAINT:
begin
dc := BeginPaint (hWindow, ps);
GetClIEntRect (hWindow, rc);
rc.bottom := Round(rc.bottom / 2);
DrawText (dc, 'Hello, World!', -1, rc,
DT_SINGLELINE or DT_CENTER or DT_VCENTER);
EndPaint (hWindow, ps);
Result:= 0;
Exit;
end;
WM_SIZE:
begin
if (hwndButton<>0) and (wPrm = SIZEFULLSCREEN) or (wPrm = SIZENORMAL) then
begin
rc.left := Round((LOWord(lPrm) - cx) / 2);
rc.top := Round(HIWord(lPrm) * 3 / 4 - cy / 2);
MoveWindow (hwndButton,rc.left, rc.top, cx, cy, True);
end;
Result:= 0;
Exit;
end;
WM_COMMAND:
begin
if (LOWORD(wPrm) = 1) and (HIWord(wPrm) = BN_CLICKED) and
(HWND(lPrm) = hwndButton) then
begin
DestroyWindow (hWindow);
end;
Result:= 0;
Exit;
end;
end;
Result:=DefWindowProc (hWindow, nMsg, wPrm, lPrm);
end;
//winmain
var
hWindowMain:HWND;
MyMsg:MSG;
wcex:WNDCLASSEX;
begin
wcex.cbSize := SizeOf(WNDCLASSEX);
wcex.style := CS_HREDRAW or CS_VREDRAW;
wcex.lpfnWndProc := @MainWndProc;
wcex.cbClsExtra := 0;
wcex.cbWndExtra := 0;
wcex.hInstance := MainInstance;
wcex.hIcon := LoadIcon (0, IDI_APPLICATION);
wcex.hCursor := LoadCursor (0, IDC_ARROW);
wcex.hbrBackground := HBRUSH(COLOR_WINDOW+1);
wcex.lpszClassName := 'WinTestWin';
wcex.hIconSm := LoadIcon (0, IDI_APPLICATION);
RegisterClassEx (wcex);
hWindowMain := CreateWindow (
'WinTestWin',
'Hello',
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
MainInstance,
nil
);
ShowWindow (hWindowMain, CmdShow);
UpdateWindow (hWindowMain);
while GetMessage (MyMsg, 0, 0, 0)=True do
begin
TranslateMessage (MyMsg);
DispatchMessage (MyMsg);
end;
end.
最後發現Delphi生成的代碼僅有16.5k,比VC小了一半以上。說明Delphi有一個不錯的編譯器,加之VCL的可視化功能,應該是一個很好的開發工具。 以我來看,Delphi開發大型系統是一點問題沒有的,甚至可能性能將超過VC,只是VCL的高封裝層次使得使用VCL的程序通常都大,但開發效率卻不是VC可以比的。 Delphi也同樣可以不使用VCL寫程序,像網上有人將Quake2的源碼翻譯成了Delphi的,效果與C寫的相差無幾。