比较VC和Delphi的WinTest工程

2016-02-19 18:26 2 1 收藏

今天天气好晴朗处处好风光,好天气好开始,图老师又来和大家分享啦。下面给大家推荐比较VC和Delphi的WinTest工程,希望大家看完后也有个好心情,快快行动吧!

【 tulaoshi.com - 编程语言 】

 

  看过几篇关于VC和Delphi比较的文章,自己也有心写写代码试试,我在VC6下新建了一个工程,叫WinTest。代码如下:
  
  #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:
    {
     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_SINGLELINE | 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 (LOWORD(wPrm) == 1 && HIWORD(wPrm) == BN_CLICKED &&
      (HWND) lPrm == hwndButton)
     {
      DestroyWindow (hWindow);
     }
     return 0;
     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;

   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);

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  
   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))
   {
    TranslateMessage (&MyMsg);
    DispatchMessage (&MyMsg);
   }
   return MyMsg.wParam;
  }

  
  其中选择Release方式编译,打开VC6的最小代码优化,编译生成的执行码为36.0KB,
  然后将其翻译成Delphi代码,如下:
  
  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;
      end;

      WM_PAINT:
      begin
     dc := BeginPaint (hWindow, ps);
     GetClientRect (hWindow, rc);

     rc.bottom := Round(rc.bottom / 2);
     DrawText (dc, 'Hello, World!', -1, rc,
     DT_SINGLELINE or DT_CENTER or DT_VCENTER);

     EndPaint (hWindow, ps);
     Result:= 0;
     Exit;
      end;

      WM_SIZE:
      begin
     if (hwndButton0) and (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;
      end;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

      WM_COMMAND:
      begin
     if (LOWORD(wPrm) = 1) and (HIWORD(wPrm) = BN_CLICKED) and
      (HWND(lPrm) = hwndButton) then
     begin
      DestroyWindow (hWindow);
     end;
     Result:= 0;
     Exit;
      end;

    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
   begin
    TranslateMessage (MyMsg);
    DispatchMessage (MyMsg);
   end;
   
  end.
  
  最后发现Delphi生成的代码仅有16.5k,比VC小了一半以上。说明Delphi有一个不错的编译器,加之VCL的可视化功能,应该是一个很好的开发工具。 以我来看,Delphi开发大型系统是一点问题没有的,甚至可能性能将超过VC,只是VCL的高封装层次使得使用VCL的程序通常都大,但开发效率却不是VC可以比的。 Delphi也同样可以不使用VCL写程序,像网上有人将Quake2的源码翻译成了Delphi的,效果与C写的相差无几。


来源:https://www.tulaoshi.com/n/20160219/1617679.html

延伸阅读
BCB及Delphi工程文件扩展名一览 FileProductPurposedefproj.optDelphi 1 onlyDefault project options for the IDEdelphi.dmtDelphi 1 onlyThe file used to store menu templatesdelphi.dskDelphi 1 onlyThe default project desktop filedelphi.hdxDelphi 1 and 2 onlyThe MultiHelp index filedefproj.dofDelphi 2 and laterDefaul...
标签: MySQL mysql数据库
怎样在vc、delphi、vb等程序中使用mysql呢(mysql odbc驱动程序的使用) 我们经常会遇到这样问题,怎样在非web程序或asp程序中使用mysql数据库呢?对于这个问题有两个解决方案: 1.使用mysql提供的api函数库。 很多有名的mysql客户端工具就是这样实现的,大名鼎鼎的winmysql工具就是这样的。这在大部分的开发工具中都可以实现。 比如vc,bcb,...
  关键词: Delphi;Word;Excel;报表;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 1.引言 在利用Delphi开发应用程序的时候,通常我们要设计出很多各种格式的文档、报表,经常要进行文件的创建、编辑及修改,虽然Delphi本身提供了很多设计报表的控件或方法,但是存在明显的不足,比如中文...
标签: Delphi
  0. DISCLAIMER: 1. What is the registry? 2. What does the registry look like? 3. How to read and write data to the registry     3.1 RegCreateKey()     3.2 RegOpenKey()     3.3 RegSetValue()     3.4 RegQueryValue()  &nbs...
转自: http://www.qqread.com/mysql/z442108305.html 对于程序开发人员而言,目前使用最流行的两种后台数据库即为MySQL和SQLServer。这两者最基本的相似之处在于数据存储和属于查询系统。你可以使用sql来访问这两种数据库的数据,因为它们都支持ansi-sql。 还有,这两种数据库系统都支持二进制关键词和关键索引,这就大大地加快了查询速度。...

经验教程

747

收藏

70
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部