1 #include <windows.h>
2 #include <stdio.h>
3 #include "resource.h"
4
5 static int CaretWidth = 2;
6 static int CaretHeight = 16;
7 static int CharWidth = 10;
8 static int CharHeight = 16;
9 static HBITMAP CaretBitmap;
10
11 ULONG __cdecl DbgPrint(IN PCH Format, IN ...);
12
13 LRESULT WINAPI MainWndProc(HWND, UINT, WPARAM, LPARAM);
14
15 int WINAPI
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)16 WinMain(HINSTANCE hInstance,
17 HINSTANCE hPrevInstance,
18 LPSTR lpszCmdLine,
19 int nCmdShow)
20 {
21 WNDCLASS wc;
22 MSG msg;
23 HWND hWnd;
24
25 CaretBitmap = LoadBitmap(hInstance, (LPCTSTR)IDB_CARET);
26
27 wc.lpszClassName = "CaretTestClass";
28 wc.lpfnWndProc = MainWndProc;
29 wc.style = CS_VREDRAW | CS_HREDRAW;
30 wc.hInstance = hInstance;
31 wc.hIcon = LoadIcon(NULL, (LPCTSTR)IDI_APPLICATION);
32 wc.hCursor = LoadCursor(NULL, (LPCTSTR)IDC_ARROW);
33 wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
34 wc.lpszMenuName = NULL;
35 wc.cbClsExtra = 0;
36 wc.cbWndExtra = 0;
37 if (RegisterClass(&wc) == 0)
38 {
39 fprintf(stderr, "RegisterClass failed (last error 0x%lX)\n",
40 GetLastError());
41 return(1);
42 }
43
44 hWnd = CreateWindow(wc.lpszClassName,
45 "Caret Test",
46 WS_OVERLAPPEDWINDOW,
47 0,
48 0,
49 200,
50 250,
51 NULL,
52 NULL,
53 hInstance,
54 NULL);
55 if (hWnd == NULL)
56 {
57 fprintf(stderr, "CreateWindow failed (last error 0x%lX)\n",
58 GetLastError());
59 return(1);
60 }
61
62 ShowWindow(hWnd, nCmdShow);
63
64 while(GetMessage(&msg, NULL, 0, 0))
65 {
66 TranslateMessage(&msg);
67 DispatchMessage(&msg);
68 }
69
70 return msg.wParam;
71 }
72
MainWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)73 LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
74 {
75 POINT pt;
76 switch(msg)
77 {
78 case WM_ACTIVATE:
79 switch(LOWORD(wParam))
80 {
81 case WA_ACTIVE:
82 case WA_CLICKACTIVE:
83 if(!ShowCaret(hWnd))
84 DbgPrint("ShowCaret(0x%x)\n", hWnd);
85 break;
86 case WA_INACTIVE:
87 if(!HideCaret(hWnd))
88 DbgPrint("HideCaret(0x%x)\n", hWnd);
89 break;
90 }
91 break;
92
93 case WM_KEYDOWN:
94 if(!GetCaretPos(&pt))
95 {
96 DbgPrint("GetCaretPos() failed!\n");
97 break;
98 }
99 switch(wParam)
100 {
101 case VK_LEFT:
102 pt.x -= CharWidth;
103 break;
104 case VK_UP:
105 pt.y -= CharHeight;
106 break;
107 case VK_RIGHT:
108 pt.x += CharWidth;
109 break;
110 case VK_DOWN:
111 pt.y += CharHeight;
112 break;
113 }
114 if(!SetCaretPos(pt.x, pt.y))
115 DbgPrint("SetCaretPos() failed!\n");
116 break;
117
118 case WM_RBUTTONDOWN:
119 if(!CreateCaret(hWnd, CaretBitmap, 0, 0))
120 DbgPrint("CreateCaret() for window 0x%x failed!\n", hWnd);
121 else
122 if(!ShowCaret(hWnd))
123 DbgPrint("ShowCaret(0x%x)\n", hWnd);
124 break;
125
126 case WM_LBUTTONDOWN:
127 if(!CreateCaret(hWnd, (HBITMAP)0, CaretWidth, CaretHeight))
128 DbgPrint("CreateCaret() for window 0x%x failed!\n", hWnd);
129 else
130 if(!ShowCaret(hWnd))
131 DbgPrint("ShowCaret(0x%x)\n", hWnd);
132 break;
133
134 case WM_CREATE:
135 if(!CreateCaret(hWnd, (HBITMAP)0, CaretWidth, CaretHeight))
136 DbgPrint("CreateCaret() for window 0x%x failed!\n", hWnd);
137 else
138 if(!SetCaretPos(1, 1))
139 DbgPrint("SetCaretPos(%i, %i) failed!\n", 1, 1);
140 break;
141
142 case WM_DESTROY:
143 if(!DestroyCaret())
144 DbgPrint("DestroyCaret() failed!\n");
145 PostQuitMessage(0);
146 break;
147
148 default:
149 return DefWindowProc(hWnd, msg, wParam, lParam);
150 }
151 return 0;
152 }
153