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);
加载了中文的菜单,而不用默认的英文菜单。