Saya telah membaca beberapa artikel tentang membandingkan VC dan Delphi, dan saya juga tertarik untuk menulis kode untuk mencobanya. Kodenya adalah sebagai berikut:
#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:
{
TM TextMetric TM;
dc = getDc (hWindow);
SelectObject (DC, GetStockObject (system_fixed_font));
GetTextMetrics (DC, & TM);
cx = tm.tmavecharwidth * 30;
cy = (tm.tmheight + tm.tMexternalleading) * 2;
Dirilis (HWindow, DC);
hwndbutton = createWindow (
"tombol",
"Klik disini",
WS_CHILD |
0, 0, cx, cy,
hwindow,
(Hmenu) 1,
((LpcreateStruct) lprm)-> hinstance,
BATAL
);
kembali 0;
merusak;
}
case wm_destroy:
{
PostquitMessage (0);
kembali 0;
merusak;
}
case wm_paint:
{
dc = beginpaint (hwindow, & ps);
GetClientRect (hwindow, & rc);
rc.bottom = rc.bottom / 2;
Drawtext (DC, "Halo, Dunia!", -1, & RC,
Dt_singleline |
Endpaint (hwindow, & ps);
kembali 0;
merusak;
}
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);
}
kembali 0;
merusak;
}
case wm_command:
{
if (loword (wprm) == 1 && hiword (wprm) == bn_clicked &&
(Hwnd) lprm == hwndbutton)
{
DestroyWindow (HWindow);
}
kembali 0;
merusak;
}
}
return DEFWINDOWPROC (HWindow, NMSG, WPRM, LPRM);
}
// Winmain
Int __stdcall WinMain (Hinstance Hinst, Hinstance HPREV, LPSTR LPCMD, int nshow)
{
Hwndowmain;
Msg mymsg;
Wndclassex wcex;
wcex.cbsize = sizeof (wndclassex);
WCEX.STYLE = CS_HREDRAW |.
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",
"Halo",
Ws_overlappedWindow,
Cw_usedefault,
Cw_usedefault,
Cw_usedefault,
Cw_usedefault,
0,
0,
Hinst,
BATAL
);
ShowWindow (HWindowMain, NShow);
UpdateWindow (hwindowmain);
While (getMessage (& mymsg, 0, 0, 0))
{
TranslateMessage (& mymsg);
DispatchMessage (& mymsg);
}
return mymsg.wparam;
}
Pilih rilis untuk dikompilasi, nyalakan optimalisasi kode minimum VC6, dan kode eksekusi yang dihasilkan oleh kompilasi adalah 36.0kb.
Kemudian menerjemahkannya ke dalam kode Delphi sebagai berikut:
program Wintest;
penggunaan
Windows, pesan;
var
hwndbutton: hwnd;
CX, CY: Integer;
fungsi mainwndproc (hwindow: hwnd; nmsg: uint; wprm: wparam; lprm: lparam): lResult; stdcall;
var
DC: HDC;
PS: PaintStruct;
RC: Trect;
TM: TextMetric;
PCTST: PCreateStruct;
Mulai
case nmsg dari
Wm_create:
Mulai
dc: = getDc (hWindow);
SelectObject (DC, GetStockObject (system_fixed_font));
GetTextMetrics (DC, TM);
cx: = tm.tmaveCharwidth * 30;
cy: = (tm.tmheight + tm.tmexternalleading) * 2;
Dirilis (HWindow, DC);
PCTST: = PCreateStruct (LPRM);
hwndbutton: = createWindow (
'tombol',
'Klik disini',
Ws_child atau ws_visible atau bs_pushbutton,
0, 0, cx, cy,
hwindow,
Hmenu (1),
pctst^.hinstance,
nol
);
Hasil: = 0;
KELUAR;
akhir;
WM_DESTROY:
Mulai
PostquitMessage (0);
Hasil: = 0;
KELUAR;
akhir;
Wm_paint:
Mulai
dc: = beginpaint (hwindow, ps);
GetClientRect (HWindow, RC);
rc.bottom: = round (rc.bottom / 2);
Drawtext (DC, 'Halo, World!', -1, RC,
Dt_singleline atau dt_center atau dt_vcenter);
Endpaint (hwindow, ps);
Hasil: = 0;
KELUAR;
akhir;
WM_SIZE:
Mulai
if (hwndbutton <> 0) dan (wprm = sizefullscreen) atau (wprm = sizenormal) kemudian
Mulai
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);
akhir;
Hasil: = 0;
KELUAR;
akhir;
WM_COMMAND:
Mulai
if (loword (wprm) = 1) dan (hiword (wprm) = bn_klicked) dan
(Hwnd (lprm) = hwndbutton)
Mulai
DestroyWindow (HWindow);
akhir;
Hasil: = 0;
KELUAR;
akhir;
akhir;
Hasil: = DefWindowProc (HWindow, NMSG, WPRM, LPRM);
akhir;
// WinMain
var
hwindowmain: hwnd;
Mymsg: msg;
WCEX: wndclassex;
Mulai
wcex.cbsize: = sizeof (wndclassex);
WCEX.STYLE: = CS_HREDRAW atau 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',
'Halo',
Ws_overlappedWindow,
Cw_usedefault,
Cw_usedefault,
Cw_usedefault,
Cw_usedefault,
0,
0,
Maininstance,
nol
);
Showwindow (hwindowmain, cmdshow);
UpdateWindow (hwindowmain);
Sedangkan getMessage (mymsg, 0, 0, 0) = true do
Mulai
TranslateMessage (mymsg);
DispatchMessage (MyMSG);
akhir;
akhir.
Akhirnya, ditemukan bahwa kode yang dihasilkan oleh Delphi hanya 16,5k, yang lebih dari setengah lebih kecil dari VC. Ini menunjukkan bahwa Delphi memiliki kompiler yang baik, dan fungsi visualisasi VCL harus menjadi alat pengembangan yang baik. Menurut pendapat saya, Delphi tidak memiliki masalah dalam mengembangkan sistem besar, dan kinerjanya bahkan dapat melebihi VC. . Delphi juga tidak dapat menggunakan VCL untuk menulis program.