当前位置:网站首页>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;
}
运行结果

边栏推荐
- Julia 1.6 1.7 common problem solving
- Neo4j installation tutorial
- Ansible practical Series II_ Getting started with Playbook
- Install MySQL for Ubuntu 20.04
- The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
- Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
- 数据库高级学习笔记--SQL语句
- Some problems in the development of unity3d upgraded 2020 VR
- 软件测试-面试题分享
- Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
猜你喜欢

图片上色项目 —— Deoldify

MySQL master-slave replication, read-write separation

Deoldify project problem - omp:error 15:initializing libiomp5md dll,but found libiomp5md. dll already initialized.

Install mongdb tutorial and redis tutorial under Windows

QT creator support platform

软件测试与质量学习笔记3--白盒测试

Did you forget to register or load this tag
![[recommended by bloggers] background management system of SSM framework (with source code)](/img/7f/a6b7a8663a2e410520df75fed368e2.png)
[recommended by bloggers] background management system of SSM framework (with source code)

Idea import / export settings file

Django running error: error loading mysqldb module solution
随机推荐
Software testing and quality learning notes 3 -- white box testing
QT creator create button
Swagger、Yapi接口管理服务_SE
In the era of DFI dividends, can TGP become a new benchmark for future DFI?
AI benchmark V5 ranking
How to set up voice recognition on the computer with shortcut keys
引入了junit为什么还是用不了@Test注解
Introduction to the easy copy module
Heating data in data lake?
Ansible实战系列二 _ Playbook入门
自动机器学习框架介绍与使用(flaml、h2o)
【博主推荐】C# Winform定时发送邮箱(附源码)
[recommended by bloggers] C WinForm regularly sends email (with source code)
JDBC原理
One click extraction of tables in PDF
Database advanced learning notes -- SQL statement
ImportError: libmysqlclient. so. 20: Cannot open shared object file: no such file or directory solution
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
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
Django running error: error loading mysqldb module solution