This commit is contained in:
2025-11-06 01:00:40 +08:00
parent 24cbbcccc4
commit f945901c18
4 changed files with 300 additions and 2 deletions
+259
View File
@@ -0,0 +1,259 @@
#include "c_WinGuiApp.h"
#include "c_WinGuiUtil.h"
/* -------------------------------------------------------------------------------------------------------------- */
/* */
#define MAIN_WINDOW_CLASSNAME "KeyMouse"
/* -------------------------------------------------------------------------------------------------------------- */
/* */
#define DIRECTION_SIZE 100
static char Directions[DIRECTION_SIZE];
static int xVal = 10;
static int yVal = 30;
/* -------------------------------------------------------------------------------------------------------------- */
/* */
static BOOL MainWindow_OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct){
c_WinGuiUtil_MoveWindowToScreenCenter(hwnd);
return TRUE;
}
static void MainWindow_OnDestroy(HWND hwnd){
PostQuitMessage(0);
}
static void MainWindow_OnPaint(HWND hwnd){
PAINTSTRUCT PaintStruct;
RECT Rect;
HDC hdc = BeginPaint(hwnd, &PaintStruct);
static char* Message[]={
{
"WM_CHAR",
"WM_KEY",
"WM_SYSKEY",
"WM_MOUSEMOVE",
"WM_MOUSEDOWN",
"WM_MOUSEUP",
}
};
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
HFONT OldFont = SelectFont(hdc, GetStockObject(OEM_FIXED_FONT));
GetClientRect(hwnd, &Rect);
DrawText(hdc, "MOUSE AND KEYBOARD DEMONSTRATION", -1, &Rect, DT_CENTER);
Rect.top = 20;
Rect.bottom = 40;
DrawText(hdc, "(Try experimenting with mouse and keyboard)", -1, &Rect, DT_CENTER);
SelectFont(hdc, OldFont);
for(int i=0; i<sizeof(Message)/sizeof(Message[0]); i++){
TextOut(hdc, xVal, yVal + (20 * (i+1)), Message[i], strlen(Message[i]));
}
EndPaint(hwnd, &PaintStruct);
}
static void MainWindow_OnChar(HWND hwnd, UINT ch, int cRepeat){
char s[100];
HDC hdc = GetDC(hwnd);
int size = snprintf(s, sizeof(s)/sizeof(s[0]), "WM_CHAR ==> Ch = %c, cRepeat = %d "
, ch
, cRepeat);
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
TextOut(hdc, xVal, yVal + 20, s, size);
ReleaseDC(hwnd, hdc);
}
static void MainWindow_OnKey(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags){
char s[100];
HDC hdc = GetDC(hwnd);
int size =0;
if(fDown){
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_KEYDOWN ==> vk = %c, fDown = %d, cRepeat = %d, flags = %d "
, vk
, fDown
, cRepeat
, flags
);
}else{
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_KEYUP ==> vk = %c, fDown = %d, cRepeat = %d, flags = %d "
, vk
, fDown
, cRepeat
, flags
);
}
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
TextOut(hdc, xVal, yVal + 40, s, size);
ReleaseDC(hwnd, hdc);
}
static void MainWindow_OnSysKey(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags){
char s[100];
HDC hdc = GetDC(hwnd);
int size = 0;
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
if(fDown){
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_SYSKEYDOWN ==> vk = %d, fDown = %d, cRepeat = %d, flags = %d "
, vk
, fDown
, cRepeat
, flags);
TextOut(hdc, xVal, yVal + 60, s, size);
FORWARD_WM_SYSKEYDOWN(hwnd, vk, cRepeat, flags, DefWindowProc);
}else{
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_SYSKEYUP ==> vk = %d, fDown = %d, cRepeat = %d, flags = %d "
, vk
, fDown
, cRepeat
, flags);
TextOut(hdc, xVal, yVal + 60, s, size);
FORWARD_WM_SYSKEYUP(hwnd, vk, cRepeat, flags, DefWindowProc);
}
ReleaseDC(hwnd, hdc);
}
static void MainWindow_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags){
char s[100];
HDC hdc = GetDC(hwnd);
int size =0;
if(fDoubleClick){
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_LBUTTONDBLCLK ==> Db = %d, x = %d, y = %d, flags = %d "
, fDoubleClick
, x
, y
, keyFlags
);
}else{
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_LBUTTONDOWN ==> Db = %d, x = %d, y = %d, flags = %d "
, fDoubleClick
, x
, y
, keyFlags
);
}
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
TextOut(hdc, xVal, yVal + 100, s, size);
ReleaseDC(hwnd, hdc);
}
static void MainWindow_OnLButtonUp(HWND hwnd, int x, int y, UINT keyFlags){
char s[100];
HDC hdc = GetDC(hwnd);
int size =0;
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_LBUTTONUP ==> x = %d, y = %d, flags = %d "
, x
, y
, keyFlags
);
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
TextOut(hdc, xVal, yVal + 120, s, size);
ReleaseDC(hwnd, hdc);
}
static void MainWindow_OnMouseMove(HWND hwnd, int x, int y, UINT keyFlags){
char s[100];
HDC hdc = GetDC(hwnd);
int size =0;
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_MOUSEMOVE ==> x = %d, y = %d, flags = %d "
, x
, y
, keyFlags
);
if((keyFlags & MK_CONTROL) == MK_CONTROL){
SetTextColor(hdc, RGB(0, 0, 255));
}
if((keyFlags & MK_LBUTTON) == MK_LBUTTON){
SetTextColor(hdc, RGB(0, 255, 0));
}
if((keyFlags & MK_RBUTTON) == MK_RBUTTON){
SetTextColor(hdc, RGB(255, 0, 0));
}
if((keyFlags & MK_SHIFT) == MK_SHIFT){
SetTextColor(hdc, RGB(255, 0, 255));
}
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
TextOut(hdc, xVal, yVal + 80, s, size);
ReleaseDC(hwnd, hdc);
}
/* -------------------------------------------------------------------------------------------------------------- */
/* */
c_WinGuiApp_EventBegin(WndProc)
c_WinGuiApp_OnEvent(WM_CREATE, MainWindow_OnCreate)
c_WinGuiApp_OnEvent(WM_CHAR, MainWindow_OnChar)
c_WinGuiApp_OnEvent(WM_KEYDOWN, MainWindow_OnKey)
c_WinGuiApp_OnEvent(WM_KEYUP, MainWindow_OnKey)
c_WinGuiApp_OnEvent(WM_SYSKEYDOWN, MainWindow_OnSysKey)
c_WinGuiApp_OnEvent(WM_SYSKEYUP, MainWindow_OnSysKey)
c_WinGuiApp_OnEvent(WM_MOUSEMOVE, MainWindow_OnMouseMove)
c_WinGuiApp_OnEvent(WM_LBUTTONDBLCLK, MainWindow_OnLButtonDown)
c_WinGuiApp_OnEvent(WM_LBUTTONDOWN, MainWindow_OnLButtonDown)
c_WinGuiApp_OnEvent(WM_LBUTTONUP, MainWindow_OnLButtonUp)
c_WinGuiApp_OnEvent(WM_DESTROY, MainWindow_OnDestroy)
c_WinGuiApp_OnEvent(WM_PAINT, MainWindow_OnPaint)
c_WinGuiApp_EventEnd
/* -------------------------------------------------------------------------------------------------------------- */
/* */
static bool Register(HINSTANCE hInstance){
WNDCLASS WndClass;
WndClass.lpszClassName = MAIN_WINDOW_CLASSNAME;
WndClass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
WndClass.cbWndExtra = 0;
WndClass.cbClsExtra = 0;
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = WndProc;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
WndClass.lpszMenuName = NULL;
return (RegisterClass(&WndClass)!=0);
}
static HWND Create(HINSTANCE hInstance, int nCmdShow){
HWND hWindow = CreateWindowEx(0, MAIN_WINDOW_CLASSNAME, MAIN_WINDOW_CLASSNAME,
WS_OVERLAPPEDWINDOW,
800, 600,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if(!hWindow) return NULL;
c_WinGuiApp.hMainWindow = hWindow;
ShowWindow(hWindow, nCmdShow);
UpdateWindow(hWindow);
return hWindow;
}
/* -------------------------------------------------------------------------------------------------------------- */
/* */
c_WinGuiApp_Main(Register, Create)
+5 -2
View File
@@ -21,6 +21,11 @@
#include <stdlib.h> #include <stdlib.h>
#endif /*INCLUDED_STDLIB_H*/ #endif /*INCLUDED_STDLIB_H*/
#ifndef INCLUDED_STDIO_H
#define INCLUDED_STDIO_H
#include <stdio.h>
#endif /*INCLUDED_STDIO_H*/
#ifndef INCLUDED_TIME_H #ifndef INCLUDED_TIME_H
#define INCLUDED_TIME_H #define INCLUDED_TIME_H
#include <time.h> #include <time.h>
@@ -52,8 +57,6 @@
#endif /*INCLUDED_INTTYPES_H*/ #endif /*INCLUDED_INTTYPES_H*/
/* -------------------------------------------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------------------------------------------- */
/* */ /* */
+11
View File
@@ -0,0 +1,11 @@
#include <c_WinGuiUtil.h>
void c_WinGuiUtil_MoveWindowToScreenCenter(HWND hwnd){
int width = GetSystemMetrics(SM_CXSCREEN);
int height = GetSystemMetrics(SM_CYSCREEN);
RECT Rect;
GetWindowRect(hwnd, &Rect);
int x = (width - (Rect.right - Rect.left))/2;
int y = (height - (Rect.bottom - Rect.top))/2;
SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef INCLUDED_C_WINGUIUTIL_H
#define INCLUDED_C_WINGUIUTIL_H
#ifndef INCLUDED_WINDOWS_H
#define INCLUDED_WINDOWS_H
#include <windows.h>
#endif /*INCLUDED_WINDOWS_H*/
/* -------------------------------------------------------------------------------------------------------------- */
/* */
typedef struct {
int x;
int y;
}c_WinGui_Point_t;
/* -------------------------------------------------------------------------------------------------------------- */
/* */
void c_WinGuiUtil_MoveWindowToScreenCenter(HWND hwnd);
#endif /*INCLUDED_C_WINGUIUTIL_H*/