最簡Windows編程

來源:互聯網
上載者:User

#include <stdio.h>   int main()
{       printfss("Hello world\n");       return 0;   }

無數人知道這段代碼,而知道下面的代碼的人數比上面的要稍少了一些.

#include <windows.h>

int main()

{

MessageBox(NULL,"Hello World","window",MB_OK);

return 0;

}

這兩段代碼運行後都會顯示dos視窗,下面的代碼將把你真正帶入windows環境,就沒有dos視窗什麼事了。

#include <windows.h>

int WINAPI WinMain(HINSTANCE hins,HINSTANCE preHins,LPSTR cmd,int show)

{

MessageBox(NULL,"Hello World","window",MB_OK);

return 0;
}
 

這樣,你就編寫了一個最簡單的windows程式,但只有一個訊息框,還沒有真正意義上的視窗。

#include <windows.h>

//訊息處理函數
 LRESULT CALLBACK WinPorc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(HINSTANCE hins,HINSTANCE phins,LPSTR cmd,int show)
 {
 HWND hwnd;
 MSG msg;
 WNDCLASS wnd;

ZeroMemory(&wnd,sizeof(WNDCLASS));
 wnd.hbrBackground = (HBRUSH)::GetStockObject(DKGRAY_BRUSH);
 wnd.hInstance = hins;
 wnd.lpfnWndProc = WinPorc;
 wnd.lpszClassName="test";
 wnd.style = CS_VREDRAW|CS_HREDRAW;

if(!::RegisterClass(&wnd))
 {
  return 0;
 }

hwnd = ::CreateWindow("test","test",WS_OVERLAPPED|WS_SYSMENU,0,0,100,100,NULL,NULL,hins,NULL);
 if(hwnd==NULL)
 {
  return 0;
 }
 
 ShowWindow(hwnd,show);
 UpdateWindow(hwnd);
 
 while(TRUE){
  if(::PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
   if(msg.message == WM_QUIT){
    break;
   }  
  ::TranslateMessage(&msg);
  ::DispatchMessage(&msg);
  }
 }
    return 0;
}

LRESULT CALLBACK WinPorc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
 switch(msg){
   case WM_DESTROY:
       PostQuitMessage(0);
    break;
   default:
    break;
 }
 return ::DefWindowProc(hwnd,msg,wParam,lParam);
}

相關文章

聯繫我們

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