71 const int width_ = width % 2 == 0 ? width : width + 1;
72 int base0 = 3*(height - y0 - 1)*width_;
73 int base1 = 3*(height - y1)*width_;
80 for (
int x = x0;
x < x1;
x++)
82 img[base0 + 3*
x + 0] = 255;
83 img[base0 + 3*
x + 1] = 0;
84 img[base0 + 3*
x + 2] = 0;
86 img[base1 + 3*
x + 0] = 255;
87 img[base1 + 3*
x + 1] = 0;
88 img[base1 + 3*
x + 2] = 0;
91 for (
int y = (height - y1);
y < (height - y0 - 1);
y++)
93 img[3*
y*width_ + 3*x0 + 0] = 255;
94 img[3*
y*width_ + 3*x0 + 1] = 0;
95 img[3*
y*width_ + 3*x0 + 2] = 0;
97 img[3*
y*width_ + 3*(x1-1) + 0] = 255;
98 img[3*
y*width_ + 3*(x1-1) + 1] = 0;
99 img[3*
y*width_ + 3*(x1-1) + 2] = 0;
104 #define DEFAULT_CLASS_NAME "MainWindow" 105 #define IMAGE_WINDOW_CLASS_NAME "ImageWindow" 107 LRESULT CALLBACK
WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
109 if (uMsg == WM_CREATE)
112 CREATESTRUCT *cs = (CREATESTRUCT*)lParam;
116 SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) ptr);
127 return ptr->
WindowProc(hWnd, uMsg, wParam, lParam);
129 return DefWindowProc(hWnd, uMsg, wParam, lParam);
158 if (m_quit_count >= m_ref_count)
161 ShowWindow(
m_hWnd, SW_HIDE);
171 unsigned int x = GET_X_LPARAM(lParam);
172 unsigned int y = GET_Y_LPARAM(lParam);
192 if (m_event_callback != NULL)
206 if (m_event_callback != NULL)
220 if (m_event_callback != NULL)
241 unsigned int x = GET_X_LPARAM(lParam);
242 unsigned int y = GET_Y_LPARAM(lParam);
259 unsigned int swap = x0;
265 unsigned int swap = y0;
270 WindowRect.left = x0;
271 WindowRect.right = x1;
273 WindowRect.bottom = y1;
275 InvalidateRect(iw->
m_hWnd, &WindowRect,
false);
285 unsigned int swap = x0;
291 unsigned int swap = y0;
296 WindowRect.left = x0;
297 WindowRect.right = x1;
299 WindowRect.bottom = y1;
301 InvalidateRect(iw->
m_hWnd, &WindowRect,
false);
308 if (m_event_callback != NULL)
310 m_event_callback->MouseMove((
WIDGET_HANDLE)w, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
327 if (m_event_callback != NULL)
342 unsigned int swap = x0;
348 unsigned int swap = y0;
353 m_event_callback->RectSelected((
WIDGET_HANDLE)w, x0, y0, x1, y1);
358 InvalidateRect(iw->
m_hWnd, NULL,
false);
364 if (m_event_callback != NULL)
376 if (m_event_callback != NULL)
388 if (m_event_callback != NULL)
400 if (m_event_callback != NULL)
402 char key[2] = {0, 0};
405 GetKeyboardState(keys);
407 int c = ToAscii((
unsigned int) wParam, MapVirtualKey((
unsigned int) wParam, 0), keys, (
unsigned short*)key, 0);
421 if (m_event_callback != NULL)
423 char key[2] = {0, 0};
426 GetKeyboardState(keys);
428 int c = ToAscii((
unsigned int) wParam, MapVirtualKey((
unsigned int) wParam, 0), keys, (
unsigned short*)key, 0);
440 int idx = LOWORD(wParam);
441 int cmd = HIWORD(wParam);
444 const int nSize = (int) m_widgets.size();
445 if (idx != 0 && idx > 0 && idx <= nSize)
447 w = m_widgets[idx-1];
453 if (m_event_callback != NULL)
462 if (m_event_callback != NULL)
464 BOOL checked = IsDlgButtonChecked(
m_hWnd, idx);
466 m_event_callback->ValueChanged((
WIDGET_HANDLE)w, (checked ? 1 : 0));
473 if (m_event_callback != NULL)
482 if (m_event_callback != NULL)
484 int value = (int) SendMessage(w->
m_hWnd, CB_GETCURSEL, 0, 0);
498 int idx = (int) wParam;
499 NMHDR *nmhdr = (LPNMHDR)lParam;
502 const int nSize = (int) m_widgets.size();
503 if (idx != 0 && idx > 0 && idx <= nSize)
505 w = m_widgets[idx-1];
511 if (m_event_callback != NULL && nmhdr->code == -12 )
513 int value = (int) SendMessage(w->
m_hWnd, TBM_GETPOS, 0, 0);
537 HDC dc = BeginPaint(iw->
m_hWnd, &ps);
548 unsigned int swap = x0;
554 unsigned int swap = y0;
577 return DefWindowProc(hWnd, uMsg, wParam, lParam);
593 WindowRect.right = x +
width;
595 WindowRect.bottom = y +
height;
597 m_hInstance = GetModuleHandle(NULL);
599 if (m_ref_count == 0)
601 wc.style = CS_HREDRAW | CS_VREDRAW;
602 wc.lpfnWndProc = (WNDPROC)
WndProc;
605 wc.hInstance = m_hInstance;
606 wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
607 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
608 wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
609 wc.lpszMenuName = NULL;
612 if (!RegisterClass(&wc))
614 MessageBox(NULL,
"Failed To Register The MainWindow Class.",
"ERROR", MB_OK|MB_ICONEXCLAMATION);
619 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
620 wc.lpfnWndProc = (WNDPROC)
WndProc;
623 wc.hInstance = m_hInstance;
624 wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
625 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
626 wc.hbrBackground = NULL;
627 wc.lpszMenuName = NULL;
630 if (!RegisterClass(&wc))
632 MessageBox(NULL,
"Failed To Register The Image Window Class.",
"ERROR", MB_OK|MB_ICONEXCLAMATION);
639 dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
640 dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
642 AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);
645 if (WindowRect.top < 0)
647 WindowRect.bottom -= WindowRect.top;
650 if (WindowRect.left < 0)
652 WindowRect.right -= WindowRect.left;
657 if (!(
m_hWnd = CreateWindowEx(dwExStyle,
663 WindowRect.left, WindowRect.top,
664 WindowRect.right-WindowRect.left,
665 WindowRect.bottom-WindowRect.top,
671 MessageBox(NULL,
"Couldn't create window.",
"ERROR", MB_OK | MB_ICONEXCLAMATION);
675 m_event_callback = NULL;
678 SetForegroundWindow(
m_hWnd);
684 int i,
c = (int) m_widgets.size();
686 for (i = c - 1; i >= 0; i--)
711 MessageBox(NULL,
"Release Compatible Device Context Failed.",
"SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
717 MessageBox(NULL,
"Release Device Context Failed.",
"SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
726 wglMakeCurrent(NULL, NULL);
730 MessageBox(NULL,
"Release Device Context Failed.",
"SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
736 MessageBox(NULL,
"Release Device Context Failed.",
"SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
743 MessageBox(NULL,
"Could Not Release hWnd.",
"SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
754 MessageBox(NULL,
"Could Not Release hWnd.",
"SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
762 if (m_ref_count == 0 && m_hInstance)
766 MessageBox(NULL,
"Could Not Unregister Class.",
"SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
798 if (!(iw->
m_hWnd = CreateWindowEx(0,
801 WS_CLIPSIBLINGS | WS_CHILD | WS_VISIBLE,
810 MessageBox(NULL,
"Couldn't create image window.",
"ERROR", MB_OK | MB_ICONEXCLAMATION);
815 SetWindowPos(iw->
m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
819 MessageBox(NULL,
"Couldn't create device context." ,
"ERROR", MB_OK | MB_ICONEXCLAMATION);
826 MessageBox(NULL,
"Couldn't create compatible device context." ,
"ERROR", MB_OK | MB_ICONEXCLAMATION);
831 iw->
m_index = (int) m_widgets.size();
832 m_widgets.push_back(iw);
847 if (!(w->
m_hWnd = CreateWindowEx(0,
850 WS_CLIPSIBLINGS | WS_CHILD | WS_VISIBLE,
855 (HMENU)(m_widgets.size()+1),
859 MessageBox(NULL,
"Couldn't create button.",
"ERROR", MB_OK | MB_ICONEXCLAMATION);
866 SetWindowPos(w->
m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
868 w->
m_index = (int) m_widgets.size();
869 m_widgets.push_back(w);
884 if (!(w->
m_hWnd = CreateWindowEx(0,
887 WS_CLIPSIBLINGS | WS_CHILD | WS_VISIBLE,
892 (HMENU)(m_widgets.size()+1),
896 MessageBox(NULL,
"Couldn't create label.",
"ERROR", MB_OK | MB_ICONEXCLAMATION);
903 SetWindowPos(w->
m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
905 w->
m_index = (int) m_widgets.size();
906 m_widgets.push_back(w);
921 if (!(w->
m_hWnd = CreateWindowEx(0,
924 WS_CHILD | WS_VISIBLE | BS_CHECKBOX | BS_AUTOCHECKBOX,
929 (HMENU)(m_widgets.size()+1),
933 MessageBox(NULL,
"Couldn't create check box.",
"ERROR", MB_OK | MB_ICONEXCLAMATION);
940 SetWindowPos(w->
m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
942 w->
m_index = (int) m_widgets.size();
943 m_widgets.push_back(w);
963 if (!(w->
m_hWnd = CreateWindowEx(WS_EX_STATICEDGE,
966 WS_CHILD | WS_VISIBLE | WS_BORDER,
971 (HMENU)(m_widgets.size()+1),
975 MessageBox(NULL,
"Couldn't create text edit.",
"ERROR", MB_OK | MB_ICONEXCLAMATION);
982 SetWindowPos(w->
m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
984 w->
m_index = (int) m_widgets.size();
985 m_widgets.push_back(w);
1000 if (!(w->
m_hWnd = CreateWindowEx(0,
1003 WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS,
1008 (HMENU)(m_widgets.size()+1),
1012 MessageBox(NULL,
"Couldn't create text edit.",
"ERROR", MB_OK | MB_ICONEXCLAMATION);
1019 SendMessage(w->
m_hWnd, TBM_SETRANGE, TRUE, MAKELONG(min_value, max_value));
1020 SendMessage(w->
m_hWnd, TBM_SETPAGESIZE, 0, step);
1021 SendMessage(w->
m_hWnd, TBM_SETTICFREQ, step, 0);
1022 SendMessage(w->
m_hWnd, TBM_SETPOS, TRUE, value);
1024 SetWindowPos(w->
m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
1026 w->
m_index = (int) m_widgets.size();
1027 m_widgets.push_back(w);
1042 if (!(w->
m_hWnd = CreateWindowEx(0,
1045 WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST,
1050 (HMENU)(m_widgets.size()+1),
1054 MessageBox(NULL,
"Couldn't create text edit.",
"ERROR", MB_OK | MB_ICONEXCLAMATION);
1061 for (
int i = 0; i < num_entries; i++)
1063 SendMessage(w->
m_hWnd, CB_ADDSTRING, 0, (LPARAM)entries[i]);
1066 SendMessage(w->
m_hWnd, CB_SETCURSEL, current_entry, 0);
1068 SetWindowPos(w->
m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
1070 w->
m_index = (int) m_widgets.size();
1071 m_widgets.push_back(w);
1089 if (!(glw->
m_hWnd = CreateWindowEx(0,
1092 WS_CLIPSIBLINGS | WS_CHILD | WS_VISIBLE,
1101 MessageBox(NULL,
"Couldn't create image window.",
"ERROR", MB_OK | MB_ICONEXCLAMATION);
1108 SetWindowPos(glw->
m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
1112 MessageBox(NULL,
"Couldn't create device context." ,
"ERROR", MB_OK | MB_ICONEXCLAMATION);
1119 PIXELFORMATDESCRIPTOR pfd = {
1120 sizeof(PIXELFORMATDESCRIPTOR),
1122 PFD_DRAW_TO_WINDOW |
1123 PFD_SUPPORT_OPENGL |
1143 iPixelFormat = ChoosePixelFormat(glw->
m_hDC, &pfd);
1146 BOOL bResult = SetPixelFormat(glw->
m_hDC, iPixelFormat, &pfd);
1149 MessageBox(NULL,
"ERROR: SetPixelFormat failed",
"ERROR", MB_OK | MB_ICONEXCLAMATION);
1159 MessageBox(NULL,
"ERROR: couldn't allocate HGLRC",
"ERROR", MB_OK | MB_ICONEXCLAMATION);
1168 glw->
m_index = (int) m_widgets.size();
1169 m_widgets.push_back(glw);
1183 Button_GetText(w->
m_hWnd, text, len);
1187 Static_GetText(w->
m_hWnd, text, len);
1191 GetWindowText(w->
m_hWnd, text, len);
1204 Button_SetText(w->
m_hWnd, text);
1208 Static_SetText(w->
m_hWnd, text);
1212 SetWindowText(w->
m_hWnd, text);
1235 DeleteObject(iw->
m_hBmp);
1249 bminfo.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
1250 bminfo.bmiHeader.biWidth = pImage->
width;
1251 bminfo.bmiHeader.biHeight = pImage->
height;
1252 bminfo.bmiHeader.biPlanes = 1;
1253 bminfo.bmiHeader.biBitCount = 24;
1254 bminfo.bmiHeader.biCompression = BI_RGB;
1255 bminfo.bmiHeader.biSizeImage = 0;
1256 bminfo.bmiHeader.biXPelsPerMeter = 1;
1257 bminfo.bmiHeader.biYPelsPerMeter = 1;
1258 bminfo.bmiHeader.biClrUsed = 0;
1259 bminfo.bmiHeader.biClrImportant = 0;
1261 iw->
m_hBmp = CreateDIBSection(iw->
m_hDC, &bminfo, DIB_RGB_COLORS, (
void**)&iw->
m_pPixels, NULL, 0);
1265 MessageBox(NULL,
"Couldn't create DIB." ,
"ERROR", MB_OK | MB_ICONEXCLAMATION);
1272 MessageBox(NULL,
"Couldn't Select Object." ,
"ERROR", MB_OK | MB_ICONEXCLAMATION);
1278 int padding = 4 - (3 * pImage->
width) % 4;
1279 if (padding == 4) padding = 0;
1286 int padding = 4 - (3 *
width) % 4;
1287 if (padding == 4) padding = 0;
1291 const int out_offset = 6 * width + padding;
1293 unsigned char *out = iw->
m_buffer + (3 * width + padding) * (height - 1);
1294 unsigned char *
in = pImage->
pixels;
1296 for (
int y = 0;
y <
height;
y++, out -= out_offset)
1298 for (
int x = 0;
x <
width;
x++, out += 3, in++)
1300 const unsigned int c = *
in;
1309 const int out_offset = 6 * width + padding;
1311 unsigned char *out = iw->
m_buffer + (3 * width + padding) * (height - 1);
1312 unsigned char *
in = pImage->
pixels;
1314 for (
int y = 0;
y <
height;
y++, out -= out_offset)
1316 for (
int x = 0;
x <
width;
x++, out += 3, in += 3)
1328 InvalidateRect(iw->
m_hWnd, NULL,
false);
1344 value = (checked ? 1 : 0);
1350 value = (int) SendMessage(w->
m_hWnd, TBM_GETPOS, 0, 0);
1356 value = (int) SendMessage(w->
m_hWnd, CB_GETCURSEL, 0, 0);
1378 SendMessage(w->
m_hWnd, TBM_SETPOS, TRUE, value);
1384 SendMessage(w->
m_hWnd, CB_SETCURSEL, value, 0);
1400 SwapBuffers(glw->
m_hDC);
1432 ShowWindow(
m_hWnd, SW_SHOW);
1433 InvalidateRect(
m_hWnd, NULL,
false);
1437 ShowWindow(w->
m_hWnd, SW_SHOW);
1438 InvalidateRect(w->
m_hWnd, NULL,
false);
1447 ShowWindow(
m_hWnd, SW_HIDE);
1451 ShowWindow(w->
m_hWnd, SW_HIDE);
1461 SetWindowPos(w->
m_hWnd, HWND_TOP, 0, 0, width, height, SWP_NOMOVE);
1465 SetWindowPos(
m_hWnd, HWND_TOP, 0, 0, width, height, SWP_NOMOVE);
1473 result |= GetAsyncKeyState(VK_SHIFT) < 0 ?
IVT_SHIFT_KEY : 0;
1474 result |= GetAsyncKeyState(VK_MENU) < 0 ?
IVT_ALT_KEY : 0;
1482 int i,
c = (int) m_widgets.size();
1484 for (i = 0; i <
c; i++)
WIDGET_HANDLE AddTextEdit(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
bool SetImage(WIDGET_HANDLE widget, const CByteImage *pImage)
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
WIDGET_HANDLE AddLabel(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
Win32Widget * FindWidget(HWND handle)
int width
The width of the image in pixels.
#define IMAGE_WINDOW_CLASS_NAME
CWin32MainWindow(int x, int y, int width, int height, const char *title)
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
WIDGET_HANDLE AddComboBox(int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, WIDGET_HANDLE parent=0)
bool GetText(WIDGET_HANDLE widget, char *text, int len)
void Hide(WIDGET_HANDLE widget=0)
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
bool MakeCurrentGLWidget(WIDGET_HANDLE widget)
unsigned char * pixels
The pointer to the the pixels.
bool SetValue(WIDGET_HANDLE widget, int value)
bool SetText(WIDGET_HANDLE widget, const char *text)
bool SwapBuffersGLWidget(WIDGET_HANDLE widget)
void DrawRect(unsigned char *img, int width, int height, int x0, int y0, int x1, int y1)
GLsizei const GLfloat * value
WIDGET_HANDLE AddCheckBox(int x, int y, int width, int height, const char *text, bool checked, WIDGET_HANDLE parent=0)
int height
The height of the image in pixels.
bool GetValue(WIDGET_HANDLE widget, int &value)
GLenum GLsizei GLsizei height
#define IVT_MIDDLE_BUTTON
ImageType type
The type of the image.
WIDGET_HANDLE AddImage(int x, int y, int width, int height, WIDGET_HANDLE parent=0)
int GetModifierKeyState()
#define DEFAULT_CLASS_NAME
void SetSize(int width, int height, WIDGET_HANDLE widget=0)
void Show(WIDGET_HANDLE widget=0)
WIDGET_HANDLE AddButton(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
WIDGET_HANDLE AddSlider(int x, int y, int width, int height, int min_value, int max_value, int step, int value, WIDGET_HANDLE parent=0)
GLubyte GLubyte GLubyte GLubyte w
WIDGET_HANDLE AddGLWidget(int x, int y, int width, int height, WIDGET_HANDLE parent=0)