c++ Win32 窗口/键盘控制/鼠标控制/定时器/绘画 基础实现 使用vscode和gcc

gcc 版本 12.2.0

vscode 需要修改脚本 添加"-mwindows"编译指令

			"-mwindows"

tasks.json

            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-mwindows"
            ]

如果出现乱码修改系统设置

在这里插入图片描述

src

#include <windows.h>
#include <iostream>
#include <sstream>

/*************************************
第一步定义WinMain()
第二步定义消息处理函数
第三步注册窗口
第四步创建窗口
第五步显示窗口
第六步消息循环
第七步处理消息
**************************************/

//接收标准输出句柄
HANDLE get_put=0;

/*
自定义消息
系统消息ID范围		0-0x03FF
自定义消息ID范围	0x0400-0x7fff
WM_USER==0x0400
自定义消息需要自己发送
*/
#define MY_ONE_A WM_USER+1020

void print_painting(HWND hWnd);//绘画
void create_A(HWND hWnd);//创建子窗口
void print_size_A(HWND hWnd,LPARAM lParam);//在dos输出
void print_def_message(HWND hWnd,WPARAM wParam,LPARAM lParam);//自定义消息输出
void keydown_key(HWND hWnd,WPARAM wParam);//键盘摁下输出
void keyup_key(HWND hWnd,WPARAM wParam);//键盘抬起输出
void distinguish_key_char(HWND hWnd,WPARAM wParam);//字符消息输出
void lllbutton_down(HWND hWnd,WPARAM wParam,LPARAM lParam);//鼠标信息
void lllbutton_up(HWND hWnd,WPARAM wParam,LPARAM lParam);//鼠标信息
void rrrbutton_down(HWND hWnd,WPARAM wParam,LPARAM lParam);//鼠标信息
void rrrbutton_up(HWND hWnd,WPARAM wParam,LPARAM lParam);//鼠标信息
void mousemove(HWND hWnd,WPARAM wParam,LPARAM lParam);//鼠标移动消息
WPARAM timer_bomb(HWND hWnd,WPARAM wParam);//定时器
std::string wm_rl(std::string xxx,WPARAM wParam,LPARAM lParam);//格式化字符串
std::string wm_rl(std::string xxx,WPARAM wParam);//格式化字符串
std::string wm_rl(std::string xxx,std::string xxx_2,std::string xxx_3,std::string xxx_4,WPARAM wParam,LPARAM lParam);//格式化字符串
LRESULT CALLBACK WndProc(HWND hWnd,UINT msgld,WPARAM wParam,LPARAM lParam);
//第二步定义消息处理函数|系统调用该函数
LRESULT CALLBACK WndProc(HWND hWnd,UINT msgld,WPARAM wParam,LPARAM lParam)
{
	
	//第七步处理消息
	switch (msgld)
	{
	//定时器处理
	case WM_TIMER:
		timer_bomb(hWnd,wParam);
		break;
	//鼠标信息
	case WM_LBUTTONDOWN:
		lllbutton_down(hWnd,wParam,lParam);
		break;
	case WM_LBUTTONUP:
		lllbutton_up(hWnd,wParam,lParam);
		break;
	case WM_RBUTTONDOWN:
		rrrbutton_down(hWnd,wParam,lParam);
		break;
	case WM_RBUTTONUP:
		rrrbutton_up(hWnd,wParam,lParam);
		break;
	//鼠标移动消息
	case WM_MOUSEMOVE:
		mousemove(hWnd,wParam,lParam);
	//字符消息区分大小写
	case WM_CHAR:
		distinguish_key_char(hWnd,wParam);
		break;
	//键盘消息不区分大小写
	//键盘摁下
	case WM_KEYDOWN:
		keydown_key(hWnd,wParam);
		break;
	//键盘抬起
	case WM_KEYUP:
		keyup_key(hWnd,wParam);
		break;
	//MY_ONE_A自定义消息
	case MY_ONE_A:
		print_def_message(hWnd,wParam,lParam);
		break;
	/*
	在Win32编程中WM_SIZE消息是窗口大小改变时发送的消息当窗口大小发生变化时
	系统会发送WM_SIZE消息给窗口过程函数
	以便程序可以根据新的窗口尺寸来进行相应的处理
	*/
	case WM_SIZE:
		print_size_A(hWnd,lParam);
		break;
	/*
	在Win32编程中WM_CREATE消息是在窗口被创建时发送的消息当一个窗口被创建时
	系统会发送WM_CREATE消息给窗口过程函数
	以便程序可以在窗口创建的过程中进行一些初始化工作
	*/
	case WM_CREATE:
		SetTimer(hWnd,1,1000,NULL);//定时器
		SetTimer(hWnd,2,1000,NULL);//定时器
		create_A(hWnd);
		break;
	/*
	在Win32编程中WM_SYSCOMMAND消息是在用户执行系统命令
	(如点击窗口菜单|按下窗口标题栏上的按钮等)时发送的消息
	当用户执行系统命令时系统会发送WM_SYSCOMMAND消息给窗口过程函数
	以便程序可以根据具体的系统命令进行相应的处理
	*/
	case WM_SYSCOMMAND:
		if (wParam==SC_CLOSE)
		{
			int off_xxx=MessageBox(hWnd,"是否退出","?",MB_YESNO);
			if (off_xxx==IDYES){}
			else return 0;
		}
		break;
	/*
	在Win32编程中WM_DESTROY消息是在窗口即将被销毁时发送的消息
	当一个窗口即将被销毁时系统会发送WM_DESTROY消息给窗口过程函数
	以便程序可以进行一些清理工作
	*/
	case WM_COMMAND:
        // 处理按钮点击事件
        if (LOWORD(wParam)==1) 
		{
            HDC hdc=GetDC(hWnd);
            RECT rect={45,45,60,60}; // 设置输出区域
            DrawText(hdc,"a",1,&rect,DT_LEFT); // 输出字符"a"到指定区域
            ReleaseDC(hWnd,hdc);
        }
        break;
	//关闭关闭关闭
	case WM_DESTROY:
		//PostQuitMessage(0);//让GetMessage函数返回0
		/*
		在Win32编程中WM_QUIT消息是一个系统发送给消息循环以通知应用程序退出的消息
		当调用PostQuitMessage函数时系统会发送WM_QUIT消息给消息循环从而导致消息循环结束
		应用程序退出
		*/
		//消息制造
		PostMessage(hWnd,WM_QUIT,0,0);
		/*
		PostMessage();投递消息 消息发出后立刻返回不等待消息处理结果
		SendMessage();发送消息 发消息会等待消息处理的结果
		*/
		break;
	}

	return DefWindowProc(hWnd,msgld,wParam,lParam);
}

