Skip to content

Instantly share code, notes, and snippets.

@opsJson
Last active February 24, 2022 22:27
Show Gist options
  • Select an option

  • Save opsJson/c956f6f8990b9497de84858d5f9053ad to your computer and use it in GitHub Desktop.

Select an option

Save opsJson/c956f6f8990b9497de84858d5f9053ad to your computer and use it in GitHub Desktop.
Easy interface for drawing Windows controls.
#ifndef EASY_WIN32_CONTROLS_H
#define EASY_WIN32_CONTROLS_H
// EXAMPLE:
// https://ibb.co/GRHmv5s
// case WM_CREATE: {
// startWindow(5, 5, hwnd);
//
// newWindow(STATIC, "Edit Example:", 0, 0, 0);
// newWindow(EDIT, "Any text", 200, 0, 0);
//
// newLineWindow();
// offsetWindow(0, 10);
//
// newWindow(BUTTON, "Click Me!", 100, 100, (HMENU)1);
//
// newLineWindow();
// offsetWindow(0, 10);
//
// newWindow(RADIO, "foo", 80, 0, 0);
// newWindow(RADIO, "bar", 80, 0, 0);
// newWindow(CHECKBOX, "foo bar", 80, 0, 0);
// break;
// }
#include <windows.h>
enum {
STATIC,
EDIT,
BUTTON,
CHECKBOX,
RADIO
};
size_t _EasyWindows_Curret_X = 0;
size_t _EasyWindows_Curret_Y = 0;
size_t _EasyWindows_Fist_X = 0;
size_t _EasyWindows_Last_Height = 0;
size_t _EasyWindows_Sum_Height = 0;
HWND _EasyWindows_HWND = 0;
LPSTR _EasyWindows_TYPE = 0;
DWORD _EasyWindows_STYLE = 0;
DWORD _EasyWindows_Custom_STYLE = 0;
void startWindow(size_t x, size_t y, HWND hwnd) {
_EasyWindows_Sum_Height = 0;
_EasyWindows_Fist_X = x;
_EasyWindows_Curret_X = x;
_EasyWindows_Curret_Y = y;
_EasyWindows_HWND = hwnd;
}
POINT getposWindow() {
POINT pos;
pos.x = _EasyWindows_Curret_X;
pos.y = _EasyWindows_Curret_Y;
return pos;
}
void offsetWindow(size_t xOffset, size_t yOffset) {
_EasyWindows_Curret_X += xOffset;
_EasyWindows_Curret_Y += yOffset;
}
void newLineWindow() {
_EasyWindows_Curret_X = _EasyWindows_Fist_X;
_EasyWindows_Curret_Y += _EasyWindows_Last_Height;
_EasyWindows_Sum_Height += _EasyWindows_Curret_Y;
}
void styleWindow(DWORD style) {
_EasyWindows_Custom_STYLE = style;
}
static void _styleWindow(DWORD style) {
_EasyWindows_STYLE = WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL | style;
}
HWND newWindow(DWORD type, LPSTR text, size_t width, size_t height, HMENU menu) {
DWORD style = 0;
//set class and style
if (type == EDIT) {
_EasyWindows_TYPE = "EDIT";
style |= WS_BORDER;
style |= WS_TABSTOP;
}
else if (type == STATIC) {
_EasyWindows_TYPE = "STATIC";
}
else if (type == BUTTON) {
_EasyWindows_TYPE = "BUTTON";
style |= WS_TABSTOP;
}
else if (type == CHECKBOX) {
_EasyWindows_TYPE = "BUTTON";
style |= BS_AUTOCHECKBOX;
style |= WS_TABSTOP;
}
else if (type == RADIO) {
_EasyWindows_TYPE = "BUTTON";
style |= BS_AUTORADIOBUTTON;
style |= WS_TABSTOP;
}
_styleWindow(style | _EasyWindows_Custom_STYLE);
//auto size
if (width == 0) width = strlen(text) * 9;
if (height == 0) height = 20;
//create window
HWND child = CreateWindow(_EasyWindows_TYPE, text, _EasyWindows_STYLE,
_EasyWindows_Curret_X, _EasyWindows_Curret_Y,
width, height,
_EasyWindows_HWND,
menu, (HINSTANCE)GetWindowLongPtr(_EasyWindows_HWND, GWLP_HINSTANCE), NULL);
//auto x
_EasyWindows_Curret_X += width;
_EasyWindows_Last_Height = height;
return child;
}
#define newWindow(type, text, width, height, menu) \
newWindow(type, text, width, height, (HMENU)menu)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment