1.入口函數WinMain
- #include <windows.h>
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow)
- {
- MessageBox(NULL, TEXT("一個最簡單的Win32程式!"), TEXT("Win32程式"), MB_OK);
- return 0;
- }
WINAPI,APIENTRY,PASCAL等價的,
#define WINAPI __stdcall
調用約定,壓棧方式等。
2.建立視窗
- #include <windows.h>
- LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
- int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
- {
- WNDCLASSEX wClass;
- ZeroMemory(&wClass,sizeof(WNDCLASSEX));
- wClass.cbClsExtra=NULL;
- wClass.cbSize=sizeof(WNDCLASSEX);
- wClass.cbWndExtra=NULL;
- wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
- wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
- wClass.hIcon=NULL;
- wClass.hIconSm=NULL;
- wClass.hInstance=hInst;
- wClass.lpfnWndProc=(WNDPROC)WinProc;
- wClass.lpszClassName=TEXT("視窗類名");
- wClass.lpszMenuName=NULL;
- wClass.style=CS_HREDRAW|CS_VREDRAW;
- if(!RegisterClassEx(&wClass))
- {
- int nResult=GetLastError();
- MessageBox(NULL,
- TEXT("注冊視窗類失敗."),
- TEXT("失敗"),
- MB_ICONERROR);
- }
- HWND hWnd=CreateWindowEx(NULL,
- TEXT("視窗類名"),
- TEXT("win32應用程式"),
- WS_OVERLAPPEDWINDOW,
- 200,
- 200,
- 640,
- 480,
- NULL,
- NULL,
- hInst,
- NULL);
- if(!hWnd)
- {
- int nResult=GetLastError();
- MessageBox(NULL,
- TEXT("視窗建立失敗."),
- TEXT("失敗"),
- MB_ICONERROR);
- }
- ShowWindow(hWnd,nShowCmd);
- MSG msg;
- ZeroMemory(&msg,sizeof(MSG));/*清零*/
- while(GetMessage(&msg,NULL,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
- {
- switch(msg)
- {
- case WM_DESTROY:
- {
- PostQuitMessage(0);
- return 0;
- }
- break;
- }
- return DefWindowProc(hWnd,msg,wParam,lParam);
- }
- 基本套路,可以把這個存個模版。
3.處理其他消息
- case WM_LBUTTONDOWN:
- {
- TCHAR szFileName[MAX_PATH];
- HINSTANCE hInstance = GetModuleHandle(NULL);
- GetModuleFileName(hInstance, szFileName, MAX_PATH);
- MessageBox(hWnd, szFileName, TEXT("此程式是:"), MB_OK | MB_ICONINFORMATION);
- return 0;
- }
4.資源的使用
首先需要編輯一個資源檔案*.rc,我是用的
http://www.resedit.net/
ResEdit:
第一次使用讓你指定平台的include目錄:
資源的頭檔案是resource.h
- #ifndef IDC_STATIC
- #define IDC_STATIC (-1)
- #endif
- #define IDI_MYICON 101
- #define IDR_MYMENU 103
- #define IDR_CNMENU 106
- #define IDM_STUFF_GO 40000
- #define IDM_____O_1 40000
- #define IDM__GO_SOMEWHERE_ELSE1 40001
- #define IDM_____X_1 40001
- #define IDM_FILE_EXIT 40002
- #define IDM____G_1 40002
- #define IDM_STUFF_M01 40003
- #define IDM_______W_1 40003
- #define IDM_FILE_OPEN 40004
- #define IDM_____T_1 40004
資源App.rc:
- // Generated by ResEdit 1.5.6
- // Copyright (C) 2006-2010
- // http://www.resedit.net
- #include <windows.h>
- #include <commctrl.h>
- #include <richedit.h>
- #include "resource.h"
- //
- // Menu resources
- //
- LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
- IDR_CNMENU MENU
- {
- POPUP "檔案(&F)"
- {
- MENUITEM "打開(&O)", IDM_____O_1
- MENUITEM "退出(&X)", IDM_____X_1
- }
- POPUP "材料(&S)"
- {
- MENUITEM "去(&G)", IDM____G_1
- MENUITEM "轉到别處(&W)", IDM_______W_1
- MENUITEM "測試(&T)", IDM_____T_1
- }
- }
- LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
- IDR_MYMENU MENUEX
- {
- POPUP "&File", 0, 0, 0
- {
- MENUITEM "&Open", IDM_FILE_OPEN, 0, 0
- MENUITEM "E&xit", IDM_FILE_EXIT, 0, 0
- }
- POPUP "&Stuff", 0, 0, 0
- {
- MENUITEM "&Go", IDM_STUFF_GO, 0, 0
- MENUITEM "G&o Somewhere else", IDM__GO_SOMEWHERE_ELSE1, 0, 0
- MENUITEM "M01", IDM_STUFF_M01, 0, 0
- }
- }
- //
- // Icon resources
- //
- LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
- IDI_MYICON ICON "icon1.ico"
這樣就可以在建立視窗類時,為視窗指定一個預設的菜單:
- wClass.lpszMenuName=MAKEINTRESOURCE(IDR_MYMENU);
當然可以建立視窗時,不用視窗類的:
- HMENU hMenu=LoadMenu(hInst,MAKEINTRESOURCE(IDR_CNMENU));
- HWND hWnd=CreateWindowEx(NULL,
- TEXT("視窗類名"),
- TEXT("win32應用程式"),
- WS_OVERLAPPEDWINDOW,
- 200,
- 200,
- 640,
- 480,
- NULL,
- hMenu,/*菜單*/
- hInst,
- NULL);