//定时器处理
WPARAM timer_bomb(HWND hWnd,WPARAM wParam)
{

	
	WriteConsole(get_put,wm_rl("定时器:",wParam).c_str(),strlen(wm_rl("定时器:",wParam).c_str()),NULL,NULL);
	return wParam;
}

void distinguish_key_char(HWND hWnd,WPARAM wParam)
{
	WriteConsole(get_put,wm_rl("WM_CHAR:",wParam).c_str(),strlen(wm_rl("WM_CHAR:",wParam).c_str()),NULL,NULL);
}

//键盘抬起
void keyup_key(HWND hWnd,WPARAM wParam)
{
	WriteConsole(get_put,wm_rl("WM_KEYUP:",wParam).c_str(),strlen(wm_rl("WM_KEYUP:",wParam).c_str()),NULL,NULL);
}

//键盘摁下
void keydown_key(HWND hWnd,WPARAM wParam)
{
	WriteConsole(get_put,wm_rl("WM_KEYDOWN:",wParam).c_str(),strlen(wm_rl("WM_KEYDOWN:",wParam).c_str()),NULL,NULL);
}

//在dos输出
void print_size_A(HWND hWnd,LPARAM lParam) 
{
    short _width=LOWORD(lParam);
    short _height=HIWORD(lParam);
	//格式化字符串
    std::stringstream ss;
    ss<<"W:"<<_width<<"|H:"<<_height<<std::endl;
    std::string text=ss.str();
    const char *_text_a=text.c_str();
    WriteConsoleA(get_put,_text_a,strlen(_text_a),NULL,NULL);
}

//自定义消息输出
void print_def_message(HWND hWnd,WPARAM wParam,LPARAM lParam)
{
	//格式化字符串
	std::stringstream de;
	de<<"自定义消息"<<wParam<<"|"<<lParam;
	std::string texts=de.str();
	MessageBox(hWnd,texts.c_str(),"",MB_OK);
}

