MFC_1——採用windows API函數來產生一個視窗顯示helloword

來源:互聯網
上載者:User

標籤:

//採用windows API函數來產生一個視窗顯示helloword:#include <windows.h>    LRESULT CALLBACK myWndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);   //進入WinMain函數int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)      {      const static TCHAR appName[] = TEXT("Hello world"); //初始化WNDCLASSEX,調用RegisterClassEx函數註冊視窗類別WNDCLASSEX myWin;      myWin.cbSize = sizeof(myWin);      myWin.style = CS_HREDRAW | CS_VREDRAW;      myWin.lpfnWndProc = myWndProc;      myWin.cbClsExtra = 0;      myWin.cbWndExtra = 0;      myWin.hInstance = hInstance;      myWin.hIcon = 0;      myWin.hIconSm  = 0;      myWin.hCursor = 0;      myWin.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);      myWin.lpszMenuName = 0;      myWin.lpszClassName = appName;      //Register ,調用RegisterClassEx函數註冊視窗類別     if (!RegisterClassEx(&myWin)) return 0;      const HWND hWindow = CreateWindow(      appName,      appName,      WS_OVERLAPPEDWINDOW,      CW_USEDEFAULT,      CW_USEDEFAULT,      CW_USEDEFAULT,      CW_USEDEFAULT,      0,      0,      hInstance,      0);  //調用ShowWindow和UpdateWindow函數顯示並更新視窗/*進入訊息迴圈。關於訊息迴圈再簡單說下,Windows應用程式是訊息驅動的,系統或使用者讓應用程式進行某項操作或完成某個任務時會發送訊息,進入程式的訊息佇列,然後訊息迴圈會將訊息佇列中的訊息取出,交予相應的視窗過程處理,此程式的視窗過程函數就是myWndProc函數,視窗過程函數處理完訊息就完成了某項操作或任務。本例是要顯示“HELLO WORLD”字串,UpdateWindow函數會發送WM_PAINT訊息,但是此訊息不經過訊息佇列而是直接送到視窗過程處理,在視窗過程函數中最終繪製了“HELLO WORLD”字串。*/ShowWindow(hWindow,iCmdShow);      UpdateWindow(hWindow);      {      MSG msg;      while(GetMessage(&msg,0,0,0))      {      TranslateMessage(&msg);      DispatchMessage(&msg);      }      return (int)msg.wParam;      }      }      LRESULT CALLBACK myWndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)      {      if (msg==WM_PAINT)      {      PAINTSTRUCT ps;      const HDC hDC = BeginPaint(hWindow,&ps);      RECT rect;      GetClientRect(hWindow,&rect);      DrawText(hDC,TEXT("HELLO WORLD"),-1,&rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);      EndPaint(hWindow,&ps);      return 0;      }      else if (msg==WM_DESTROY)      {      PostQuitMessage(0);      return 0;      }      return DefWindowProc(hWindow,msg,wParam,lParam);      }  

  

MFC_1——採用windows API函數來產生一個視窗顯示helloword

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.