Skip to content

Instantly share code, notes, and snippets.

@dbechrd
Created February 28, 2020 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbechrd/773b84f492bde1af520a5bf402e8d2ba to your computer and use it in GitHub Desktop.
Save dbechrd/773b84f492bde1af520a5bf402e8d2ba to your computer and use it in GitHub Desktop.
#include "Winning.h"
#include <sstream>
LRESULT CALLBACK WindowProc(
_In_ HWND hwnd,
_In_ UINT msg,
_In_ WPARAM wparam,
_In_ LPARAM lparam)
{
switch (msg) {
case WM_CLOSE: {
PostQuitMessage(0);
break;
} case WM_KEYDOWN: {
bool repeat = lparam & 0x40000000;
switch (wparam) {
case VK_SPACE: {
if (repeat) {
OutputDebugString(L"Space repeat\n");
} else {
OutputDebugString(L"Space pressed\n");
}
break;
}
}
break;
} case WM_KEYUP: {
switch (wparam) {
case VK_SPACE: {
OutputDebugString(L"Space released\n");
break;
}
}
break;
} case WM_CHAR: {
char c = (char)wparam;
break;
} case WM_LBUTTONDOWN: {
POINTS *p = (POINTS *)&lparam;
std::wostringstream oss;
oss << "LMB pressed @ x: " << p->x << ", y: " << p->y << "\n";
OutputDebugString(oss.str().c_str());
break;
} case WM_LBUTTONUP: {
POINTS *p = (POINTS *)&lparam;
std::wostringstream oss;
oss << "LMB released @ x: " << p->x << ", y: " << p->y << "\n";
OutputDebugString(oss.str().c_str());
break;
}
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
HWND create_window(HINSTANCE hinstance, const WCHAR *title, int width, int height)
{
static WNDCLASSEX wc = { 0 };
if (!wc.cbSize) {
wc.cbSize = sizeof(wc);
wc.style = CS_OWNDC;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinstance;
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = L"MainWindow";
wc.hIconSm = nullptr;
RegisterClassEx(&wc);
}
RECT rect;
rect.left = (1920 - width) / 2;
rect.top = (1080 - height) / 2;
rect.right = width + rect.left;
rect.bottom = height + rect.top;
DWORD style = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
AdjustWindowRect(&rect, style, FALSE);
HWND hwnd = CreateWindowEx(0, wc.lpszClassName, title, style, rect.left,
rect.top, width, height, nullptr, nullptr, hinstance, nullptr);
return hwnd;
}
int CALLBACK WinMain (
_In_ HINSTANCE hinstance,
_In_opt_ HINSTANCE hprevinstance,
_In_ LPSTR cmdline,
_In_ int showcmd)
{
HWND hwnd = create_window(hinstance, L"Title", 1600, 900);
ShowWindow(hwnd, SW_SHOW);
MSG msg = { 0 };
BOOL result;
while (result = GetMessage(&msg, nullptr, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DestroyWindow(hwnd);
return result ? result : (int)msg.wParam;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment