#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)