Windows 編程[6] – 學習表單產生的過程六: 最終的代碼!

來源:互聯網
上載者:User
program Project1;uses  Windows, Messages;{回呼函數; 其中要處理的訊息很多, 最好用 case 語句}function WndProc(wnd: HWND; msg: UINT; wParam: Integer; lParam: Integer): Integer; stdcall;begin  Result := 0;  case msg of    WM_DESTROY: PostQuitMessage(0); {收到 WM_DESTROY 後, 發送 WM_QUIT 訊息指示退出}  else    {其他訊息交 DefWindowProc 處理; DefWindowProc 會返回回呼函數需要的傳回值}    Result := DefWindowProc(wnd, msg, wParam, lParam);  end;end;{登記與註冊視窗類別的函數}function RegMyWndClass: Boolean;var  MyWndClass: TWndClass;begin  {指定視窗類別型}  MyWndClass.style         := CS_HREDRAW or CS_VREDRAW;  {改變大小時重繪}  MyWndClass.lpfnWndProc   := @WndProc;                  {回呼函數指標}  MyWndClass.cbClsExtra    := 0;                         {沒有額外的視窗類別資訊}  MyWndClass.cbWndExtra    := 0;                         {沒有額外的視窗資訊}  MyWndClass.hInstance     := HInstance;                 {程式執行個體控制代碼}  MyWndClass.hIcon         := 0;                         {沒指定表徵圖}  MyWndClass.hCursor       := LoadCursor(0, IDC_ARROW);  {選用了系統提供的指標}  MyWndClass.hbrBackground := HBRUSH(COLOR_BTNFACE + 1); {背景色使用 Windows 預設的按鈕顏色}  MyWndClass.lpszMenuName  := nil;                       {不指定預設菜單}  MyWndClass.lpszClassName := 'MyWindowClass';           {給視窗類別型命名}  Result := RegisterClass(MyWndClass)  0;              {註冊視窗類別型}end;{主程式}var  hWnd: THandle;  Msg : TMsg;begin  {調用登記與註冊視窗的函數}  if not RegMyWndClass then  begin    MessageBox(0, '視窗類別註冊失敗!', '提示', MB_OK + MB_ICONERROR);    Exit;  end;  {建立視窗並返回控制代碼; 既然有 CreateWindowEx 就不使用 CreateWindow 了}  hWnd := CreateWindowEx(0,                      {不使用擴充風格}                         'MyWindowClass',        {視窗類別型名}                         '新視窗',               {標題}                         WS_OVERLAPPEDWINDOW,    {視窗的常規樣式}                         Integer(CW_USEDEFAULT), {預設水平位置}                         Integer(CW_USEDEFAULT), {預設垂直位置}                         Integer(CW_USEDEFAULT), {預設寬度}                         Integer(CW_USEDEFAULT), {預設高度}                         0,                      {無父視窗}                         0,                      {無主菜單}                         HInstance,              {執行個體控制代碼}                         nil                     {無附加資訊}                         );  if hWnd = 0 then {如果視窗建立失敗}  begin    MessageBox(0, '視窗建立失敗!', '提示', MB_OK + MB_ICONERROR);    Exit;  end;  {顯示視窗與更新視窗}  ShowWindow(hWnd, SW_SHOWNORMAL);  UpdateWindow(hWnd);  {訊息迴圈; GetMessage 在收到 WM_QUIT 訊息時會返回 False, 從而終止迴圈}  while(GetMessage(Msg, 0, 0, 0)) do  begin    TranslateMessage(Msg); {需要對部分鍵盤訊息的再處理}    DispatchMessage(Msg);  {將訊息發送給回呼函數}  end;  Halt(Msg.wParam); {沒有了訊息迴圈程式自然會退出; 加上這句, 程式會根據退出碼主動退出}end.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.