Просмотр исходного кода

feat: 添加键盘、鼠标操作事件

nicetry12138 1 год назад
Родитель
Сommit
42aece38f8

BIN
图形学/DirectX学习/Image/007.png


BIN
图形学/DirectX学习/Image/008.png


+ 41 - 2
图形学/DirectX学习/README.md

@@ -283,8 +283,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
 
 这里使用 `PostQuitMessage(69)` 没有任何含义,单纯就是为了测试输出结果是否生效
 
+### 窗口消息
 
-消息循环的类型有很多
+消息的类型有很多
 
 [list of windows Message](https://wiki.winehq.org/List_Of_Windows_Messages)
 
@@ -307,10 +308,48 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
 
 `WM_CHAR` 是用于文本输入的信息,所以一些按键按下之后不会触发 `WM_CHAR`,比如 `F1`、`F2`,并且 `WM_CHAR` 是大小写敏感的,`WM_KEYDOWN` 则大小写不敏感
 
+同样按下 `F` 键,`WM_KEYDOWN` 的 `wParam` 是 **0x0000046**,表示大写的 `F`;而 `WM_CHAR` 的 `wParam` 是 **0x0000066** 表示小写的 `f`
+
+```cpp
+case WM_CHAR:
+{
+	static std::string title;
+	title.push_back((char)wParam);
+	SetWindowText(hWnd, to_wide_string(title).c_str());
+	break;
+}
+```
+
+![](Image/007.png)
+
 以鼠标点击为例
 
 ![](Image/006.png)
 
 主要的消息触发就是:`WM_LBUTTONDOWN` 和 `WM_LBUTOTNUP` 来表示鼠标左键的点击和松开,对应的鼠标右键点击就是 `WM_RBUTTONDOWN` 和 `WM_RBUTTONUP`,鼠标移动有 `WM_MOUSEMOVE`
 
- 
+[LBUTTONDOWN](https://learn.microsoft.com/zh-cn/windows/win32/inputdev/wm-lbuttondown) 更多参数信息官方文档有说明
+
+```cpp
+POINT pt;
+pt.x = GET_X_LPARAM(lParam);
+pt.y = GET_Y_LPARAM(lParam);
+
+pt = MAKEPOINTS(lParam);
+```
+
+通过上面俩种方法可以获得鼠标相对工作区左上角的坐标
+
+```cpp
+case WM_LBUTTONDOWN:
+{
+	POINTS pt = MAKEPOINTS(lParam);
+	std::ostringstream oss;
+	oss << "(" << pt.x << ", " << pt.y << ")";
+	SetWindowText(hWnd, to_wide_string(oss.str()).c_str());
+	break;
+}
+```
+
+![](Image/008.png)
+

+ 68 - 0
图形学/DirectX学习/src/hd3d/hd3d/ChiliWin.h

@@ -0,0 +1,68 @@
+/******************************************************************************************
+*	Chili Direct3D Engine																  *
+*	Copyright 2018 PlanetChili <http://www.planetchili.net>								  *
+*																						  *
+*	This file is part of Chili Direct3D Engine.											  *
+*																						  *
+*	Chili Direct3D Engine is free software: you can redistribute it and/or modify		  *
+*	it under the terms of the GNU General Public License as published by				  *
+*	the Free Software Foundation, either version 3 of the License, or					  *
+*	(at your option) any later version.													  *
+*																						  *
+*	The Chili Direct3D Engine is distributed in the hope that it will be useful,		  *
+*	but WITHOUT ANY WARRANTY; without even the implied warranty of						  *
+*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the						  *
+*	GNU General Public License for more details.										  *
+*																						  *
+*	You should have received a copy of the GNU General Public License					  *
+*	along with The Chili Direct3D Engine.  If not, see <http://www.gnu.org/licenses/>.    *
+******************************************************************************************/
+#pragma once
+
+// target Windows 7 or later
+#define _WIN32_WINNT 0x0601
+#include <sdkddkver.h>
+// The following #defines disable a bunch of unused windows stuff. If you 
+// get weird errors when trying to do some windows stuff, try removing some
+// (or all) of these defines (it will increase build time though).
+#ifndef FULL_WINTARD
+#define WIN32_LEAN_AND_MEAN
+#define NOGDICAPMASKS
+#define NOSYSMETRICS
+#define NOMENUS
+#define NOICONS
+#define NOSYSCOMMANDS
+#define NORASTEROPS
+#define OEMRESOURCE
+#define NOATOM
+#define NOCLIPBOARD
+#define NOCOLOR
+#define NOCTLMGR
+#define NODRAWTEXT
+#define NOKERNEL
+#define NONLS
+#define NOMEMMGR
+#define NOMETAFILE
+#define NOOPENFILE
+#define NOSCROLL
+#define NOSERVICE
+#define NOSOUND
+#define NOTEXTMETRIC
+#define NOWH
+#define NOCOMM
+#define NOKANJI
+#define NOHELP
+#define NOPROFILER
+#define NODEFERWINDOWPOS
+#define NOMCX
+#define NORPC
+#define NOPROXYSTUB
+#define NOIMAGE
+#define NOTAPE
+#endif
+
+#define NOMINMAX
+
+#define STRICT
+
+#include <Windows.h>

+ 16 - 0
图形学/DirectX学习/src/hd3d/hd3d/WinMain.cpp

@@ -1,5 +1,6 @@
 #include <Windows.h>
 #include <cstdlib>
+#include <sstream>
 #include "Test/WindowsMessageMap.h"
 
 inline std::wstring to_wide_string(const std::string& input) //string to wstring
@@ -25,6 +26,21 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
 			SetWindowText(hWnd, L"Reset Title");
 		}
 		break;
+	case WM_CHAR:
+	{
+		static std::string title;
+		title.push_back((char)wParam);
+		SetWindowText(hWnd, to_wide_string(title).c_str());
+		break;
+	}
+	case WM_LBUTTONDOWN:
+	{
+		POINTS pt = MAKEPOINTS(lParam);
+		std::ostringstream oss;
+		oss << "(" << pt.x << ", " << pt.y << ")";
+		SetWindowText(hWnd, to_wide_string(oss.str()).c_str());
+		break;
+	}
 	default:
 		break;
 	}