//创建子窗口
void create_A(HWND hWnd)
{
	//发送自定义消息
	PostMessage(hWnd,MY_ONE_A,1,1);
	//子窗口
	MessageBox(NULL,"create","a",MB_OK);
	CreateWindowEx
	(
		0,
		"EDIT",
		"aaaaa",
		WS_CHILD|WS_VISIBLE|WS_BORDER,
		0,
		0,
		200,
		200,
		hWnd,
		NULL,
		0,
		NULL
	);
}

//绘画
void print_painting(HWND hWnd)
{
	//拿到画笔
	PAINTSTRUCT ps={0};
	HDC hdc=BeginPaint(hWnd,&ps);
	/*
	for (int i = 0; i <100; i++)
	{
		SetPixel(hdc,500+i,500+i,RGB(255,0,0));
	}
	SetPixel(hdc,600,600,RGB(255,0,0));
	Rectangle(hdc, 0, 0, 100, 100);
	Ellipse(hdc, 0, 0, 100, 100);*/
	HBRUSH hBrush=CreateSolidBrush(RGB(255,0,0));
	HGDIOBJ def_f = SelectObject(hdc,hBrush);
	Ellipse(hdc,0,0,100,100);
	//释放画笔
	EndPaint(hWnd,&ps);
	SelectObject(hdc,def_f);
	DeleteObject(hBrush);
}

std::string wm_rl(std::string xxx,WPARAM wParam)
{
	std::stringstream key_char;
	key_char<<std::endl<<xxx<<" "<<wParam;
	std::string text_key=key_char.str();
	return	text_key;
}

std::string wm_rl(std::string xxx,WPARAM wParam,LPARAM lParam)
{
	std::stringstream key_char;
	key_char<<std::endl<<xxx<<" | 摁键状态 | "<<wParam<<" | "<<"X | "<<LOWORD(lParam)<<"Y |"<<HIWORD(lParam);
	std::string text_key=key_char.str();
	return	text_key;
}

std::string wm_rl(std::string xxx,std::string xxx_2,std::string xxx_3,std::string xxx_4,WPARAM wParam,LPARAM lParam)
{
	std::stringstream key_char;
	key_char<<std::endl<<xxx<<" | "<<xxx_2<<" | "<<wParam<<" | "<<xxx_3<<" | "<<LOWORD(lParam)<<xxx_4<<" |"<<HIWORD(lParam);
	std::string text_key=key_char.str();
	return	text_key;
}

void lllbutton_down(HWND hWnd,WPARAM wParam,LPARAM lParam)
{
	WriteConsole(get_put,wm_rl("WM_LBUTTONDOWN",wParam,lParam).c_str(),strlen(wm_rl("WM_LBUTTONDOWN",wParam,lParam).c_str()),NULL,NULL);
}
void lllbutton_up(HWND hWnd,WPARAM wParam,LPARAM lParam)
{
	WriteConsole(get_put,wm_rl("WM_LBUTTONUP",wParam,lParam).c_str(),strlen(wm_rl("WM_LBUTTONUP",wParam,lParam).c_str()),NULL,NULL);
}
void rrrbutton_down(HWND hWnd,WPARAM wParam,LPARAM lParam)
{
	WriteConsole(get_put,wm_rl("WM_RBUTTONDOWN",wParam,lParam).c_str(),strlen(wm_rl("WM_RBUTTONDOWN",wParam,lParam).c_str()),NULL,NULL);
}
void rrrbutton_up(HWND hWnd,WPARAM wParam,LPARAM lParam)
{
	WriteConsole(get_put,wm_rl("WM_RBUTTONUP",wParam,lParam).c_str(),strlen(wm_rl("WM_RBUTTONUP",wParam,lParam).c_str()),NULL,NULL);
}
//鼠标移动消息

void mousemove(HWND hWnd,WPARAM wParam,LPARAM lParam)
{
	WriteConsole(get_put,wm_rl("鼠标移动消息","鼠标移动消息状态","x","y",wParam,lParam).c_str(),strlen(wm_rl("鼠标移动消息","鼠标移动消息状态","x","y",wParam,lParam).c_str()),NULL,NULL);
}

