C++ windows窗体的创建过程
2014-10-12 21:46
519 查看
关于windows窗体的创建过程,记录一下,加深印象!
#include<Windows.h>
#include<stdio.h>
LRESULT CALLBACK WindowSunProc(
HWND hwnd, //handle to window
UINT uMsg, //message identifier
WPARAM wParam, //first message parameter
LPARAM lParam //second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, //handle to current instance
HINSTANCE hPrevInstance,//handle to previous instance
LPSTR lpCmdLine, //command line
int nCmdShow //show state
)
{
//初始化,初始化包括窗口类的定义、注册、创建窗口实例和显示窗口四部分
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[] = "窗口"; //窗口类名
char lpszTitle[] = "测试窗口"; //窗口标题名
//窗口类定义,窗口类定义了窗口的形式与功能,窗口类定义通过给窗口类数据结构WNDCLASS赋值完成
//该数据结构中包含窗口类的各种属性
wndclass.style = 0; // 窗口类型为缺省类型CS_ Class Style
wndclass.lpfnWndProc = WindowSunProc; //定义窗口处理函数
wndclass.cbClsExtra = 0; //窗口类无扩展
wndclass.cbWndExtra = 0; //窗口实例无扩展
wndclass.hInstance = hInstance; //当前实例句柄
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //窗口的最小化图标为缺省图标
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); // 窗口采用箭头光标
wndclass.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH)); //窗口背景为白色
wndclass.lpszMenuName = NULL; //窗口无菜单
wndclass.lpszClassName = (LPCWCHAR)lpszClassName; //窗口类名为“窗口”
//以下是窗口类的注册-----------Windows系统本身提供部分预定义的窗口类,程序员也可以自定义窗口类,窗口类必须先注册后使用。
if (!RegisterClass(&wndclass))
{
MessageBeep(0);
return -1;
}
char *windowClassName = "窗口类名";
char *windowsApplicationName = "窗口标题";
hwnd = CreateWindow((LPCWSTR)windowClassName,
(LPCWSTR)windowsApplicationName,
WS_OVERLAPPEDWINDOW,
100,
50,
400,
250,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
while (GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WindowSunProc(
HWND hwnd, //handle to window
UINT uMsg, //message identifier
WPARAM wParam, //first message parameter
LPARAM lParam //second message parameter
)
{
switch (uMsg)
{
case WM_CHAR: //在窗口中按下一个字符键,将弹出相应的信息
char szChar[20];
sprintf_s(szChar, "char code is %d", wParam);//将字符消息放入szChar中
MessageBox(hwnd, (LPCWSTR)szChar, (LPCWSTR)"char", 0);
break;
case WM_LBUTTONDOWN: //在窗口中按下鼠标左键时,弹出提示信息并输出文字
MessageBox(hwnd, (LPCWSTR)"mouse clicked", (LPCWSTR)"message", 0);
HDC hdc;
hdc = GetDC(hwnd);//不能在响应WM_PAINT消息时调用
TextOut(hdc, 0, 50, (LPCWSTR)"快乐生活", strlen("快乐生活"));
ReleaseDC(hwnd, hdc);
break;
case WM_PAINT: //重新绘制窗口
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd, &ps);//BeginPaint只能在响应WM_PAINT消息时调用
TextOut(hDC, 0, 0, (LPCWSTR)"只要努力就会看到阳光", strlen("只要努力就会看到阳光"));
EndPaint(hwnd, &ps);
break;
case WM_CLOSE: //关闭窗口时弹出消息框
if (IDYES == MessageBox(hwnd, (LPCWSTR)"是否真的结束?", (LPCWSTR)"message", MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
#include<Windows.h>
#include<stdio.h>
LRESULT CALLBACK WindowSunProc(
HWND hwnd, //handle to window
UINT uMsg, //message identifier
WPARAM wParam, //first message parameter
LPARAM lParam //second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, //handle to current instance
HINSTANCE hPrevInstance,//handle to previous instance
LPSTR lpCmdLine, //command line
int nCmdShow //show state
)
{
//初始化,初始化包括窗口类的定义、注册、创建窗口实例和显示窗口四部分
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[] = "窗口"; //窗口类名
char lpszTitle[] = "测试窗口"; //窗口标题名
//窗口类定义,窗口类定义了窗口的形式与功能,窗口类定义通过给窗口类数据结构WNDCLASS赋值完成
//该数据结构中包含窗口类的各种属性
wndclass.style = 0; // 窗口类型为缺省类型CS_ Class Style
wndclass.lpfnWndProc = WindowSunProc; //定义窗口处理函数
wndclass.cbClsExtra = 0; //窗口类无扩展
wndclass.cbWndExtra = 0; //窗口实例无扩展
wndclass.hInstance = hInstance; //当前实例句柄
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //窗口的最小化图标为缺省图标
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); // 窗口采用箭头光标
wndclass.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH)); //窗口背景为白色
wndclass.lpszMenuName = NULL; //窗口无菜单
wndclass.lpszClassName = (LPCWCHAR)lpszClassName; //窗口类名为“窗口”
//以下是窗口类的注册-----------Windows系统本身提供部分预定义的窗口类,程序员也可以自定义窗口类,窗口类必须先注册后使用。
if (!RegisterClass(&wndclass))
{
MessageBeep(0);
return -1;
}
char *windowClassName = "窗口类名";
char *windowsApplicationName = "窗口标题";
hwnd = CreateWindow((LPCWSTR)windowClassName,
(LPCWSTR)windowsApplicationName,
WS_OVERLAPPEDWINDOW,
100,
50,
400,
250,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
while (GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WindowSunProc(
HWND hwnd, //handle to window
UINT uMsg, //message identifier
WPARAM wParam, //first message parameter
LPARAM lParam //second message parameter
)
{
switch (uMsg)
{
case WM_CHAR: //在窗口中按下一个字符键,将弹出相应的信息
char szChar[20];
sprintf_s(szChar, "char code is %d", wParam);//将字符消息放入szChar中
MessageBox(hwnd, (LPCWSTR)szChar, (LPCWSTR)"char", 0);
break;
case WM_LBUTTONDOWN: //在窗口中按下鼠标左键时,弹出提示信息并输出文字
MessageBox(hwnd, (LPCWSTR)"mouse clicked", (LPCWSTR)"message", 0);
HDC hdc;
hdc = GetDC(hwnd);//不能在响应WM_PAINT消息时调用
TextOut(hdc, 0, 50, (LPCWSTR)"快乐生活", strlen("快乐生活"));
ReleaseDC(hwnd, hdc);
break;
case WM_PAINT: //重新绘制窗口
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd, &ps);//BeginPaint只能在响应WM_PAINT消息时调用
TextOut(hDC, 0, 0, (LPCWSTR)"只要努力就会看到阳光", strlen("只要努力就会看到阳光"));
EndPaint(hwnd, &ps);
break;
case WM_CLOSE: //关闭窗口时弹出消息框
if (IDYES == MessageBox(hwnd, (LPCWSTR)"是否真的结束?", (LPCWSTR)"message", MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
相关文章推荐
- 使用API创建窗体(类似VC的创建过程)
- Delphi - 窗体创建过程
- 窗体的创建过程
- 窗体创建的过程
- 带有按钮并且可以执行单击事件的WINFORM窗体,体悟C#的创建过程
- Qt创建窗体的过程
- QT源码解析(七)Qt创建窗体的过程
- QT源码解析(七)Qt创建窗体的过程
- Delphi窗体创建释放过程及单元文件小结
- QT源码解析(七)Qt创建窗体的过程,作者“ tingsking18 ”(真正的创建QPushButton是在show()方法中,show()方法又调用了setVisible方法)
- Delphi窗体创建释放过程及单元文件小结(转)
- 子窗体创建过程
- 第一课.创建窗体类及窗体,窗体过程函数
- Qt创建窗体的过程
- 第一课.创建窗体类及窗体,窗体过程函数
- Android应用程序窗体View的创建过程
- 【Demo 0021】窗体创建过程
- QT源码解析(七)Qt创建窗体的过程
- MFC的windows窗体创建过程
- Qt简单窗体程序全界面创建过程