win32 消息、事件、创建一个窗口

消息

消息记录了事件,消息就是对事件的描述
win32 消息、事件、创建一个窗口
win32 消息、事件、创建一个窗口
win32 消息、事件、创建一个窗口

创建一个窗口实现代码


#include "StdAfx.h"

#include "pr.h"


LRESULT CALLBACK WindowProc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam);


int APIENTRY WinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow)
{
	DbgPrintf("WM_SIZE %d\n", hInstance);
	//窗口的类名				
	TCHAR className[] = L"My First Window";

	
	WNDCLASS wndclass = {0};
	//当前窗口的背景颜色
	wndclass.hbrBackground = (HBRUSH)COLOR_MENU;
	//消息处理函数:当应用程序有事件触发的时候,由WindowProc函数取处理,
	wndclass.lpfnWndProc = WindowProc;
	//给当前窗口取名
	wndclass.lpszClassName = className;
	//当前程序的句柄
	wndclass.hInstance = hInstance;

	//注册窗口类
	RegisterClass(&wndclass);

	// 创建窗口  							
	HWND hwnd = CreateWindow(
		className,				//类名		
		TEXT("我的第一个窗口"),				//窗口标题		
		WS_OVERLAPPEDWINDOW,				//窗口外观样式 		
		10,				//相对于父窗口的X坐标		
		10,				//相对于父窗口的Y坐标		
		600,			//窗口的宽度  		
		300,			//窗口的高度  		
		NULL,			//父窗口句柄,为NULL  		
		NULL,			//菜单句柄,为NULL  		
		hInstance,		//当前应用程序的句柄  		
		NULL);			//附加数据一般为NULL		

	if (hwnd == NULL)	//是否创建成功  		
		return 0;
	



		// 显示窗口  			
		ShowWindow(hwnd, SW_SHOW);

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

}


LRESULT CALLBACK WindowProc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam)
{
	
	switch (uMsg)
	{
		//窗口消息							
	case WM_CREATE:
	{
		DbgPrintf("WM_CREATE %d %d\n", wParam, lParam);
		CREATESTRUCT* createst = (CREATESTRUCT*)lParam;
		DbgPrintf("CREATESTRUCT %s\n", createst->lpszClass);

		return 0;
	}
	case WM_MOVE:
	{
		DbgPrintf("WM_MOVE %d %d\n", wParam, lParam);
		POINTS points = MAKEPOINTS(lParam);
		DbgPrintf("X Y %d %d\n", points.x, points.y);

		return 0;
	}
	case WM_SIZE:
	{
		DbgPrintf("WM_SIZE %d %d\n", wParam, lParam);
		int newWidth = (int)(short)LOWORD(lParam);
		int newHeight = (int)(short)HIWORD(lParam);
		DbgPrintf("WM_SIZE %d %d\n", newWidth, newHeight);

		return 0;
	}
	case WM_DESTROY:
	{
		DbgPrintf("WM_DESTROY %d %d\n", wParam, lParam);
		PostQuitMessage(0);

		return 0;
	}
	//键盘消息							
	case WM_KEYUP:
	{
		DbgPrintf("WM_KEYUP %d %d\n", wParam, lParam);

		return 0;
	}
	case WM_KEYDOWN:
	{
		DbgPrintf("WM_KEYDOWN %d %d\n", wParam, lParam);

		return 0;
	}
	//鼠标消息							
	case WM_LBUTTONDOWN:
	{
		DbgPrintf("WM_LBUTTONDOWN %d %d\n", wParam, lParam);
		POINTS points = MAKEPOINTS(lParam);
		DbgPrintf("WM_LBUTTONDOWN %d %d\n", points.x, points.y);

		return 0;
	}
	}

	
	return DefWindowProc(hwnd, uMsg, wParam, lParam);

}
上一篇:win32 API编程之常用控件


下一篇:通过pinvoke.net生成win32托管代码