天天看點

[Win32]系列1

1.入口函數WinMain

  1. #include <windows.h> 
  2. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,  
  3.     LPSTR lpCmdLine, int nCmdShow) 
  4.     MessageBox(NULL, TEXT("一個最簡單的Win32程式!"), TEXT("Win32程式"), MB_OK); 
  5.     return 0; 

WINAPI,APIENTRY,PASCAL等價的,

#define WINAPI __stdcall

調用約定,壓棧方式等。

2.建立視窗

  1. #include <windows.h> 
  2. LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); 
  3. int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd) 
  4.     WNDCLASSEX wClass; 
  5.     ZeroMemory(&wClass,sizeof(WNDCLASSEX)); 
  6.     wClass.cbClsExtra=NULL; 
  7.     wClass.cbSize=sizeof(WNDCLASSEX); 
  8.     wClass.cbWndExtra=NULL; 
  9.     wClass.hbrBackground=(HBRUSH)COLOR_WINDOW; 
  10.     wClass.hCursor=LoadCursor(NULL,IDC_ARROW); 
  11.     wClass.hIcon=NULL; 
  12.     wClass.hIconSm=NULL; 
  13.     wClass.hInstance=hInst; 
  14.     wClass.lpfnWndProc=(WNDPROC)WinProc; 
  15.     wClass.lpszClassName=TEXT("視窗類名"); 
  16.     wClass.lpszMenuName=NULL; 
  17.     wClass.style=CS_HREDRAW|CS_VREDRAW; 
  18.     if(!RegisterClassEx(&wClass)) 
  19.     { 
  20.         int nResult=GetLastError(); 
  21.         MessageBox(NULL, 
  22.             TEXT("注冊視窗類失敗."), 
  23.             TEXT("失敗"), 
  24.             MB_ICONERROR); 
  25.     } 
  26.     HWND hWnd=CreateWindowEx(NULL, 
  27.             TEXT("視窗類名"), 
  28.             TEXT("win32應用程式"), 
  29.             WS_OVERLAPPEDWINDOW, 
  30.             200, 
  31.             200, 
  32.             640, 
  33.             480, 
  34.             NULL, 
  35.             NULL, 
  36.             hInst, 
  37.             NULL); 
  38.     if(!hWnd) 
  39.     { 
  40.         int nResult=GetLastError(); 
  41.         MessageBox(NULL, 
  42.             TEXT("視窗建立失敗."), 
  43.             TEXT("失敗"), 
  44.             MB_ICONERROR); 
  45.     } 
  46.     ShowWindow(hWnd,nShowCmd); 
  47.     MSG msg; 
  48.     ZeroMemory(&msg,sizeof(MSG));/*清零*/ 
  49.     while(GetMessage(&msg,NULL,0,0)) 
  50.     { 
  51.         TranslateMessage(&msg); 
  52.         DispatchMessage(&msg); 
  53.     } 
  54.     return 0; 
  55. LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) 
  56.     switch(msg) 
  57.     { 
  58.         case WM_DESTROY: 
  59.         { 
  60.             PostQuitMessage(0); 
  61.             return 0; 
  62.         } 
  63.         break; 
  64.     } 
  65.     return DefWindowProc(hWnd,msg,wParam,lParam); 
  1. 基本套路,可以把這個存個模版。
3.處理其他消息
  1. case WM_LBUTTONDOWN: 
  2.        TCHAR szFileName[MAX_PATH]; 
  3.        HINSTANCE hInstance = GetModuleHandle(NULL); 
  4.        GetModuleFileName(hInstance, szFileName, MAX_PATH); 
  5.        MessageBox(hWnd, szFileName, TEXT("此程式是:"), MB_OK | MB_ICONINFORMATION);  
  6.        return 0;         
4.資源的使用 首先需要編輯一個資源檔案*.rc,我是用的 http://www.resedit.net/ ResEdit:
[Win32]系列1
第一次使用讓你指定平台的include目錄:
[Win32]系列1
資源的頭檔案是resource.h
  1. #ifndef IDC_STATIC 
  2. #define IDC_STATIC (-1) 
  3. #endif 
  4. #define IDI_MYICON                              101 
  5. #define IDR_MYMENU                              103 
  6. #define IDR_CNMENU                              106 
  7. #define IDM_STUFF_GO                            40000 
  8. #define IDM_____O_1                             40000 
  9. #define IDM__GO_SOMEWHERE_ELSE1                 40001 
  10. #define IDM_____X_1                             40001 
  11. #define IDM_FILE_EXIT                           40002 
  12. #define IDM____G_1                              40002 
  13. #define IDM_STUFF_M01                           40003 
  14. #define IDM_______W_1                           40003 
  15. #define IDM_FILE_OPEN                           40004 
  16. #define IDM_____T_1                             40004 
資源App.rc:  
  1. // Generated by ResEdit 1.5.6 
  2. // Copyright (C) 2006-2010 
  3. // http://www.resedit.net 
  4. #include <windows.h> 
  5. #include <commctrl.h> 
  6. #include <richedit.h> 
  7. #include "resource.h" 
  8. // 
  9. // Menu resources 
  10. // 
  11. LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL 
  12. IDR_CNMENU MENU 
  13.     POPUP "檔案(&F)" 
  14.     { 
  15.         MENUITEM "打開(&O)", IDM_____O_1 
  16.         MENUITEM "退出(&X)", IDM_____X_1 
  17.     } 
  18.     POPUP "材料(&S)" 
  19.     { 
  20.         MENUITEM "去(&G)", IDM____G_1 
  21.         MENUITEM "轉到别處(&W)", IDM_______W_1 
  22.         MENUITEM "測試(&T)", IDM_____T_1 
  23.     } 
  24. LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL 
  25. IDR_MYMENU MENUEX 
  26.     POPUP "&File", 0, 0, 0 
  27.     { 
  28.         MENUITEM "&Open", IDM_FILE_OPEN, 0, 0 
  29.         MENUITEM "E&xit", IDM_FILE_EXIT, 0, 0 
  30.     } 
  31.     POPUP "&Stuff", 0, 0, 0 
  32.     { 
  33.         MENUITEM "&Go", IDM_STUFF_GO, 0, 0 
  34.         MENUITEM "G&o Somewhere else", IDM__GO_SOMEWHERE_ELSE1, 0, 0 
  35.         MENUITEM "M01", IDM_STUFF_M01, 0, 0 
  36.     } 
  37. // 
  38. // Icon resources 
  39. // 
  40. LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL 
  41. IDI_MYICON         ICON           "icon1.ico" 
這樣就可以在建立視窗類時,為視窗指定一個預設的菜單:  
  1. wClass.lpszMenuName=MAKEINTRESOURCE(IDR_MYMENU); 
當然可以建立視窗時,不用視窗類的:
  1. HMENU hMenu=LoadMenu(hInst,MAKEINTRESOURCE(IDR_CNMENU)); 
  2. HWND hWnd=CreateWindowEx(NULL, 
  3.         TEXT("視窗類名"), 
  4.         TEXT("win32應用程式"), 
  5.         WS_OVERLAPPEDWINDOW, 
  6.         200, 
  7.         200, 
  8.         640, 
  9.         480, 
  10.         NULL, 
  11.         hMenu,/*菜單*/ 
  12.         hInst, 
  13.         NULL); 

繼續閱讀