Windows Api 创建窗口

windowsmsg.jpg
 
第一步:设计窗口类
// 创建窗口对象
typedef struct tagWNDCLASS {
   UINT      style;
   WNDPROC   lpfnWndProc; //窗口消息处理函数处理相关操作
   int       cbClsExtra;
   int       cbWndExtra;
   HINSTANCE hInstance; //当前窗口是哪个应用程序的 应用程序的实例句柄
   HICON     hIcon; //图标
   HCURSOR   hCursor; //鼠标形状
  HBRUSH    hbrBackground; //背景色
  LPCTSTR   lpszMenuName; //指定窗口菜单名
  LPCTSTR   lpszClassName; //窗体名
} WNDCLASS, *PWNDCLASS;
 
TCHAR title[] = TEXT("My First Window");
WNDCLASS wndclass = {0};
wndclass.lpszClassName = title; //窗体名
wndclass.hInstance = hInstance;
wndclass.hbrBackground = (HBRUSH)COLOR_MENU; //画刷类型
wndclass.lpfnWndProc = WndProc; //指定处理函数
 
第二步: 注册窗口类
RegisterClass(&wndclass);
 
第三步: 创建窗口
HWND WINAPI CreateWindow(
  _In_opt_  LPCTSTR lpClassName,  //类名
  _In_opt_  LPCTSTR lpWindowName, //窗口标题
  _In_      DWORD dwStyle,        //外观样式
  _In_      int x,                //相对父窗口的X坐标
  _In_      int y,                //相对父窗口的Y坐标
  _In_      int nWidth,           //窗口宽度
  _In_      int nHeight,          //窗口高度
  _In_opt_  HWND hWndParent,      //父窗口句柄
  _In_opt_  HMENU hMenu,         //菜单句柄
  _In_opt_  HINSTANCE hInstance, //当前应用程序句柄
  _In_opt_  LPVOID lpParam       //附加参数一般为NULL
);
 
第三步: 显示窗口
BOOL WINAPI ShowWindow( _In_ HWND hWnd, _In_ int nCmdShow );
 
第四步:消息循环
MSG msg;
BOOL bRet;
while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        TranslateMessage(&msg); //翻译消息 转换消息
        DispatchMessage(&msg);  //分发消息 调用消息处理函数 再次进到0环,拿着窗口句柄分发给窗口对象的消息处理函数
    }
}
 
第五步:窗口回调函数
窗口回调函数不处理的消息,由 DefWindowProc 处理

#include <Windows.h>
#include <stdio.h>
LRESULT CALLBACK WindowProc(
	_In_  HWND hwnd,
	_In_  UINT uMsg,
	_In_  WPARAM wParam,
	_In_  LPARAM lParam
);

int CALLBACK WinMain(
	_In_  HINSTANCE hInstance,
	_In_  HINSTANCE hPrevInstance,
	_In_  LPSTR lpCmdLine,
	_In_  int nCmdShow
) {
	 WNDCLASS wnd = { 0 };
	 TCHAR ClassName[] = TEXT("first window");
	 wnd.cbClsExtra = 0;
	 wnd.cbWndExtra = 0;
	 wnd.hInstance = hInstance;
	 wnd.lpszMenuName = NULL;
	 wnd.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	 wnd.lpfnWndProc = WindowProc;
	 wnd.hIcon = LoadIcon(NULL, IDI_ERROR);
	 wnd.hCursor = LoadCursor(NULL, IDC_SIZENESW);
	 wnd.lpszClassName = ClassName;
	 wnd.style = CS_VREDRAW | CS_HREDRAW;

	 RegisterClass(&wnd);

	 TCHAR WindowName[] = TEXT("Fuck白狼黑客编程学习之路");
	 //HWND CreateWindow(
		// LPCTSTR lpClassName, //指向已注册类名的指针
		// LPCTSTR lpWindowName, //指向窗口名的指针
		// DWORD dwStyle, //窗口样式
		// int x, //窗口水平位置
		// int y, //窗口的垂直位置
		// int nWidth, //窗口宽度
		// int nHeight, //窗口高度
		// HWND hWndParent, //父窗口或所有者窗口的句柄
		// HMENU, //菜单或子窗口标识符的句柄
		// 句柄hInstance, //应用程序实例的句柄
		// LPVOID lpParam //指向wi的指针)

		HWND hwnd =  CreateWindow(ClassName, WindowName, WS_OVERLAPPEDWINDOW, 0, 0, 300, 400, NULL, NULL, hInstance, NULL);

		ShowWindow(hwnd, nCmdShow);
		UpdateWindow(hwnd);

		MSG msg;
		while (GetMessage(&msg,NULL,0,0))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}


	
		return 0;
}

LRESULT CALLBACK WindowProc(
	_In_  HWND hwnd,
	_In_  UINT uMsg,
	_In_  WPARAM wParam,
	_In_  LPARAM lParam
) {
	switch (uMsg)
	{	
		case WM_PAINT:
		{
			return 0;
		}
		case WM_CLOSE:
		{
			return 0;
		}
		case WM_LBUTTONDOWN:
		{
			return 0;
		}
		default:
			return DefWindowProc(hwnd, uMsg, wParam, lParam);
	}
}

QQ20190228-144724@2x.png

本博客所有文章如无特别注明均为原创。作者:odaycaogen复制或转载请以超链接形式注明转自 123``blog
原文地址《Windows Api 创建窗口

相关推荐

发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(0)