標籤:介面 過程 ios 技術分享 article 空間 它的 stream null
1 Windows編程基礎1.1Win32應用程式基底本類型
1) 控制台程式
不須要完好的windows表單,能夠使用DOS表單方式顯示
2) Win32表單程式
包括表單的程式,能夠通過表單與程式進行互動
3) Win32庫程式
提供已有的代碼,供其它程式使用
動態庫(DLL):是在啟動並執行時候能夠載入的。
靜態庫(LIB):是在編譯連結是使用的程式。成為當前程式的一部分。
1.2標頭檔和庫1.2.1標頭檔
主要的標頭檔windows.h包括了windows經常使用的定義等,其它,還包括了一些其它的標頭檔。比方:
1、 windef.h: 定義個中資料類型
2、 winbase.h:定義了kernel的相關函數
3、 wingdi.h:定義了畫圖和檔案
4、 winuser.h:表單及空間
5、 winnt.h:提供了Unicode的支援
1.2.2庫
1、Kernel32.lib :提供進程線程記憶體等等API函數定義
2、User32.lib :表單及介面的API函數
3、Gdi32.lib :提供畫圖、文字等API函數支援
1.1.1執行個體helloworld開發環境VS2010。編譯過程中可能會報錯誤,可能是編碼的問題:須要進行一些配置的設定:項目--屬性---配置屬性-常規:字元集 設定為未設定
/*File : helloworld.cpp *Auth : sjin *Date : 20140519 *Mail : [email protected] */#include <iostream>#include <Windows.h>int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ){MessageBox(NULL,"hello world..","first win32 test",MB_OK);return 0;}
執行結果:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2ppbl8xMzE0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >
1.1編寫第一個表單程式
處理相關的流程例如以下:
1、表單入口函數WinMain
2、表單處理函數
3、注冊表單類型
4、建立表單
5、顯示表單
6、訊息處理
7、程式退出
看以下的代碼:
/*File : helloworld.cpp *Auth : sjin *Date : 20140519 *Mail : [email protected] */#include <iostream>#include <Windows.h>using namespace std;HINSTANCE g_hInst = NULL;//2表單處理函數/*函數功能:該函數是一個應用程式定義的函數。它處理髮送給表單的訊息 *當表單處理訊息訊息時,系統調用該函數 *參數: *hwnd:指向表單的控制代碼。
*uMsg:指定訊息類型。
*wParam:指定其餘的、訊息特定的資訊。該參數的內容與UMsg參數值有關。 *IParam:指定其餘的、訊息特定的資訊。該參數的內容與uMsg參數值有關。
*/LRESULT CALLBACK WndProc(HWND hwnd,/**/UINT nMsg,WPARAM wParam,LPARAM IParam){switch( nMsg ){case WM_DESTROY://表單銷毀的訊息//發送訊息退出函數PostQuitMessage(0);return 0;default:break;}//調用return DefWindowProc(hwnd,nMsg,wParam,IParam);}//3注冊表單類型/* * */BOOL MyRegister( LPSTR pszClassName){WNDCLASS wc = {'\0'};wc.style = CS_VREDRAW | CS_HREDRAW;wc.lpfnWndProc = WndProc;wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.hInstance = g_hInst;wc.hIcon = LoadIcon(g_hInst,MAKEINTRESOURCE(100));;wc.hCursor = NULL;wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE);wc.lpszMenuName = NULL;wc.lpszClassName = pszClassName;ATOM natom = RegisterClass(&wc);if(0 == natom){MessageBox(NULL,"Register Failed","Error!",MB_OK | MB_ICONWARNING);}else{//MessageBox(NULL,"Register Successed","Successed!",MB_OK);}return TRUE;}//4 表單建立HWND myCreateWindow(LPSTR pszClassName){//建立表單HWND hwnd =CreateWindow(pszClassName,"HelloWnd",WS_OVERLAPPEDWINDOW,100,100,300,500,NULL,NULL,g_hInst,NULL);if(0 == hwnd){MessageBox(NULL,"CreateWindow Failed","Error!",MB_OK | MB_ICONWARNING);}else{//MessageBox(NULL,"CreateWindow Successed","Successed!",MB_OK);}return hwnd;}//5 顯示表單void Displaywnd(HWND hwnd){//顯示ShowWindow(hwnd,SW_SHOW);//重新整理UpdateWindow(hwnd);}//6 訊息處理void Message(){MSG msg = {'\0'};//訊息迴圈處理。擷取訊息while(GetMessage(&msg,NULL,0,0)){//派發訊息DispatchMessage(&msg);/*會運行表單處理函數*/}}//1、入口函數int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ){g_hInst = hInstance;//注冊表單類型MyRegister("my first win32");//常見注冊類型的表單HWND hwnd = myCreateWindow("my first win32");//顯示表單Displaywnd(hwnd);//訊息處理Message();return 0;}
資源檔
/*File : helloworld.rc *Auth : sjin *Date : 20140519 *Mail : [email protected] */100 ICON "2222.ico"
圖片例如以下:
走進windows編程的世界-----入門篇