J'ai lu plusieurs articles sur la comparaison de VC et Delphi, et je suis également intéressé à écrire du code pour l'essayer. Le code est le suivant:
#include <windows.h>
Hwnd hwndbutton;
int Cx, Cy;
Lresult Callback Mainwndproc (Hwnd Hwindow, Uint NMSG, WPARAM WPRM, LPRM LPRM)
{
HDC DC;
PeintStruct ps;
Rect rc;
commutateur (NMSG)
{
cas wm_create:
{
TMTMETRIC TM;
dc = getdc (hwindow);
SelectObject (dc, getStockObject (System_fixed_font));
GetTextMetrics (DC et TM);
cx = tm.tmavecharwidth * 30;
cy = (tm.tmheight + tm.tmexternalading) * 2;
Libéré (Hwindow, DC);
hwndbutton = createwindow (
"bouton",
"Cliquez ici",
WS_CHILD | WS_VISIBLE |
0, 0, cx, cy,
Hwindow,
(Hmenu) 1,
((Lpcreatestruct) lprm) -> hinstance,
NUL
));
retour 0;
casser;
}
cas WM_Destroy:
{
PostquitMessage (0);
retour 0;
casser;
}
cas wm_paint:
{
dc = débutpaint (hwindow, & ps);
GetClientRect (Hwindow, & rc);
rc.bottom = rc.bottom / 2;
DrawText (DC, "Hello, World!", -1, & rc,
Dt_singLeline |
Endpaint (hwindow, & ps);
retour 0;
casser;
}
cas wm_size:
{
if (hwndbutton && (wprm == sizefulScreen || 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);
}
retour 0;
casser;
}
cas wm_command:
{
if (loword (wprm) == 1 && hiword (wprm) == bn_clicked &&
(Hwnd) lprm == hwndbutton)
{
Destroywindow (Hwindow);
}
retour 0;
casser;
}
}
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 |
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",
"Bonjour",
Ws_overlappwindow,
Cw_usefault,
Cw_usefault,
Cw_usefault,
Cw_usefault,
0,
0,
hinst,
NUL
));
Showwindow (Hwindowmain, nshow);
UpdateWindow (Hwindowmain);
While (getMessage (& mymsg, 0, 0, 0)))
{
TranslateMessage (& mymsg);
DispatchMessage (& mymsg);
}
retourner mymsg.wparam;
}
Sélectionnez la version à compiler, activez l'optimisation du code minimum de VC6, et le code d'exécution généré par la compilation est de 36,0 Ko.
Traduisez-le ensuite en code Delphi comme suit:
Programme Wintest;
usages
Windows, messages;
var
Hwndbutton: Hwnd;
CX, Cy: entier;
fonction MainWndProc (Hwindow: hwnd; nmsg: uint; wprm: wparam; lprm: lparam): lresult; stdcall;
var
DC: HDC;
PS: peinture;
RC: Trect;
TM: TextMetric;
PCTST: PCreateStruct;
Commencer
cas nmsg de
Wm_create:
Commencer
dc: = getdc (hwindow);
SelectObject (dc, getStockObject (System_fixed_font));
GetTextMetrics (DC, TM);
cx: = tm.tmavecharwidth * 30;
Cy: = (tm.tmheight + tm.tmexternalading) * 2;
Libéré (Hwindow, DC);
PCTST: = PCreatestruct (LPRM);
hwndbutton: = createwindow (
'bouton',
'Cliquez ici',
Ws_child ou ws_visible ou bs_pushbutton,
0, 0, cx, cy,
Hwindow,
Hmenu (1),
pctst ^ .hinstance,
néant
));
Résultats: = 0;
Sortie;
fin;
Wm_destroy:
Commencer
PostquitMessage (0);
Résultats: = 0;
Sortie;
fin;
Wm_paint:
Commencer
DC: = Beginpaint (Hwindow, PS);
GetClientRect (Hwindow, RC);
rc.bottom: = round (rc.bottom / 2);
DrawText (DC, «Hello, World!», -1, RC,
Dt_singLeline ou dt_center ou dt_vcenter);
Endpaint (Hwindow, PS);
Résultats: = 0;
Sortie;
fin;
Wm_size:
Commencer
if (hwndbutton <> 0) et (wprm = sizefullScreen) ou (wprm = sizenormal)
Commencer
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);
fin;
Résultats: = 0;
Sortie;
fin;
Wm_command:
Commencer
if (loword (wprm) = 1) et (hiword (wprm) = bn_clicked) et
(Hwnd (lprm) = hwndbutton) alors
Commencer
Destroywindow (Hwindow);
fin;
Résultats: = 0;
Sortie;
fin;
fin;
Résultat: = defwindowProc (Hwindow, NMSG, WPRM, LPRM);
fin;
// Winmain
var
Hwindowmain: hwnd;
MYMSG: MSG;
wcex: wndclassex;
Commencer
wcex.cbSize: = sizeof (wndclassex);
wcex.style: = cs_hredraw ou 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',
'Bonjour',
Ws_overlappwindow,
Cw_usefault,
Cw_usefault,
Cw_usefault,
Cw_usefault,
0,
0,
Maininstance,
néant
));
Showwindow (hwindowmain, cmdshow);
UpdateWindow (Hwindowmain);
tandis que getMessage (mymsg, 0, 0, 0) = true do
Commencer
TranslateMessage (MYMSG);
DispatchMessage (MYMSG);
fin;
fin.
Enfin, il a été constaté que le code généré par Delphi n'était que de 16,5k, ce qui était plus de moitié plus petit que VC. Cela montre que Delphi a un bon compilateur et que la fonction de visualisation VCL devrait être un bon outil de développement. À mon avis, Delphi n'a aucun problème à développer de grands systèmes, et ses performances peuvent même dépasser VC. . Delphi ne peut pas non plus utiliser VCL pour écrire des programmes.