Compare VC and Delphi WINTEST project

xiaoxiao2021-03-06  42

I have seen a few articles about VC and Delphi comparison, I also have a heart to write code, I have built a project in VC6, called Wintest. The code is as follows: #include

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_singlex | 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 (WPRM) == 1 && HiWord (WPRM) == Bn_Clicked && (HWND) {DestroyWindow (Hwindow);} Return 0; Break;}

Return DefwindowProc (Hwindow, NMSG, WPRM, LPRM);

// WinMain Int __stddcall 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) {TranslateMsSage (& mymsg); DispatchMessage (& mymsg);} return mymsg.wparam;

Which selected Release mode is compiled, open the minimum code optimization of VC6, and the compiled execution code is 36.0KB, then translate it into a Delphi code, as follows: 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;

WM_Paint: Begin DC: = BeginPaint (Hwindow, PS); GetClientRect (Hwindow, RC);

Rc.Bottom: = Round (rc.bottom / 2); DrawText (DC, 'Hello, World!', -1, RC, DT_SINGLINE OR DT_CENTER OR DT_VCENTER);

Endpaint (hwindow, ps); result: = 0; exit; end;

WM_SIZE: Begin if (HWndButton <> 0) OR (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;

WM_COMMAND: BeGin IF (WPRM) = 1) AND (Hwnd (LPRM) = bn_clicked) and (hwnd (lprm) = hwndbutton) THEN Begin DestroyWindow (HWINDOW); END; Result: = 0;

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 beg, translateMessage (Mymsg); DispatchMessage (mymsg); end; end. Finally, the code generated by Delphi is only 16.5k, more than half of VC. Note Delphi has a good compiler, plus VCL visualization, should be a good development tool. In me, Delphi has developed a large system is no problem, and even possibilities will exceed VC, just VCL's high package hierarchy makes the program usually large, but the development efficiency is not Vc. Delphi also can also use the VCL write program, and someone translated Quake2's source code into Delphi, the effect is almost the same as C wrist.

转载请注明原文地址:https://www.9cbs.com/read-68607.html

New Post(0)