QTools  6.6.0
qwin_gui.c
Go to the documentation of this file.
1 
40 #include "qwin_gui.h"
41 #include <stdlib.h>
42 
43 static HWND l_hWnd;
44 static HDC l_hDC;
45 
46 /*--------------------------------------------------------------------------*/
47 HWND CreateCustDialog(HINSTANCE hInst, int iDlg, HWND hParent,
48  WNDPROC lpfnWndProc, LPCTSTR lpWndClass)
49 {
50  WNDCLASSEX wndclass;
51 
52  wndclass.cbSize = sizeof(wndclass);
53  wndclass.style = CS_HREDRAW | CS_VREDRAW;
54  wndclass.lpfnWndProc = lpfnWndProc;
55  wndclass.cbClsExtra = 0;
56  wndclass.cbWndExtra = DLGWINDOWEXTRA;
57  wndclass.hInstance = hInst;
58  wndclass.hIcon = NULL;
59  wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
60  wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
61  wndclass.lpszMenuName = NULL;
62  wndclass.lpszClassName = lpWndClass;
63  wndclass.hIconSm = NULL;
64 
65  RegisterClassEx(&wndclass);
66 
67  l_hWnd = CreateDialog(hInst, MAKEINTRESOURCE(iDlg), hParent, NULL);
68  l_hDC = GetDC(l_hWnd); /* the DC for the client area of the window */
69 
70  /* NOTE: WM_INITDIALOG provides stimulus for initializing dialog controls.
71  * Dialog box procedures typically use this message to initialize controls
72  * and carry out any other initialization tasks that affect the appearance
73  * of the dialog box.
74  */
75  SendMessage(l_hWnd, WM_INITDIALOG, (WPARAM)0, (LPARAM)0);
76 
77  return l_hWnd;
78 }
79 
80 /*--------------------------------------------------------------------------*/
82  UINT itemID,
83  HBITMAP hBitmapUp, HBITMAP hBitmapDwn,
84  HCURSOR hCursor)
85 {
86  me->itemID = itemID;
87  me->hBitmapUp = hBitmapUp;
88  me->hBitmapDown = hBitmapDwn;
89  me->hCursor = hCursor;
90  me->isDepressed = 0;
91 }
92 /*..........................................................................*/
94  DeleteObject(me->hBitmapUp);
95  DeleteObject(me->hBitmapDown);
96  DeleteObject(me->hCursor);
97 }
98 /*..........................................................................*/
100  OwnerDrawnButton * const me,
101  LPDRAWITEMSTRUCT lpdis)
102 {
104 
105  if ((lpdis->itemAction & ODA_DRAWENTIRE) != 0U) {
106  if (me->hCursor != NULL) {
107  SetClassLongPtr(lpdis->hwndItem,
108  GCLP_HCURSOR, (LONG_PTR)me->hCursor);
109  me->hCursor = NULL; /* mark the cursor set */
110  }
111  DrawBitmap(lpdis->hDC, me->hBitmapUp,
112  lpdis->rcItem.left, lpdis->rcItem.top);
113  me->isDepressed = 0;
114  ret = BTN_PAINTED;
115  }
116  else if ((lpdis->itemAction & ODA_SELECT) != 0U) {
117  if ((lpdis->itemState & ODS_SELECTED) != 0U) {
118  DrawBitmap(lpdis->hDC, me->hBitmapDown,
119  lpdis->rcItem.left, lpdis->rcItem.top);
120  me->isDepressed = !0;
121  ret = BTN_DEPRESSED;
122  }
123  else {
124  /* NOTE: the bitmap for button "UP" look will be
125  * drawn in the ODA_DRAWENTIRE action
126  */
127  me->isDepressed = 0;
128  ret = BTN_RELEASED;
129  }
130  }
131  return ret;
132 }
133 /*..........................................................................*/
134 void OwnerDrawnButton_set(OwnerDrawnButton * const me, int isDepressed) {
135  if (me->isDepressed != isDepressed) {
136  HWND hItem = GetDlgItem(l_hWnd, me->itemID);
137  me->isDepressed = isDepressed;
138  if (isDepressed) {
139  DrawBitmap(GetDC(hItem), me->hBitmapDown, 0, 0);
140  }
141  else {
142  DrawBitmap(GetDC(hItem), me->hBitmapUp, 0, 0);
143  }
144  }
145 }
146 /*..........................................................................*/
148  return me->isDepressed;
149 }
150 
151 /*--------------------------------------------------------------------------*/
153  UINT width, UINT height,
154  UINT itemID, BYTE const bgColor[3])
155 {
156  BITMAPINFO bi24BitInfo;
157  RECT rect;
158 
159  me->src_width = width;
160  me->src_height = height;
161 
162  me->hItem = GetDlgItem(l_hWnd, itemID);
163  me->dst_hDC = GetDC(me->hItem);
164  GetWindowRect(me->hItem, &rect);
165  me->dst_width = rect.right - rect.left;
166  me->dst_height = rect.bottom - rect.top;
167 
168  me->bgColor[0] = bgColor[0];
169  me->bgColor[1] = bgColor[1];
170  me->bgColor[2] = bgColor[2];
171 
172  bi24BitInfo.bmiHeader.biBitCount = 3U*8U; /* 3 RGB bytes */
173  bi24BitInfo.bmiHeader.biCompression = BI_RGB; /* RGB color */
174  bi24BitInfo.bmiHeader.biPlanes = 1U;
175  bi24BitInfo.bmiHeader.biSize = sizeof(bi24BitInfo.bmiHeader);
176  bi24BitInfo.bmiHeader.biWidth = me->src_width;
177  bi24BitInfo.bmiHeader.biHeight = me->src_height;
178 
179  me->src_hDC = CreateCompatibleDC(me->dst_hDC);
180  me->hBitmap = CreateDIBSection(me->src_hDC, &bi24BitInfo, DIB_RGB_COLORS,
181  (void **)&me->bits, 0, 0);
182  SelectObject(me->src_hDC, me->hBitmap);
183 
186 }
187 /*..........................................................................*/
189  DeleteDC(me->src_hDC);
190  DeleteObject(me->hBitmap);
191  OutputDebugString("GraphicDisplay_xtor\n");
192 }
193 /*..........................................................................*/
195  UINT n;
196  BYTE r = me->bgColor[0];
197  BYTE g = me->bgColor[1];
198  BYTE b = me->bgColor[2];
199  BYTE *bits = me->bits;
200 
201  for (n = me->src_width * me->src_height; n != 0U; --n, bits += 3) {
202  bits[0] = b;
203  bits[1] = g;
204  bits[2] = r;
205  }
206 }
207 /*..........................................................................*/
209  StretchBlt(me->dst_hDC, 0, 0, me->dst_width, me->dst_height,
210  me->src_hDC, 0, 0, me->src_width, me->src_height, SRCCOPY);
211 }
212 
213 /* SegmentDisplay ----------------------------------------------------------*/
215  UINT segmentNum, UINT bitmapNum)
216 {
217  UINT n;
218 
219  me->hSegment = (HWND *)calloc(segmentNum, sizeof(HWND));
220  me->segmentNum = segmentNum;
221  for (n = 0U; n < segmentNum; ++n) {
222  me->hSegment[n] = NULL;
223  }
224  me->hBitmap = (HBITMAP *)calloc(bitmapNum, sizeof(HBITMAP));
225  me->bitmapNum = bitmapNum;
226  for (n = 0U; n < bitmapNum; ++n) {
227  me->hBitmap[n] = NULL;
228  }
229 }
230 /*..........................................................................*/
232  UINT n;
233 
234  for (n = 0U; n < me->segmentNum; ++n) {
235  DeleteObject(me->hSegment[n]);
236  }
237  free(me->hSegment);
238 
239  for (n = 0U; n < me->bitmapNum; ++n) {
240  DeleteObject(me->hBitmap[n]);
241  }
242  free(me->hBitmap);
243 }
244 /*..........................................................................*/
246  UINT segmentNum, UINT segmentID)
247 {
248  if (segmentNum < me->segmentNum) {
249  me->hSegment[segmentNum] = GetDlgItem(l_hWnd, segmentID);
250  return me->hSegment[segmentNum] != NULL;
251  }
252  else {
253  return FALSE;
254  }
255 }
256 /*..........................................................................*/
258  UINT bitmapNum, HBITMAP hBitmap)
259 {
260  if ((bitmapNum < me->bitmapNum) && (hBitmap != NULL)) {
261  me->hBitmap[bitmapNum] = hBitmap;
262  return TRUE;
263  }
264  else {
265  return FALSE;
266  }
267 }
268 /*..........................................................................*/
270  UINT segmentNum, UINT bitmapNum)
271 {
272  if ((segmentNum < me->segmentNum) && (bitmapNum < me->bitmapNum)) {
273  SendMessage(me->hSegment[segmentNum], STM_SETIMAGE,
274  IMAGE_BITMAP,
275  (LPARAM)me->hBitmap[bitmapNum]);
276  return TRUE;
277  }
278  else {
279  return FALSE;
280  }
281 }
282 
283 /*--------------------------------------------------------------------------*/
284 /* DrawBitmap() function adapted from the book "Programming Windows" by
285 * Charles Petzold.
286 */
287 void DrawBitmap(HDC hdc, HBITMAP hBitmap,
288  int xStart, int yStart)
289 {
290  BITMAP bm;
291  POINT ptSize, ptOrg;
292  HDC hdcMem = CreateCompatibleDC(hdc);
293 
294  SelectObject(hdcMem, hBitmap);
295  SetMapMode(hdcMem, GetMapMode(hdc));
296 
297  GetObject(hBitmap, sizeof(BITMAP), (LPVOID)&bm);
298  ptSize.x = bm.bmWidth;
299  ptSize.y = bm.bmHeight;
300  DPtoLP(hdc, &ptSize, 1);
301 
302  ptOrg.x = 0;
303  ptOrg.y = 0;
304  DPtoLP(hdcMem, &ptOrg, 1);
305 
306  BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y,
307  hdcMem, ptOrg.x, ptOrg.y, SRCCOPY);
308  DeleteDC(hdcMem);
309 }
310 
311 
GraphicDisplay::hBitmap
HBITMAP hBitmap
Definition: qwin_gui.h:97
OwnerDrawnButton::hBitmapDown
HBITMAP hBitmapDown
Definition: qwin_gui.h:62
SegmentDisplay_initSegment
BOOL SegmentDisplay_initSegment(SegmentDisplay *const me, UINT segmentNum, UINT segmentID)
Definition: qwin_gui.c:245
OwnerDrawnButton::hBitmapUp
HBITMAP hBitmapUp
Definition: qwin_gui.h:61
GraphicDisplay_redraw
void GraphicDisplay_redraw(GraphicDisplay *const me)
Definition: qwin_gui.c:208
l_hDC
static HDC l_hDC
Definition: qwin_gui.c:44
GraphicDisplay::dst_hDC
HDC dst_hDC
Definition: qwin_gui.h:93
BTN_RELEASED
Definition: qwin_gui.h:71
OwnerDrawnButton::hCursor
HCURSOR hCursor
Definition: qwin_gui.h:63
GraphicDisplay::bits
BYTE * bits
Definition: qwin_gui.h:98
SegmentDisplay
Definition: qwin_gui.h:125
SegmentDisplay::hSegment
HWND * hSegment
Definition: qwin_gui.h:126
GraphicDisplay_init
void GraphicDisplay_init(GraphicDisplay *const me, UINT width, UINT height, UINT itemID, BYTE const bgColor[3])
Definition: qwin_gui.c:152
OwnerDrawnButton
Definition: qwin_gui.h:59
OwnerDrawnButton::isDepressed
int isDepressed
Definition: qwin_gui.h:64
OwnerDrawnButtonAction
OwnerDrawnButtonAction
Definition: qwin_gui.h:67
BTN_NOACTION
Definition: qwin_gui.h:68
SegmentDisplay::hBitmap
HBITMAP * hBitmap
Definition: qwin_gui.h:128
CreateCustDialog
HWND CreateCustDialog(HINSTANCE hInst, int iDlg, HWND hParent, WNDPROC lpfnWndProc, LPCTSTR lpWndClass)
Definition: qwin_gui.c:47
GraphicDisplay::src_width
int src_width
Definition: qwin_gui.h:91
GraphicDisplay_xtor
void GraphicDisplay_xtor(GraphicDisplay *const me)
Definition: qwin_gui.c:188
GraphicDisplay::src_hDC
HDC src_hDC
Definition: qwin_gui.h:90
SegmentDisplay_init
void SegmentDisplay_init(SegmentDisplay *const me, UINT segmentNum, UINT bitmapNum)
Definition: qwin_gui.c:214
GraphicDisplay::bgColor
BYTE bgColor[3]
Definition: qwin_gui.h:99
GraphicDisplay::dst_height
int dst_height
Definition: qwin_gui.h:95
GraphicDisplay
Definition: qwin_gui.h:89
BTN_DEPRESSED
Definition: qwin_gui.h:70
OwnerDrawnButton_isDepressed
BOOL OwnerDrawnButton_isDepressed(OwnerDrawnButton const *const me)
Definition: qwin_gui.c:147
SegmentDisplay::segmentNum
UINT segmentNum
Definition: qwin_gui.h:127
OwnerDrawnButton_draw
enum OwnerDrawnButtonAction OwnerDrawnButton_draw(OwnerDrawnButton *const me, LPDRAWITEMSTRUCT lpdis)
Definition: qwin_gui.c:99
SegmentDisplay::bitmapNum
UINT bitmapNum
Definition: qwin_gui.h:129
GraphicDisplay::hItem
HWND hItem
Definition: qwin_gui.h:96
OwnerDrawnButton_init
void OwnerDrawnButton_init(OwnerDrawnButton *const me, UINT itemID, HBITMAP hBitmapUp, HBITMAP hBitmapDwn, HCURSOR hCursor)
Definition: qwin_gui.c:81
OwnerDrawnButton_xtor
void OwnerDrawnButton_xtor(OwnerDrawnButton *const me)
Definition: qwin_gui.c:93
GraphicDisplay::dst_width
int dst_width
Definition: qwin_gui.h:94
qwin_gui.h
QWIN GUI facilities for building realistic embedded front panels.
SegmentDisplay_setSegment
BOOL SegmentDisplay_setSegment(SegmentDisplay *const me, UINT segmentNum, UINT bitmapNum)
Definition: qwin_gui.c:269
SegmentDisplay_initBitmap
BOOL SegmentDisplay_initBitmap(SegmentDisplay *const me, UINT bitmapNum, HBITMAP hBitmap)
Definition: qwin_gui.c:257
GraphicDisplay::src_height
int src_height
Definition: qwin_gui.h:92
GraphicDisplay_clear
void GraphicDisplay_clear(GraphicDisplay *const me)
Definition: qwin_gui.c:194
OwnerDrawnButton_set
void OwnerDrawnButton_set(OwnerDrawnButton *const me, int isDepressed)
Definition: qwin_gui.c:134
DrawBitmap
void DrawBitmap(HDC hdc, HBITMAP hBitmap, int xStart, int yStart)
Definition: qwin_gui.c:287
BTN_PAINTED
Definition: qwin_gui.h:69
SegmentDisplay_xtor
void SegmentDisplay_xtor(SegmentDisplay *const me)
Definition: qwin_gui.c:231
l_hWnd
static HWND l_hWnd
Definition: qwin_gui.c:43
OwnerDrawnButton::itemID
UINT itemID
Definition: qwin_gui.h:60