走進windows編程的世界-----入門篇
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]
執行結果:
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編程的世界-----入門篇