1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#include <windows.h>
#include <conio.h>
#define COLOR_RED RGB(255, 0, 0 )
#define COLOR_BLUE RGB(5, 5, 255 )
#define COLOR_GREEN RGB(0, 255, 0 )
#define COLOR_PURPLE RGB(255, 0, 255)
#define COLOR_YELLOW RGB(255, 255, 0)
#define COLOR_BLACK RGB(0, 0, 0 )
HBRUSH g_hbrBack;
HPEN g_hpenFore;
POINTS g_oldPoint;
BOOL do_mouse_move = FALSE;
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
void wmMouseMove(HWND, WPARAM, LPARAM);
void Do_Draw(HDC, WPARAM, LPARAM);
void wmLButtonDown(HWND, WPARAM, LPARAM);
void wmLButtonUp(HWND, WPARAM, LPARAM);
void wmKeyDown(HWND, WPARAM, LPARAM);
void wmLButtonDblClk(HWND, WPARAM, LPARAM);
char szClassName[] = "MouseTrack";
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) {
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; // Use the default background colour (grey)
if (!RegisterClassEx (&wincl)) {
return 0;
}
// Create the Window
hwnd = CreateWindowEx (0, szClassName, "Mouse Tracker", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL,
hThisInstance, NULL);
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0)) {
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
// This function is called by DispatchMessage()
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wp, LPARAM lp) {
switch (message) {
case WM_MOUSEMOVE:
wmMouseMove(hwnd, wp, lp);
break;
case WM_LBUTTONDOWN:
wmLButtonDown(hwnd, wp, lp);
break;
case WM_LBUTTONUP:
wmLButtonUp(hwnd, wp, lp);
break;
case WM_DESTROY:
PostQuitMessage(0); // Send WM_QUIT
break;
case WM_KEYDOWN:
wmKeyDown(hwnd, wp, lp);
break;
default:
return DefWindowProc(hwnd, message, wp, lp);
}
return 0;
}
void wmLButtonDown(HWND hwnd, WPARAM wp, LPARAM lp) {
g_oldPoint=MAKEPOINTS(lp);
SetCapture(hwnd);
}
void wmLButtonUp(HWND hwnd, WPARAM wp, LPARAM lp) {
ReleaseCapture();
}
void wmKeyDown (HWND hwnd, WPARAM wp, LPARAM lp) {
switch (wp) {
case VK_ESCAPE:
PostMessage(hwnd, WM_QUIT, 0, 0);
break;
case 'R': case 'G': case 'B': case 'P': case 'Y': {
COLORREF color;
DeleteObject (g_hpenFore);
switch (wp) {
case 'R':
color=COLOR_RED;
break;
case 'G':
color=COLOR_GREEN;
break;
case 'B':
color=COLOR_BLUE;
break;
case 'P':
color=COLOR_PURPLE;
break;
case 'Y':
color=COLOR_YELLOW;
break;
default:
color=COLOR_BLACK;
break;
g_hpenFore=CreatePen(PS_SOLID, 7, color);
}
break;
}
}
}
void wmMouseMove(HWND hwnd, WPARAM wp, LPARAM lp) {
POINTS p;
HDC hdc;
if (wp!=MK_LBUTTON) return;
p=MAKEPOINTS(lp);
hdc=GetDC(hwnd);
SelectObject(hdc, g_hpenFore);
MoveToEx(hdc, g_oldPoint.x, g_oldPoint.y, 0);
LineTo(hdc, p.x, p.y);
g_oldPoint=p;
ReleaseDC(hwnd, hdc);
}
|