Qt 5.10 Book Examples
main.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // main.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 
13 // ======================================================================
14 class WinAPI : public QWidget {
15 protected:
16  virtual bool nativeEvent(const QByteArray& baType,
17  void* pmessage,
18  long* result
19  )
20  {
21  QString str1 = "Windows Version = "
22  + QString::number(QSysInfo::WindowsVersion);
23  QString str2 = "Windows Message";
24 
25  WId id = effectiveWinId();
26  HWND hWnd = (HWND)id;
27  MSG* pmsg = reinterpret_cast<MSG*>(pmessage);
28 
29  switch(pmsg->message) {
30  case WM_RBUTTONDOWN:
31  ::MessageBox(hWnd,
32  (const WCHAR*)str1.utf16(),
33  (const WCHAR*)str2.utf16(),
34  MB_OK | MB_ICONEXCLAMATION
35  );
36  break;
37  default:
38  ;
39  }
40 
41  return QWidget::nativeEvent(baType, pmessage, result);
42  }
43 
44  virtual void paintEvent(QPaintEvent*)
45  {
46  WId id = effectiveWinId();
47  HWND hWnd = (HWND)id;
48  HDC hDC = ::GetDC(hWnd);
49  HBRUSH hBrush = ::CreateSolidBrush(RGB(255, 0, 255));
50  HBRUSH hBrushRect = ::CreateSolidBrush(RGB(200, 200, 200));
51  HPEN hPen = ::CreatePen(PS_SOLID, 2, RGB(0, 0, 128));
52  QString str = "GDI Drawing";
53  TEXTMETRIC tm;
54 
55  ::SelectObject(hDC, hBrushRect);
56  ::Rectangle(hDC, 0, 0, width(), height());
57  ::GetTextMetrics(hDC, &tm);
58  ::SelectObject(hDC, hBrush);
59  ::SelectObject(hDC, hPen);
60  ::Ellipse(hDC, 0, 0, width(), height());
61  ::TextOut(hDC,
62  width() / 2 - (tm.tmAveCharWidth * str.length()) / 2,
63  (height() - tm.tmHeight) / 2,
64  (const WCHAR*)str.utf16(),
65  str.length()
66  );
67  ::DeleteObject(hBrushRect);
68  ::DeleteObject(hBrush);
69  ::DeleteObject(hPen);
70  ::ReleaseDC(hWnd, hDC);
71  }
72 
73  virtual void resizeEvent(QResizeEvent* pe)
74  {
75  update();
76  QWidget::resizeEvent(pe);
77  }
78 
79 public:
80  WinAPI(QWidget* pwgt = 0) : QWidget(pwgt)
81  {
82  setAutoFillBackground(false);
83  setAttribute(Qt::WA_PaintOnScreen, true);
84  }
85 
86  QPaintEngine* paintEngine() const
87  {
88  return 0;
89  }
90 };
91 
92 // ----------------------------------------------------------------------
93 int main(int argc, char** argv)
94 {
95  QApplication app(argc, argv);
96  WinAPI winAPI;
97 
98  winAPI.resize(280, 200);
99  winAPI.show();
100 
101  return app.exec();
102 }
QPaintEngine * paintEngine() const
Definition: main.cpp:86
int main(int argc, char **argv)
Definition: main.cpp:15
Definition: main.cpp:14
virtual void resizeEvent(QResizeEvent *pe)
Definition: main.cpp:73
virtual bool nativeEvent(const QByteArray &baType, void *pmessage, long *result)
Definition: main.cpp:16
virtual void paintEvent(QPaintEvent *)
Definition: main.cpp:44
WinAPI(QWidget *pwgt=0)
Definition: main.cpp:80