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