当前位置:网站首页>vs2019 桌面程序快速入门
vs2019 桌面程序快速入门
2022-07-06 09:14:00 【imxlw00】
创建项目
选择windows桌面向导
配置项目

选择程序类型

编写代码

#include <windows.h> //底层实现窗口 的头文件
//6处理窗口过程
//CALLBACK 代表__stdcall 参数的传递顺序:从右到左 以此入栈,并且在函数返回前 清空堆栈
LRESULT CALLBACK WindowProc(
HWND hwnd, //消息所属的窗口句柄
UINT uMsg, //具体消息名称 WM_XXXX 消息名
WPARAM wParam, //键盘附加消息
LPARAM lParam //鼠标附加消息
)
{
switch (uMsg)
{
case WM_CLOSE:
//所有xxxWindow为结尾的方法 ,都不会进入到消息队列中,而是直接执行
DestroyWindow(hwnd); //DestroyWindow 发送另一个消息 WM_DESTROY
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN: //鼠标左键按下
{
int xPos = LOWORD(lParam);
int yPos = HIWORD(lParam);
char buf[1024];
wsprintf(buf,TEXT("x = %d,y = %d"), xPos, yPos);
MessageBox(hwnd, buf, TEXT("鼠标左键按下"), MB_OK);
break;
}
case WM_KEYDOWN: //键盘
MessageBox(hwnd, TEXT("键盘按下"), TEXT("键盘按下"), MB_OK);
break;
case WM_PAINT: //绘图
{
PAINTSTRUCT ps; //绘图结构体
HDC hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 100, 100, TEXT("HELLO"), strlen("HELLO"));
EndPaint(hwnd, &ps);
}
break;
}
//返回值用默认处理方式
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
//程序入口函数
//WINAPI 代表__stdcall 参数的传递顺序:从右到左 以此入栈,并且在函数返回前 清空堆栈
int WINAPI WinMain(
HINSTANCE hInstance, //应用程序实例句柄
HINSTANCE hPrevInstance, //上一个应用程序句柄,在win32环境下,参数一般为NULL,不起作用了
LPSTR lpCmdLine, //char * argv[]
int nShowCmd) //显示命令 最大化、最小化 正常
{
//1、设计窗口
//2、注册窗口
//3、创建窗口
//4、显示和更新
//5、通过循环取消息
//6、处理消息 (窗口过程)
//1、设计窗口
WNDCLASS wc;
wc.cbClsExtra = 0; //类的额外的内存
wc.cbWndExtra = 0; //窗口额外的内存
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //设置背景
wc.hCursor = LoadCursor(NULL, IDC_HAND); //设置光标 如果第一个参数为NULL,代表使用系统提供的光标
wc.hIcon = LoadIcon(NULL, IDI_ERROR); //图标 如果第一个参数为NULL,代表使用系统提供的光标
wc.hInstance = hInstance; //应用程序实例句柄 传入WinMain中的形参即可
wc.lpfnWndProc = WindowProc; //回调函数 窗口过程
wc.lpszClassName = TEXT("WIN"); //指定窗口类名称
wc.lpszMenuName = NULL; //菜单名称
wc.style = 0; //显示风格 0代表默认风格
//2、注册窗口类
RegisterClass(&wc);
//3、创建窗口
/* lpClassName, 类名 lpWindowName, 标题名 dwStyle, WS_OVERLAPPEDWINDOW 风格 x, 显示坐标 CW_USEDEFAULT 默认值 y, nWidth, 宽高 nHeight, hWndParent, 父窗口 NULL hMenu, 菜单 NULL hInstance, 实例句柄 hInstance lpParam) 附加值 NULL */
HWND hwnd = CreateWindow(wc.lpszClassName, TEXT("WINDOWS"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
//4、 显示和更新
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
//5、 通过循环取消息
/* HWND hwnd; 主窗口句柄 UINT message; 具体消息名称 WPARAM wParam; 附加消息 键盘消息 LPARAM lParam; 附加消息 鼠标消息 DWORD time; 消息产生时间 POINT pt; 附加消息 鼠标消息 x y */
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
/* _Out_ LPMSG lpMsg, 消息 _In_opt_ HWND hWnd, 捕获窗口 填NULL代表捕获所有的窗口 _In_ UINT wMsgFilterMin, //最小和最大的过滤的消息 一般填入0 _In_ UINT wMsgFilterMax) //填0代表捕获所有消息 */
//if (GetMessage(&msg, NULL, 0, 0) == FALSE)
//{
// break;
//}
//翻译消息
TranslateMessage(&msg);
//不为false
//分发消息
DispatchMessage(&msg);
}
return 0;
}
运行结果

边栏推荐
- QT creator shape
- Kept VRRP script, preemptive delay, VIP unicast details
- 报错解决 —— io.UnsupportedOperation: can‘t do nonzero end-relative seeks
- UDS learning notes on fault codes (0x19 and 0x14 services)
- Invalid default value for 'create appears when importing SQL_ Time 'error reporting solution
- Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
- 01 project demand analysis (ordering system)
- error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
- Cookie setting three-day secret free login (run tutorial)
- Classes in C #
猜你喜欢

引入了junit为什么还是用不了@Test注解

Use dapr to shorten software development cycle and improve production efficiency

Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
![[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)](/img/b7/aae35f049ba659326536904ab089cb.png)
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)

连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法

Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes

Knowledge Q & A based on Apache Jena

Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path

MySQL主從複制、讀寫分離

AcWing 1298. Solution to Cao Chong's pig raising problem
随机推荐
MySQL completely uninstalled (windows, MAC, Linux)
Summary of numpy installation problems
虚拟机Ping通主机,主机Ping不通虚拟机
JDBC原理
Project practice - background employee information management (add, delete, modify, check, login and exit)
QT creator create button
FRP intranet penetration
UDS learning notes on fault codes (0x19 and 0x14 services)
Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
Ansible实战系列三 _ task常用命令
数据库高级学习笔记--SQL语句
QT creator uses Valgrind code analysis tool
Django运行报错:Error loading MySQLdb module解决方法
Idea import / export settings file
La table d'exportation Navicat génère un fichier PDM
CSDN markdown editor
Kept VRRP script, preemptive delay, VIP unicast details
Ansible practical series I_ introduction
neo4j安装教程
Learn winpwn (3) -- sEH from scratch