第一个窗口

在这里插入图片描述

一、设计一个窗口

TCHAR className[] = "My First Window"; 		// 创建窗口类的对象 									
WNDCLASS wndclass = {0};					//一定要先将所有值赋值			
wndclass.hbrBackground = (HBRUSH)COLOR_MENU;//窗口的背景色			
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(  								
				IN  HWND hwnd,  	//句柄
				IN  UINT uMsg,  		//消息类型
				IN  WPARAM wParam,  	
				IN  LPARAM lParam  	
				)  	

七、系统处理不相关的消息

如果不是我们关注的消息,就交给系统去处理

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

八、在进程中退出

{
	switch(uMsg)	
	{
	case WM_DESTROY:							
		{														
			PostQuitMessage(0);															
			return 0;					
		}						
	}

九、win32中打印消息

#include "stdio.h"

void __cdecl OutputDebugStringF(const char *format, ...)  
{  
    va_list vlArgs;  
    char    *strBuffer = (char*)GlobalAlloc(GPTR, 4096);  
	
    va_start(vlArgs, format);  
    _vsnprintf(strBuffer, 4096 - 1, format, vlArgs);  
    va_end(vlArgs);  
    strcat(strBuffer, "\n");  
    OutputDebugStringA(strBuffer);  
    GlobalFree(strBuffer);  
    return;  
}  

OutputDebugStringF("%x",uMsg);

十、完整代码

// test.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "stdio.h"

void __cdecl OutputDebugStringF(const char *format, ...)  
{  
    va_list vlArgs;  
    char    *strBuffer = (char*)GlobalAlloc(GPTR, 4096);  
	
    va_start(vlArgs, format);  
    _vsnprintf(strBuffer, 4096 - 1, format, vlArgs);  
    va_end(vlArgs);  
    strcat(strBuffer, "\n");  
    OutputDebugStringA(strBuffer);  
    GlobalFree(strBuffer);  
    return;  
}  

	LRESULT CALLBACK WindowProc(  						//处理函数申明		
								IN  HWND hwnd,  	
								IN  UINT uMsg,  	
								IN  WPARAM wParam,  	
								IN  LPARAM lParam  	
								);  	
								
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	//窗口的类名									
	TCHAR className[] = "My First Window"; 																		
	// 创建窗口类的对象 									
	WNDCLASS wndclass = {0};						//一定要先将所有值赋值			
	wndclass.hbrBackground = (HBRUSH)COLOR_MENU;	//窗口的背景色			
	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);  			
	}  													
	return 0;
}


//处理函数
LRESULT CALLBACK WindowProc(  						
							IN  HWND hwnd,  	
							IN  UINT uMsg,  	
							IN  WPARAM wParam,  	
							IN  LPARAM lParam  	
							) 	
{								
	switch(uMsg)
	{				
		case WM_CREATE:
			{
				OutputDebugStringF("WM_CREATE:%x",uMsg);	//消息类型为1
				return 0;
			}			
		case WM_DESTROY:							
			{										
				PostQuitMessage(0);		//消息类型为2									
				return 0;					
			}						
	}							
	return DefWindowProc(hwnd,uMsg,wParam,lParam);					
}

猜你喜欢

转载自blog.csdn.net/qq_41232519/article/details/108500560
今日推荐