windows创建一个窗口
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
HWND HMainwnd = NULL; //窗体句柄
int run(); //消息循环函数
bool InitWindowsApp(HINSTANCE instanceHandle, int show); //窗体初始化函数
//窗体处->理回调函数
LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp);
//main函数
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
if(!InitWindowsApp(hInstance, nShowCmd))//判断窗体初始化是否成功
{
return 0;
}
run(); //初始化成功后循环接收消息
while(1)
{
int index =1;
}
return 0;
}
bool InitWindowsApp(HINSTANCE stanceHandle, int show)
{
WNDCLASS wc; //窗体类结构体
wc.style = CS_HREDRAW | CS_VREDRAW; //窗体风格
wc.lpfnWndProc = WndProc; //窗体处理函数
wc.cbClsExtra = 0; //窗体类是否由扩展
wc.cbWndExtra = 0; //窗体实例是否由扩展
wc.hInstance = stanceHandle; //窗体句柄
wc.hIcon = LoadIcon(0, IDI_APPLICATION); //窗体图标
wc.hCursor = LoadCursor(NULL, IDC_ARROW); //窗体鼠标样式
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //窗体背景颜色
wc.lpszMenuName = NULL; //窗体菜单
wc.lpszClassName = _T("BaseWndClass"); //窗体类名
if(!RegisterClass(&wc)) //判断窗体是否注册成功
{
MessageBox(0, _T("注册失败"), 0, 0);
return false;
}
//创建窗体
HMainwnd = CreateWindow(_T("BaseWndClass"), //窗体类名
_T("MyFrom"), //窗体标题名
WS_OVERLAPPEDWINDOW, //窗体风格
CW_USEDEFAULT,
CW_USEDEFAULT, //窗体坐标
CW_USEDEFAULT,
CW_USEDEFAULT, //窗体坐标
NULL, //窗体的父窗体
NULL, //窗体的子菜单
stanceHandle, //窗体句柄
NULL); // 如果函数成功,返回值为新窗口的句柄:如果函数失败,返回值为NULL
if(HMainwnd == 0)//判断创建窗体是否成功
{
MessageBox(0,_T( "创建窗体失败"), 0, 0);
return false;
}
ShowWindow(HMainwnd, SW_SHOW); //显示窗体
UpdateWindow(HMainwnd); //绘制窗体;
return true;
}
LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch(msg)//判断消息的种类
{
case WM_LBUTTONDOWN: //左键单击
MessageBox(0, _T("Hello World"), _T("myMbox"), MB_OK);
return 0;
case WM_KEYDOWN: //键盘消息
if(wp == VK_ESCAPE) //ESC键
DestroyWindow(HMainwnd); //销毁窗体
return 0;
case WM_DESTROY:
PostQuitMessage(0); //终止消息
return 0;
}
return DefWindowProc(wnd, msg, wp, lp); //返回缺省消息函数
}
int run()
{
MSG msg = {0}; //消息结构
BOOL bRet = 1; //获取消息
while( (bRet = GetMessage(&msg, 0, 0, 0)) != 0)//消息循环
{
printf("消息循环");
int *p=new int;
if(bRet == -1)//判断消息是否接收失败
{
MessageBox(0, _T("接受消息失败"), 0, 0);
break;
}
else
{
TranslateMessage(&msg); //转换消息为字符消息
DispatchMessage(&msg); //发送消息给窗口
}
}
return (int)msg.wParam;
}
HINSTANCE instanceHandle;
void fun()
{
InitWindowsApp(instanceHandle,1);
run();
}
#include <windows.h>
#include <tchar.h>
HWND HMainwnd = NULL; //窗体句柄
int run(); //消息循环函数
bool InitWindowsApp(HINSTANCE instanceHandle, int show); //窗体初始化函数
//窗体处->理回调函数
LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp);
//main函数
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
if(!InitWindowsApp(hInstance, nShowCmd))//判断窗体初始化是否成功
{
return 0;
}
run(); //初始化成功后循环接收消息
while(1)
{
int index =1;
}
return 0;
}
bool InitWindowsApp(HINSTANCE stanceHandle, int show)
{
WNDCLASS wc; //窗体类结构体
wc.style = CS_HREDRAW | CS_VREDRAW; //窗体风格
wc.lpfnWndProc = WndProc; //窗体处理函数
wc.cbClsExtra = 0; //窗体类是否由扩展
wc.cbWndExtra = 0; //窗体实例是否由扩展
wc.hInstance = stanceHandle; //窗体句柄
wc.hIcon = LoadIcon(0, IDI_APPLICATION); //窗体图标
wc.hCursor = LoadCursor(NULL, IDC_ARROW); //窗体鼠标样式
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //窗体背景颜色
wc.lpszMenuName = NULL; //窗体菜单
wc.lpszClassName = _T("BaseWndClass"); //窗体类名
if(!RegisterClass(&wc)) //判断窗体是否注册成功
{
MessageBox(0, _T("注册失败"), 0, 0);
return false;
}
//创建窗体
HMainwnd = CreateWindow(_T("BaseWndClass"), //窗体类名
_T("MyFrom"), //窗体标题名
WS_OVERLAPPEDWINDOW, //窗体风格
CW_USEDEFAULT,
CW_USEDEFAULT, //窗体坐标
CW_USEDEFAULT,
CW_USEDEFAULT, //窗体坐标
NULL, //窗体的父窗体
NULL, //窗体的子菜单
stanceHandle, //窗体句柄
NULL); // 如果函数成功,返回值为新窗口的句柄:如果函数失败,返回值为NULL
if(HMainwnd == 0)//判断创建窗体是否成功
{
MessageBox(0,_T( "创建窗体失败"), 0, 0);
return false;
}
ShowWindow(HMainwnd, SW_SHOW); //显示窗体
UpdateWindow(HMainwnd); //绘制窗体;
return true;
}
LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch(msg)//判断消息的种类
{
case WM_LBUTTONDOWN: //左键单击
MessageBox(0, _T("Hello World"), _T("myMbox"), MB_OK);
return 0;
case WM_KEYDOWN: //键盘消息
if(wp == VK_ESCAPE) //ESC键
DestroyWindow(HMainwnd); //销毁窗体
return 0;
case WM_DESTROY:
PostQuitMessage(0); //终止消息
return 0;
}
return DefWindowProc(wnd, msg, wp, lp); //返回缺省消息函数
}
int run()
{
MSG msg = {0}; //消息结构
BOOL bRet = 1; //获取消息
while( (bRet = GetMessage(&msg, 0, 0, 0)) != 0)//消息循环
{
printf("消息循环");
int *p=new int;
if(bRet == -1)//判断消息是否接收失败
{
MessageBox(0, _T("接受消息失败"), 0, 0);
break;
}
else
{
TranslateMessage(&msg); //转换消息为字符消息
DispatchMessage(&msg); //发送消息给窗口
}
}
return (int)msg.wParam;
}
HINSTANCE instanceHandle;
void fun()
{
InitWindowsApp(instanceHandle,1);
run();
}