熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

比較VC和Delphi的WinTest工程

2022-06-13   來源: Delphi編程 
#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 = tmtmAveCharWidth * ;
  cy = (tmtmHeight + tmtmExternalLeading) * ;
  ReleaseDC (hWindow dc);
  
  hwndButton = CreateWindow (
  button
  Click Here
  WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON
   cx cy
  hWindow
  (HMENU)
  ((LPCREATESTRUCT) lPrm)>hInstance
  NULL
  );
  
  return ;
  break;
  }
  
  case WM_DESTROY:
  {
  PostQuitMessage ();
  return ;
  break;
  }
  
  case WM_PAINT:
  {
  dc = BeginPaint (hWindow &ps);
  GetClientRect (hWindow &rc);
  
  rcbottom = rcbottom / ;
  DrawText (dc Hello World! &rc
  DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  
  EndPaint (hWindow &ps);
  return ;
  break;
  }
  
  case WM_SIZE:
  {
  if (hwndButton && (wPrm == SIZEFULLSCREEN ||wPrm == SIZENORMAL))
  {
  rcleft = (LOWORD(lPrm) cx) / ;
  rctop = HIWORD(lPrm) * / cy / ;
  MoveWindow (hwndButtonrcleft rctop cx cy TRUE);
  }
  return ;
  break;
  }
  
  case WM_COMMAND:
  {
  if (LOWORD(wPrm) == && HIWORD(wPrm) == BN_CLICKED &&
  (HWND) lPrm == hwndButton)
  {
  DestroyWindow (hWindow);
  }
  return ;
  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;
  
  wcexcbSize = sizeof(WNDCLASSEX);
  wcexstyle  = CS_HREDRAW | CS_VREDRAW;
  wcexlpfnWndProc = (WNDPROC)MainWndProc;
  wcexcbClsExtra = ;
  wcexcbWndExtra = ;
  wcexhInstance = hInst;
  wcexhIcon  = LoadIcon (NULL IDI_APPLICATION);
  wcexhCursor = LoadCursor (NULL IDC_ARROW);
  wcexhbrBackground = (HBRUSH)(COLOR_WINDOW+);
  wcexlpszClassName = WinTestWin;
  wcexhIconSm = LoadIcon (NULL IDI_APPLICATION);
  
  
  RegisterClassEx (&wcex);
  
  hWindowMain = CreateWindow (
  WinTestWin
  Hello
  WS_OVERLAPPEDWINDOW
  CW_USEDEFAULT
  CW_USEDEFAULT
  CW_USEDEFAULT
  CW_USEDEFAULT
  
  
  hInst
  NULL
  );
  
  ShowWindow (hWindowMain nShow);
  UpdateWindow (hWindowMain);
  
  while (GetMessage (&MyMsg ))
  {
  TranslateMessage (&MyMsg);
  DispatchMessage (&MyMsg);
  }
  return MyMsgwParam;
  }
  
  
  其中選擇Release方式編譯打開VC的最小代碼優化編譯生成的執行碼為KB
  然後將其翻譯成Delphi代碼如下
  
  program WinTest;
  
  uses
  WindowsMessages;
  
  var
  hwndButton:HWND;
  cxcy: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 := tmtmAveCharWidth * ;
  cy := (tmtmHeight + tmtmExternalLeading) * ;
  ReleaseDC (hWindow dc);
  pctst:= PCREATESTRUCT(lPrm);
  hwndButton := CreateWindow(
  button
  Click Here
  WS_CHILD or WS_VISIBLE or BS_PUSHBUTTON
   cx cy
  hWindow
  HMENU()
  pctst^hInstance
  nil
  );
  Result:=;
  Exit;
  end;
  
  WM_DESTROY:
  begin
  PostQuitMessage();
  Result:=;
  Exit;
  end;
  
  WM_PAINT:
  begin
  dc := BeginPaint (hWindow ps);
  GetClientRect (hWindow rc);
  
  rcbottom := Round(rcbottom / );
  DrawText (dc Hello World! rc
  DT_SINGLELINE or DT_CENTER or DT_VCENTER);
  
  EndPaint (hWindow ps);
  Result:= ;
  Exit;
  end;
  
  WM_SIZE:
  begin
  if (hwndButton<>) and (wPrm = SIZEFULLSCREEN) or (wPrm = SIZENORMAL) then
  begin
  rcleft := Round((LOWORD(lPrm) cx) / );
  rctop := Round(HIWORD(lPrm) * / cy / );
  MoveWindow (hwndButtonrcleft rctop cx cy True);
  end;
  Result:= ;
  Exit;
  end;
  
  WM_COMMAND:
  begin
  if (LOWORD(wPrm) = ) and (HIWORD(wPrm) = BN_CLICKED) and
  (HWND(lPrm) = hwndButton) then
  begin
  DestroyWindow (hWindow);
  end;
  Result:= ;
  Exit;
  end;
  
  end;
  
  Result:=DefWindowProc (hWindow nMsg wPrm lPrm);
  
  end;
  
  //winmain
  var
  hWindowMain:HWND;
  MyMsg:MSG;
  wcex:WNDCLASSEX;
  begin
  wcexcbSize := SizeOf(WNDCLASSEX);
  wcexstyle := CS_HREDRAW or CS_VREDRAW;
  wcexlpfnWndProc := @MainWndProc;
  wcexcbClsExtra := ;
  wcexcbWndExtra := ;
  wcexhInstance := MainInstance;
  wcexhIcon := LoadIcon ( IDI_APPLICATION);
  wcexhCursor := LoadCursor ( IDC_ARROW);
  wcexhbrBackground := HBRUSH(COLOR_WINDOW+);
  wcexlpszClassName := WinTestWin;
  wcexhIconSm := LoadIcon ( IDI_APPLICATION);
  
  RegisterClassEx (wcex);
  
  hWindowMain := CreateWindow (
  WinTestWin
  Hello
  WS_OVERLAPPEDWINDOW
  CW_USEDEFAULT
  CW_USEDEFAULT
  CW_USEDEFAULT
  CW_USEDEFAULT
  
  
  MainInstance
  nil
  );
  
  ShowWindow (hWindowMain CmdShow);
  UpdateWindow (hWindowMain);
  
  while GetMessage (MyMsg )=True do
  begin
  TranslateMessage (MyMsg);
  DispatchMessage (MyMsg);
  end;
  
  end
  
  最後發現Delphi生成的代碼僅有k比VC小了一半以上說明Delphi有一個不錯的編譯器加之VCL的可視化功能應該是一個很好的開發工具 以我來看Delphi開發大型系統是一點問題沒有的甚至可能性能將超過VC只是VCL的高封裝層次使得使用VCL的程序通常都大但開發效率卻不是VC可以比的
From:http://tw.wingwit.com/Article/program/Delphi/201311/8414.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.