看过几篇关于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 (hwndButton0) 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写的相差无几。