qwindow.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <stdio.h>
  2. #include "qwindow.h"
  3. #include "qapplication.h"
  4. #define MAX_WINDOW_HANDLES 128
  5. int QWindow::wndCounter = 0;
  6. QWindow *regwnd[MAX_WINDOW_HANDLES];
  7. int numregwnd = 0;
  8. QWindow::QWindow(const QString& caption, QWidget *parent, const char *name, int wflags)
  9. : QWidget(parent, name)
  10. {
  11. mycaption = caption;
  12. if ((wflags & WFLAG_NOWINDOW) == 0) {
  13. WNDCLASS wc;
  14. memset(&wc, 0, sizeof(WNDCLASS));
  15. wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  16. wc.lpfnWndProc = windowProc;
  17. wc.hInstance = qApp->hInstance;
  18. wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
  19. sprintf(wndName, "MainWClass%d", ++wndCounter);
  20. wc.lpszClassName = wndName;
  21. wc.lpszMenuName = NULL;
  22. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  23. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  24. if (!RegisterClass(&wc)) {
  25. fprintf(stderr, "cannot register window\n");
  26. exit(1);
  27. }
  28. hwnd = CreateWindowEx(
  29. 0, // no extended styles
  30. wndName, // class name
  31. caption.c_str(), // window name
  32. WS_OVERLAPPEDWINDOW, // style
  33. CW_USEDEFAULT, // geometry
  34. CW_USEDEFAULT,
  35. CW_USEDEFAULT,
  36. CW_USEDEFAULT,
  37. (HWND)NULL, // no parent or owner window
  38. (HMENU)NULL, // class menu used
  39. qApp->hInstance, // instance handle
  40. NULL); // no window creation data
  41. if (hwnd == (HWND)0) {
  42. fprintf(stderr, "cannot create window\n");
  43. exit(1);
  44. }
  45. registerWindowHandle(hwnd, this);
  46. }
  47. }
  48. QWindow::~QWindow()
  49. {
  50. unregisterWindowHandle(hwnd);
  51. }
  52. void QWindow::registerWindowHandle(HWND hwnd, QWindow *w)
  53. {
  54. for (int i = 0; i < numregwnd; ++i)
  55. if (!regwnd[i]) {
  56. regwnd[i] = w;
  57. return;
  58. }
  59. if (numregwnd >= MAX_WINDOW_HANDLES)
  60. fprintf(stderr, "warning: maximum number of window handles exceed\n");
  61. else
  62. regwnd[numregwnd++] = w;
  63. }
  64. void QWindow::unregisterWindowHandle(HWND hwnd)
  65. {
  66. for (int i = 0; i < numregwnd; ++i)
  67. if (regwnd[i]->hwnd == hwnd) {
  68. regwnd[i] = 0;
  69. break;
  70. }
  71. }
  72. QWindow *QWindow::findWindowByHandle(HWND hwnd)
  73. {
  74. for (int i = 0; i < numregwnd; ++i)
  75. if (regwnd[i] && regwnd[i]->hwnd == hwnd)
  76. return regwnd[i];
  77. return 0;
  78. }
  79. void QWindow::show()
  80. {
  81. ShowWindow(hwnd, SW_SHOW);
  82. }
  83. void QWindow::move(int x, int y)
  84. {
  85. QWidget::move(x, y);
  86. MoveWindow(hwnd, x, y, mywidth, myheight, TRUE);
  87. }
  88. void QWindow::resize(int width, int height)
  89. {
  90. QWidget::resize(width, height);
  91. MoveWindow(hwnd, myx, myy, width, height, TRUE);
  92. }
  93. void QWindow::setGeometry(int x, int y, int width, int height)
  94. {
  95. QWidget::setGeometry(x, y, width, height);
  96. MoveWindow(hwnd, x, y, width, height, TRUE);
  97. }
  98. void QWindow::setCaption(const QString& caption)
  99. {
  100. QWidget::setCaption(caption);
  101. SetWindowText(hwnd, caption.c_str());
  102. }
  103. LRESULT CALLBACK QWindow::windowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  104. {
  105. QWindow *w = QWindow::findWindowByHandle(hwnd);
  106. if (w)
  107. switch (uMsg) {
  108. case WM_MOVE:
  109. w->myx = (int)LOWORD(lParam);
  110. w->myy = (int)HIWORD(lParam);
  111. printf("window %p \"%s\": new pos %d %d\n", w, w->myname, w->myx, w->myy);
  112. break;
  113. case WM_SIZE:
  114. w->mywidth = (int)LOWORD(lParam);
  115. w->myheight = (int)HIWORD(lParam);
  116. printf("window %p \"%s\": new size %d %d\n", w, w->myname, w->mywidth, w->myheight);
  117. break;
  118. case WM_DESTROY:
  119. if (w->isTopLevelWidget())
  120. PostQuitMessage(0);
  121. break;
  122. }
  123. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  124. }