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

边栏推荐
- UDS learning notes on fault codes (0x19 and 0x14 services)
- AcWing 1298. Solution to Cao Chong's pig raising problem
- [free setup] asp Net online course selection system design and Implementation (source code +lunwen)
- [Thesis Writing] how to write function description of jsp online examination system
- [recommended by bloggers] asp Net WebService background data API JSON (with source code)
- Did you forget to register or load this tag
- [recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
- QT creator uses Valgrind code analysis tool
- QT creator runs the Valgrind tool on external applications
- QT creator test
猜你喜欢

【博主推荐】asp.net WebService 后台数据API JSON(附源码)

csdn-Markdown编辑器

Did you forget to register or load this tag
![[download app for free]ineukernel OCR image data recognition and acquisition principle and product application](/img/1b/ed39a8b9181660809a081798eb8a24.jpg)
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application

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

When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page

Navicat 導出錶生成PDM文件

La table d'exportation Navicat génère un fichier PDM
![[free setup] asp Net online course selection system design and Implementation (source code +lunwen)](/img/ac/b518796a92d00615cd374c0c835f38.jpg)
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)

机器学习--人口普查数据分析
随机推荐
数据库高级学习笔记--SQL语句
Base de données Advanced Learning Notes - - SQL statements
Request object and response object analysis
QT creator specify editor settings
JDBC原理
Use dapr to shorten software development cycle and improve production efficiency
Picture coloring project - deoldify
AcWing 1294. Cherry Blossom explanation
Swagger, Yapi interface management service_ SE
Tcp/ip protocol (UDP)
Some problems in the development of unity3d upgraded 2020 VR
[AGC009D]Uninity
Number game
JDBC principle
[蓝桥杯2020初赛] 平面切分
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
Summary of numpy installation problems
Machine learning notes week02 convolutional neural network
Idea import / export settings file
机器学习--人口普查数据分析