#include<windows.h>
#include<stdio.h>
//声明窗口过程函数
LRESULT CALLBACK WinSunProc(
HWND hwnd, //窗口句柄
UINT uMsg, //消息代码
WPARAM wParam, //消息参数
LPARAM lParam
);
//winmain函数
int WINAPI WinMain(
HINSTANCE hInstance, //程序当前运行的实例的句柄,每运行一个程序系统都会分配一个句柄值
HINSTANCE hPreInstance, //当前实例的前一个实例的句柄值,一般为null
LPSTR lpCmdLine, //传递给应用程序的命令参数
int nCmdShow //指定程序的窗口应该如何显示。
)
{
//设计一个窗口类
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance = hInstance; //由winmain函数传递
wndcls.lpfnWndProc = WinSunProc;
wndcls.lpszClassName="bailei";
wndcls.lpszMenuName = NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
//注册窗口类
RegisterClass(&wndcls);
//创建窗口,定义一个变量来保存成功创建窗口后的返回的句柄。
HWND hwnd;
hwnd = CreateWindow("bailei","白磊的第一个应用程序",WS_OVERLAPPEDWINDOW,0,0,400,400,NULL,NULL,hInstance,NULL);
//显示及刷新窗口
ShowWindow(hwnd,SW_SHOWNORMAL); //第二个参数表示窗口显示的状态
UpdateWindow(hwnd);
//定义消息结构体开始消息循环
MSG msg;
while(GetMessage(&msg,NULL,0,0)) //消息的最大值和最小值都设置为0时表示接受所有消息
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam; //返回创建成功的句柄
}
//编写窗口过程函数
LRESULT CALLBACK WinSunProc(
HWND hwnd, //窗口句柄
UINT uMsg, //消息代码
WPARAM wParam, //消息参数
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20]; //定义一个字符串
sprintf(szChar,"char code is %d",wParam);
MessageBox(hwnd,szChar,"char",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","message",0);
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc,0,50,"程序员之家",strlen("程序员之家"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"jame is the best player",strlen("jame is the best player"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES == MessageBox(hwnd,"是否真的要结束","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
#include<stdio.h>
//声明窗口过程函数
LRESULT CALLBACK WinSunProc(
HWND hwnd, //窗口句柄
UINT uMsg, //消息代码
WPARAM wParam, //消息参数
LPARAM lParam
);
//winmain函数
int WINAPI WinMain(
HINSTANCE hInstance, //程序当前运行的实例的句柄,每运行一个程序系统都会分配一个句柄值
HINSTANCE hPreInstance, //当前实例的前一个实例的句柄值,一般为null
LPSTR lpCmdLine, //传递给应用程序的命令参数
int nCmdShow //指定程序的窗口应该如何显示。
)
{
//设计一个窗口类
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance = hInstance; //由winmain函数传递
wndcls.lpfnWndProc = WinSunProc;
wndcls.lpszClassName="bailei";
wndcls.lpszMenuName = NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
//注册窗口类
RegisterClass(&wndcls);
//创建窗口,定义一个变量来保存成功创建窗口后的返回的句柄。
HWND hwnd;
hwnd = CreateWindow("bailei","白磊的第一个应用程序",WS_OVERLAPPEDWINDOW,0,0,400,400,NULL,NULL,hInstance,NULL);
//显示及刷新窗口
ShowWindow(hwnd,SW_SHOWNORMAL); //第二个参数表示窗口显示的状态
UpdateWindow(hwnd);
//定义消息结构体开始消息循环
MSG msg;
while(GetMessage(&msg,NULL,0,0)) //消息的最大值和最小值都设置为0时表示接受所有消息
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam; //返回创建成功的句柄
}
//编写窗口过程函数
LRESULT CALLBACK WinSunProc(
HWND hwnd, //窗口句柄
UINT uMsg, //消息代码
WPARAM wParam, //消息参数
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20]; //定义一个字符串
sprintf(szChar,"char code is %d",wParam);
MessageBox(hwnd,szChar,"char",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","message",0);
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc,0,50,"程序员之家",strlen("程序员之家"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"jame is the best player",strlen("jame is the best player"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES == MessageBox(hwnd,"是否真的要结束","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
创建win32程序的步骤
- 编写winmain函数
- 设计窗口类
- 注册窗口类
- 创建窗口类
- 显示并更新窗口类
- 编写消息循环(while循环)(2-6在winmain函数中完成)
- 编写窗口过程函数(switch case 语句)