Files
WinGUI/GUITest/KeyMouse.t.c
T

381 lines
11 KiB
C
Raw Normal View History

2026-07-28 14:17:49 +08:00
#include "c_WinGUI.h"
2025-11-06 01:00:40 +08:00
#include "c_WinGuiUtil.h"
2026-07-28 14:17:49 +08:00
#include <stdio.h>
#include <c_WinApp.h>
2025-11-06 01:00:40 +08:00
/* -------------------------------------------------------------------------------------------------------------- */
/* */
#define MAIN_WINDOW_CLASSNAME "KeyMouse"
/* -------------------------------------------------------------------------------------------------------------- */
/* */
#define DIRECTION_SIZE 100
static char Directions[DIRECTION_SIZE];
static int xVal = 10;
static int yVal = 30;
/* -------------------------------------------------------------------------------------------------------------- */
/* */
2026-07-28 14:17:49 +08:00
c_WinGUI_EventHandlerDef(WM_CREATE, KeyMouse_OnCreate){
2025-11-06 01:00:40 +08:00
c_WinGuiUtil_MoveWindowToScreenCenter(hwnd);
return TRUE;
}
2026-07-28 14:17:49 +08:00
c_WinGUI_EventHandlerDef(WM_DESTROY, KeyMouse_OnDestroy){
2025-11-06 01:00:40 +08:00
PostQuitMessage(0);
}
2026-07-28 14:17:49 +08:00
c_WinGUI_EventHandlerDef(WM_PAINT, KeyMouse_OnPaint){
2025-11-06 01:00:40 +08:00
PAINTSTRUCT PaintStruct;
RECT Rect;
HDC hdc = BeginPaint(hwnd, &PaintStruct);
static char* Message[]={
2026-07-28 14:17:49 +08:00
"WM_CHAR",
"WM_KEY",
"WM_SYSKEY",
"WM_MOUSEMOVE",
"WM_MOUSEDOWN",
"WM_MOUSEUP",
2025-11-06 01:00:40 +08:00
};
2026-07-28 14:17:49 +08:00
2025-11-06 01:00:40 +08:00
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);
}
2026-07-28 14:17:49 +08:00
c_WinGUI_EventHandlerDef(WM_CHAR, KeyMouse_OnChar){
2025-11-06 01:00:40 +08:00
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);
}
2026-07-28 14:17:49 +08:00
c_WinGUI_EventHandlerDef(WM_KEYDOWN, KeyMouse_OnKey){
2025-11-06 01:00:40 +08:00
char s[100];
HDC hdc = GetDC(hwnd);
int size =0;
2026-07-28 14:17:49 +08:00
if(fBool){
2025-11-06 01:00:40 +08:00
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_KEYDOWN ==> vk = %c, fDown = %d, cRepeat = %d, flags = %d "
, vk
2026-07-28 14:17:49 +08:00
, fBool
2025-11-06 01:00:40 +08:00
, cRepeat
, flags
);
}else{
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_KEYUP ==> vk = %c, fDown = %d, cRepeat = %d, flags = %d "
, vk
2026-07-28 14:17:49 +08:00
, fBool
2025-11-06 01:00:40 +08:00
, cRepeat
, flags
);
}
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
TextOut(hdc, xVal, yVal + 40, s, size);
ReleaseDC(hwnd, hdc);
}
2026-07-28 14:17:49 +08:00
c_WinGUI_EventHandlerDef(WM_SYSKEYDOWN, KeyMouse_OnSysKey){
2025-11-06 01:00:40 +08:00
char s[100];
HDC hdc = GetDC(hwnd);
int size = 0;
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
2026-07-28 14:17:49 +08:00
if(fBool){
2025-11-06 01:00:40 +08:00
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_SYSKEYDOWN ==> vk = %d, fDown = %d, cRepeat = %d, flags = %d "
, vk
2026-07-28 14:17:49 +08:00
, fBool
2025-11-06 01:00:40 +08:00
, 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
2026-07-28 14:17:49 +08:00
, fBool
2025-11-06 01:00:40 +08:00
, cRepeat
, flags);
TextOut(hdc, xVal, yVal + 60, s, size);
FORWARD_WM_SYSKEYUP(hwnd, vk, cRepeat, flags, DefWindowProc);
}
ReleaseDC(hwnd, hdc);
}
2026-07-28 14:17:49 +08:00
c_WinGUI_EventHandlerDef(WM_LBUTTONDOWN, KeyMouse_OnLButtonDown){
2025-11-06 01:00:40 +08:00
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);
}
2026-07-28 14:17:49 +08:00
c_WinGUI_EventHandlerDef(WM_LBUTTONUP, KeyMouse_OnLButtonUp){
2025-11-06 01:00:40 +08:00
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);
}
2026-07-28 14:17:49 +08:00
c_WinGUI_EventHandlerDef(WM_MOUSEMOVE, KeyMouse_OnMouseMove){
2025-11-06 01:00:40 +08:00
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);
}
2026-07-28 14:17:49 +08:00
c_WinGUI_EventHandlerDef(WM_RBUTTONDOWN, KeyMouse_OnRButtonDown)
{
2025-11-06 01:06:34 +08:00
char s[100];
HDC hdc = GetDC(hwnd);
int size =0;
if(fDoubleClick){
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_RBUTTONDBLCLK ==> Db = %d, x = %d, y = %d, flags = %d "
, fDoubleClick
, x
, y
, keyFlags
);
c_WinGuiUtil_MoveWindowToScreenCenter(hwnd);
}else{
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_RBUTTONDOWN ==> Db = %d, x = %d, y = %d, flags = %d "
, fDoubleClick
, x
, y
, keyFlags
);
}
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
TextOut(hdc, xVal, yVal + 140, s, size);
ReleaseDC(hwnd, hdc);
}
2026-07-28 14:17:49 +08:00
c_WinGUI_EventHandlerDef(WM_RBUTTONUP, KeyMouse_OnRButtonUp){
2025-11-06 01:06:34 +08:00
char s[100];
HDC hdc = GetDC(hwnd);
int size =0;
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_RBUTTONUP ==> x = %d, y = %d, flags = %d "
, x
, y
, keyFlags
);
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
TextOut(hdc, xVal, yVal + 160, s, size);
ReleaseDC(hwnd, hdc);
}
2026-07-28 14:17:49 +08:00
c_WinGUI_EventHandlerDef(WM_MBUTTONDOWN, KeyMouse_OnMButtonDown) {
char s[100];
HDC hdc = GetDC(hwnd);
int size =0;
if(fDoubleClick){
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_MBUTTONDBLCLK ==> Db = %d, x = %d, y = %d, flags = %d "
, fDoubleClick
, x
, y
, keyFlags
);
}else{
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_MBUTTONDOWN ==> Db = %d, x = %d, y = %d, flags = %d "
, fDoubleClick
, x
, y
, keyFlags
);
}
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
TextOut(hdc, xVal, yVal + 180, s, size);
ReleaseDC(hwnd, hdc);
}
c_WinGUI_EventHandlerDef(WM_MBUTTONUP, KeyMouse_OnMButtonUp){
char s[100];
HDC hdc = GetDC(hwnd);
int size =0;
size = snprintf(s, sizeof(s)/sizeof(s[0])
, "WM_MBUTTONUP ==> x = %d, y = %d, flags = %d "
, x
, y
, keyFlags
);
SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
TextOut(hdc, xVal, yVal + 160, s, size);
ReleaseDC(hwnd, hdc);
}
2025-11-06 01:00:40 +08:00
/* -------------------------------------------------------------------------------------------------------------- */
/* */
2026-07-28 14:17:49 +08:00
c_WinGUI_EventMapBegin(WndProc)
c_WinGUI_EventMap(WM_CREATE, KeyMouse_OnCreate);
c_WinGUI_EventMap(WM_CHAR, KeyMouse_OnChar);
c_WinGUI_EventMap(WM_KEYDOWN, KeyMouse_OnKey);
c_WinGUI_EventMap(WM_KEYUP, KeyMouse_OnKey);
c_WinGUI_EventMap(WM_SYSKEYDOWN, KeyMouse_OnSysKey);
c_WinGUI_EventMap(WM_SYSKEYUP, KeyMouse_OnSysKey);
c_WinGUI_EventMap(WM_MOUSEMOVE, KeyMouse_OnMouseMove);
c_WinGUI_EventMap(WM_LBUTTONDBLCLK, KeyMouse_OnLButtonDown);
c_WinGUI_EventMap(WM_LBUTTONDOWN, KeyMouse_OnLButtonDown);
c_WinGUI_EventMap(WM_LBUTTONUP, KeyMouse_OnLButtonUp);
c_WinGUI_EventMap(WM_DESTROY, KeyMouse_OnDestroy);
c_WinGUI_EventMap(WM_PAINT, KeyMouse_OnPaint);
c_WinGUI_EventMap(WM_RBUTTONDBLCLK, KeyMouse_OnRButtonDown);
c_WinGUI_EventMap(WM_RBUTTONDOWN, KeyMouse_OnRButtonDown);
c_WinGUI_EventMap(WM_RBUTTONUP, KeyMouse_OnRButtonUp);
c_WinGUI_EventMap(WM_MBUTTONDOWN, KeyMouse_OnMButtonDown);
c_WinGUI_EventMap(WM_MBUTTONDBLCLK, KeyMouse_OnMButtonDown);
c_WinGUI_EventMap(WM_MBUTTONUP, KeyMouse_OnMButtonUp);
c_WinGUI_EventMapEnd()
2025-11-06 01:00:40 +08:00
/* -------------------------------------------------------------------------------------------------------------- */
/* */
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,
2025-11-06 01:06:34 +08:00
800, 300,
// CW_USEDEFAULT, CW_USEDEFAULT,
2025-11-06 01:00:40 +08:00
NULL, NULL, hInstance, NULL);
if(!hWindow) return NULL;
ShowWindow(hWindow, nCmdShow);
UpdateWindow(hWindow);
return hWindow;
}
2026-07-28 14:17:49 +08:00
/* ------------------------------------------------------------------------------------------------------------------ */
2025-11-06 01:00:40 +08:00
/* */
2026-07-28 14:17:49 +08:00
c_WinGUI_Main() {
MSG msg;
if (!hPrevInstance) {
if (!Register(hInstance)) {
return FALSE;
}
}
HWND MainHwnd = Create(hInstance, nShowCmd);
if (!MainHwnd) {
return FALSE;
}
c_WinApp_Init(hInstance, MainHwnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
2025-11-06 01:00:40 +08:00
2026-07-28 14:17:49 +08:00
return (int)msg.wParam;
}