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