windows编程产生窗口的最基本流程
1.注册窗口 主要填充 窗口类 WNDCLASSEX 注册函数 RegisterClassEx()
2.创建窗口 主要函数 CreateWindowEx()
3.显示窗口 ShowWindow()
4.更新窗口 UpdateWindow(hwnd)
5.消息循环
只需要windows.h函数库 是最底层的窗口实现方法 结构简单 利于新手学习 但是代码量大 不适用于项目
简单窗口例子
#include<windows.h>
//消息处理函数声明
LRESULT CALLBACK MainWndProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
//主函数
INT WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcx;
HWND hwnd;
MSG msg;
BOOL fGotMessage;
HWND hwndCap = NULL;
// 注册窗口类,并创建窗口,用于显示截取的位图
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = MainWndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hinstance;
wcx.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcx.lpszMenuName = NULL;
wcx.lpszClassName = "MainWClass";
wcx.hIconSm = NULL;
if( !RegisterClassEx(&wcx))
return 1;
// 创建窗口
hwnd = CreateWindow(
"MainWClass",
"TEST",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS |
WS_MAXIMIZE | WS_POPUPWINDOW,
100,
100,
200,
200,
(HWND) NULL, (HMENU) NULL, hinstance, (LPVOID) NULL);
if (!hwnd)
return 1;
//显示窗口
ShowWindow(hwnd, nCmdShow);
//更新窗口
UpdateWindow(hwnd);
//消息循环
while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
UNREFERENCED_PARAMETER(lpCmdLine);
}
LRESULT CALLBACK MainWndProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
break;
case WM_DESTROY:
ExitProcess(0);
break;
default:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
建议新手看<<windows API>> 接触一下系统各个方面的编程
再去学习MFC 因为MFC结构不像main()函数那样清晰 不容易上手