Eu li vários artigos sobre a comparação de VC e Delphi e também estou interessado em escrever código para experimentá -lo. O código é o seguinte:
#include <windows.h>
Hwnd hwndbutton;
int cx, cy;
LRESULT RECONLECT ROBLET MAINWNDPROC (HWND HWindow, UINT NMSG, WPARAM WPRM, LPARAM LPRM)
{
HDC DC;
Pintura ps;
Ret rc;
Switch (NMSG)
{
case wm_create:
{
Tm textmétrico;
dc = getDC (hwindow);
SelectObject (DC, getStockObject (System_Fixed_Font));
GetTextMetrics (DC, & tm);
cx = tm.tMaveCharwidth * 30;
cy = (tm.tmHeight + tm.tMexternAlleading) * 2;
Lançado (Hwindow, DC);
hwndbutton = createWindow (
"botão",
"Clique aqui",
Ws_child |
0, 0, cx, cy,
hwindow,
(Hmenu) 1,
((Lpcreatestruct) lprm)-> hinstance,
NULO
);
retornar 0;
quebrar;
}
case wm_destroy:
{
Pós -QuitMessage (0);
retornar 0;
quebrar;
}
case wm_paint:
{
dc = BeginPaint (hwindow, & ps);
GetClientRect (hwindow, & rc);
rc.bottom = rc.bottom / 2;
DrawText (DC, "Olá, mundo!", -1, & rc,
DT_SINGLINE |
Endpaint (hwindow, & ps);
retornar 0;
quebrar;
}
case wm_size:
{
if (hwndbutton && (wprm == sizefullcreen || 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);
}
retornar 0;
quebrar;
}
case wm_command:
{
if (loword (wprm) == 1 && hiword (wprm) == bn_clicked &&
(HWND) LPRM == HWNDBUTTON)
{
Destrowwindow (hwindow);
}
retornar 0;
quebrar;
}
}
retornar DefwindowProc (Hwindow, NMSG, WPRM, LPRM);
}
// Winmain
int __stdcall winmain (hinstance hinst, hinstance hprev, lpstr lpcmd, int nshow)
{
Hwnd hwindowmain;
Msg mymsg;
Wndcllassex wcex;
wcex.cbsize = sizeof (wndcllassex);
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",
"Olá",
Ws_overlappedwindow,
Cw_usedefault,
Cw_usedefault,
Cw_usedefault,
Cw_usedefault,
0,
0,
Hinst,
NULO
);
ShowWindow (Hwindowmain, NSHOW);
UpdateWindow (HwindowMain);
Enquanto (getMessage (& mymsg, 0, 0, 0))
{
TRADLATEMESSAGE (& MYMSG);
DispatchMessage (& mymsg);
}
retornar mymsg.wparam;
}
Selecione a versão para compilar, ligue a otimização mínima de código do VC6 e o código de execução gerado pelo compilação é de 36,0kb.
Em seguida, traduz -o para o código Delphi da seguinte maneira:
Programa Wintest;
usos
Janelas, mensagens;
var
HWndButton: HWND;
cx, cy: inteiro;
função mainWndProc (hwindow: hwnd; nmsg: uint; wprm: wparam; lprm: lparam): lResult; stdcall;
var
DC: HDC;
PS: pintura;
RC: Trect;
TM: TextMetric;
PCTST: PCreatestruct;
Começar
caso nmsg de
Wm_create:
Começar
dc: = getDC (hwindow);
SelectObject (DC, getStockObject (System_Fixed_Font));
GetTextMetrics (DC, TM);
cx: = tm.tMaveCharwidth * 30;
cy: = (tm.tmHeight + tm.tMexternAlleading) * 2;
Lançado (Hwindow, DC);
pctst: = pcreatestruct (LPRM);
HWNDButton: = CreateWindow (
'botão',
'Clique aqui',
Ws_child ou ws_visible ou bs_pushbutton,
0, 0, cx, cy,
hwindow,
Hmenu (1),
pctst^.hinStance,
nil
);
Resultados: = 0;
Saída;
fim;
Wm_destroy:
Começar
Pós -QuitMessage (0);
Resultados: = 0;
Saída;
fim;
Wm_paint:
Começar
DC: = Beginpaint (hwindow, ps);
GetClientRect (hwindow, rc);
rc.bottom: = redond (rc.bottom / 2);
DrawText (dc, 'Olá, mundo!', -1, rc,
Dt_singLeline ou dt_center ou dt_vcenter);
Endpaint (hwindow, ps);
Resultados: = 0;
Saída;
fim;
Wm_size:
Começar
if (hwndbutton <> 0) e (wprm = sizefullcreen) ou (wprm = sizenormal) então
Começar
rc.left: = redond ((Loword (LPRM) - CX) / 2);
rc.top: = redonda (hiword (lprm) * 3/4 - cy / 2);
Movewindow (hwndbutton, rc.left, rc.top, cx, cy, true);
fim;
Resultados: = 0;
Saída;
fim;
WM_COMMAND:
Começar
if (loword (wprm) = 1) e (hiword (wprm) = bn_clicked) e
(HWND (LPRM) = hWndButton) então
Começar
Destrowwindow (hwindow);
fim;
Resultados: = 0;
Saída;
fim;
fim;
Resultado: = DEFWindowProc (Hwindow, NMSG, WPRM, LPRM);
fim;
// Winmain
var
hwindowmain: hwnd;
Mymsg: msg;
WCEX: wndcllassex;
Começar
wcex.cbsize: = sizeof (wndcllassex);
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',
'Olá',
Ws_overlappedwindow,
Cw_usedefault,
Cw_usedefault,
Cw_usedefault,
Cw_usedefault,
0,
0,
MainInstance,
nil
);
ShowWindow (Hwindowmain, CMDShow);
UpdateWindow (HwindowMain);
enquanto getMessage (mymsg, 0, 0, 0) = true do
Começar
TRADLATEMESSAGE (MYMSG);
DispatchMessage (myMSG);
fim;
fim.
Finalmente, verificou -se que o código gerado por Delphi era de apenas 16,5k, que era mais da metade menor que o VC. Isso mostra que o Delphi tem um bom compilador e a função de visualização do VCL deve ser uma boa ferramenta de desenvolvimento. Na minha opinião, a Delphi não tem problema no desenvolvimento de grandes sistemas, e seu desempenho pode até exceder o VC. . Delphi também não pode usar o VCL para escrever programas.