/*

int WINAPI WinMain(_In_ HINSTANCE hInstance, 
_In_opt_ HINSTANCE hPrevInstance, 
_In_ LPSTR lpCmdLine, 
_In_ int nShowCmd)

hInstance: 当前应用程序实例的句柄这个句柄是唯一标识当前
应用程序的实例可以用来获取应用程序的资源、窗口类等信息

hPrevInstance: 该参数在32位Windows中已经被废弃
始终为NULL在16位Windows中它用于指向前一个实例的句柄现代的Windows应用程序不需要关注这个参数

lpCmdLine: 包含命令行参数的字符串可以用来传递启动应用程序时的命令行参数
通常用于指定应用程序的启动配置或文件路径等信息

nShowCmd: 一个指示应用程序如何显示的标志可以是以下值之一:
	SW_HIDE: 隐藏窗口并激活其他窗口。
	SW_MAXIMIZE: 最大化窗口
	SW_MINIMIZE: 最小化窗口
	SW_RESTORE: 恢复窗口的大小和位置
	SW_SHOW: 激活并显示窗口
	SW_SHOWDEFAULT: 根据启动时的启动参数指定的显示方式显示窗口
	SW_SHOWMAXIMIZED: 最大化窗口
	SW_SHOWMINIMIZED: 最小化窗口
	SW_SHOWMINNOACTIVE: 最小化窗口,但不激活
	SW_SHOWNA: 激活窗口保持其当前的显示方式
	SW_SHOWNOACTIVATE: 显示窗口但不激活
	SW_SHOWNORMAL: 恢复窗口的大小和位置
*/

//第一步定义WinMain()
int WINAPI WinMain(_In_ HINSTANCE hInstance,_In_opt_ HINSTANCE hPrevInstance,_In_ LPSTR lpCmdLine,_In_ int nShowCmd)
{
	//增加DOS方便调试
	AllocConsole();
	//获得标准输出句柄
	get_put=GetStdHandle(STD_OUTPUT_HANDLE);
	//第三步注册窗口
	//设计
	WNDCLASS windows_x={0};

	//注册
	//窗口类的附加内存相当于缓冲区一般为0
	windows_x.cbClsExtra=0;
	//窗口类的附加内存相当于缓冲区一般为0
	windows_x.cbWndExtra=0;
	//获取笔刷|作用:填充背景色
	windows_x.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	//鼠标指针样式
	windows_x.hCursor=NULL;
	//系统默认图标
	windows_x.hIcon=NULL;
	//程序实例句柄
	windows_x.hInstance=hInstance;
	//消息处理函数(系统调用)
	windows_x.lpfnWndProc=WndProc;
	//给类起名
	windows_x.lpszClassName=TEXT("mainss");
	//菜单 NULL=不要菜单
	windows_x.lpszMenuName=NULL;
	//窗口类样式
	windows_x.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;

	//注册到系统/写入系统内核
	RegisterClass(&windows_x);

	//第四步创建窗口
	HWND hWnd=CreateWindow
	(
		TEXT("mainss"),
		TEXT("mainss_windows"),
		WS_OVERLAPPEDWINDOW,
		0,
		0,
		1024,
		768,
		NULL,
		NULL,
		hInstance,
		NULL
	);
	// 创建按钮
    CreateWindow
	(
		"BUTTON",
		"Press Me",
		WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
		150,100,100,50,
		hWnd,
		(HMENU)1,
		NULL,
		NULL
	);
	//第五步显示窗口
	ShowWindow(hWnd,SW_SHOW);
	UpdateWindow(hWnd);

	//第六步消息循环
	MSG msg={0};
	//如果给它指定了固定的窗口句柄则只会抓取该窗口的消息
	/*
	一般消息循环
	while (GetMessage(&msg,NULL,0,0))
	{
		//翻译
		TranslateMessage(&msg);
		//转发 
		DispatchMessage(&msg);
	}*/
	//高效的消息循环
	while (1)
	{
		if (PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
		{	
			//有消息时
			if(GetMessage(&msg,NULL,0,0))
			{
				//翻译
				TranslateMessage(&msg);
				//转发 
				DispatchMessage(&msg);
			}else return 0;//没消息时
		}
		else
		{
			//WriteConsole(get_put,"NULL",strlen("NULL"),NULL,NULL);
			//可以做其他事
		}
	}
	
	//MessageBox(NULL,"你的电脑已被入侵","Windows提示",MB_ICONERROR);
	return 0;
}  
  • 14
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值