116d4f17f58b46592abe284c7c940d821f930600
[reactos.git] / rosapps / tests / accelerator / accelerator.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #define ID_ACCEL1 0x100
6 #define ID_ACCEL2 0x101
7 #define ID_ACCEL3 0x102
8 #define ID_ACCEL4 0x103
9
10 /*
11 * {fVirt, key, cmd}
12 * fVirt |= FVIRTKEY | FCONTROL | FALT | FSHIFT
13 */
14 //static HFONT tf;
15 static ACCEL Accelerators[4] = {
16 { FVIRTKEY, VK_A, ID_ACCEL1},
17 { FVIRTKEY | FSHIFT, VK_A, ID_ACCEL2},
18 { FVIRTKEY | FCONTROL, VK_A, ID_ACCEL3},
19 { FVIRTKEY | FALT, VK_A, ID_ACCEL4}};
20 static HACCEL hAcceleratorTable;
21 static char Event[200];
22
23 LRESULT WINAPI MainWndProc(HWND, UINT, WPARAM, LPARAM);
24
25 int WINAPI
26 WinMain(HINSTANCE hInstance,
27 HINSTANCE hPrevInstance,
28 LPSTR lpszCmdLine,
29 int nCmdShow)
30 {
31 WNDCLASS wc;
32 MSG msg;
33 HWND hWnd;
34
35 wc.lpszClassName = "AcceleratorTest";
36 wc.lpfnWndProc = MainWndProc;
37 wc.style = CS_VREDRAW | CS_HREDRAW;
38 wc.hInstance = hInstance;
39 wc.hIcon = LoadIcon(NULL, (LPCTSTR)IDI_APPLICATION);
40 wc.hCursor = LoadCursor(NULL, (LPCTSTR)IDC_ARROW);
41 wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
42 wc.lpszMenuName = NULL;
43 wc.cbClsExtra = 0;
44 wc.cbWndExtra = 0;
45 if (RegisterClass(&wc) == 0)
46 {
47 fprintf(stderr, "RegisterClass failed (last error 0x%lX)\n",
48 GetLastError());
49 return(1);
50 }
51
52 hWnd = CreateWindow("AcceleratorTest",
53 "Accelerator Test",
54 WS_OVERLAPPEDWINDOW,
55 0,
56 0,
57 CW_USEDEFAULT,
58 CW_USEDEFAULT,
59 NULL,
60 NULL,
61 hInstance,
62 NULL);
63 if (hWnd == NULL)
64 {
65 fprintf(stderr, "CreateWindow failed (last error 0x%lX)\n",
66 GetLastError());
67 return(1);
68 }
69
70 /*tf = CreateFontA(14, 0, 0, TA_BASELINE, FW_NORMAL, FALSE, FALSE, FALSE,
71 ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
72 DEFAULT_QUALITY, FIXED_PITCH|FF_DONTCARE, "Timmons");*/
73
74 Event[0] = 0;
75
76 ShowWindow(hWnd, nCmdShow);
77
78 hAcceleratorTable = CreateAcceleratorTable(Accelerators,
79 sizeof(Accelerators)/sizeof(Accelerators[1]));
80 if (hAcceleratorTable == NULL)
81 {
82 fprintf(stderr, "CreateAcceleratorTable failed (last error 0x%lX)\n",
83 GetLastError());
84 return(1);
85 }
86
87 while(GetMessage(&msg, NULL, 0, 0))
88 {
89 if (!TranslateAccelerator(hWnd, hAcceleratorTable, &msg))
90 {
91 TranslateMessage(&msg);
92 DispatchMessage(&msg);
93 }
94 }
95
96 if (!DestroyAcceleratorTable(hAcceleratorTable))
97 {
98 fprintf(stderr, "DestroyAcceleratorTable failed (last error 0x%lX)\n",
99 GetLastError());
100 return(1);
101 }
102
103 //DeleteObject(tf);
104
105 return msg.wParam;
106 }
107
108 LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
109 {
110 PAINTSTRUCT ps;
111 HDC hDC;
112 char buf[200];
113
114 switch(msg)
115 {
116 case WM_PAINT:
117 hDC = BeginPaint(hWnd, &ps);
118 //SelectObject(hDC, tf);
119 sprintf(buf, "Event: '%s'", Event);
120 TextOut(hDC, 10, 10, buf, strlen(buf));
121 EndPaint(hWnd, &ps);
122 break;
123
124 case WM_DESTROY:
125 PostQuitMessage(0);
126 break;
127
128 case WM_COMMAND:
129
130 switch (LOWORD(wParam))
131 {
132 case ID_ACCEL1:
133 strcpy(Event, "A");
134 break;
135
136 case ID_ACCEL2:
137 strcpy(Event, "SHIFT+A");
138 break;
139
140 case ID_ACCEL3:
141 strcpy(Event, "CTRL+A");
142 break;
143
144 case ID_ACCEL4:
145 strcpy(Event, "ALT+A");
146 break;
147
148 default:
149 sprintf(Event, "%d", LOWORD(wParam));
150 break;
151 }
152
153 InvalidateRect(hWnd, NULL, TRUE);
154 UpdateWindow(hWnd);
155 break;
156
157 default:
158 return DefWindowProc(hWnd, msg, wParam, lParam);
159 }
160 return 0;
161 }