diff --git a/KeyMouse.t.c b/KeyMouse.t.c new file mode 100644 index 0000000..b3824ae --- /dev/null +++ b/KeyMouse.t.c @@ -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 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) + + diff --git a/c_WinGuiApp.h b/c_WinGuiApp.h index 4b1d7b6..9d14289 100644 --- a/c_WinGuiApp.h +++ b/c_WinGuiApp.h @@ -21,6 +21,11 @@ #include #endif /*INCLUDED_STDLIB_H*/ +#ifndef INCLUDED_STDIO_H +#define INCLUDED_STDIO_H +#include +#endif /*INCLUDED_STDIO_H*/ + #ifndef INCLUDED_TIME_H #define INCLUDED_TIME_H #include @@ -52,8 +57,6 @@ #endif /*INCLUDED_INTTYPES_H*/ - - /* -------------------------------------------------------------------------------------------------------------- */ /* */ diff --git a/c_WinGuiUtil.c b/c_WinGuiUtil.c new file mode 100644 index 0000000..a27eaf4 --- /dev/null +++ b/c_WinGuiUtil.c @@ -0,0 +1,11 @@ +#include + +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); +} diff --git a/c_WinGuiUtil.h b/c_WinGuiUtil.h new file mode 100644 index 0000000..ac57db1 --- /dev/null +++ b/c_WinGuiUtil.h @@ -0,0 +1,25 @@ +#ifndef INCLUDED_C_WINGUIUTIL_H +#define INCLUDED_C_WINGUIUTIL_H + +#ifndef INCLUDED_WINDOWS_H +#define INCLUDED_WINDOWS_H +#include +#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*/