Skip to content

Instantly share code, notes, and snippets.

@FatalCatharsis
Created July 27, 2017 00:38
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 FatalCatharsis/39c5f35ae78ecd5399eebe0fb2491004 to your computer and use it in GitHub Desktop.
Save FatalCatharsis/39c5f35ae78ecd5399eebe0fb2491004 to your computer and use it in GitHub Desktop.
import core.sys.windows.windows;
import std.utf : toUTFz;
import std.stdio: writeln;
int main() {
writeln("start");
try {
WinThing meh = new WinThing();
} catch(Exception e) {
writeln("error");
}
writeln("end");
return 0;
}
class WinThing {
private static string BASE_CLASS = "BaseClass";
private static WinThing*[HWND] winMap;
private HWND hwnd;
static this() {
WNDCLASSEX wc;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = &WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(NULL);
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hIconSm = wc.hIcon;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = cast(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = toUTFz!(const wchar*)(BASE_CLASS);
wc.cbSize = WNDCLASSEX.sizeof;
if(!RegisterClassEx(&wc)) {
writeln("Could not register new windows class.");
return;
}
}
extern(Windows)
static LRESULT WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) nothrow {
WinThing* window = null;
try {
if (msg == WM_CREATE) {
// pointer passed into createWindowEx can be obtained from the create struct
CREATESTRUCT* createStruct = cast(CREATESTRUCT*) lparam;
// store pointer to window in lookup map
window = cast(WinThing*)((*createStruct).lpCreateParams);
winMap[hwnd] = window;
} else {
// all messages, fetch window and invoke instance specific handler
window = winMap[hwnd];
}
} catch(Exception e) {
}
// return (*window).handleMessage(hwnd, msg, wparam, lparam);
return DefWindowProc(hwnd, msg, wparam, lparam);
}
this() {
// pass the pointer to this class to create window ex
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, toUTFz!(const wchar*)(BASE_CLASS), "Test Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 800, NULL, NULL, GetModuleHandle(NULL), cast(void*)this);
if(hwnd == NULL) {
writeln("Could not create window.");
return;
}
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
}
public LRESULT handleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
// instance specific things...
return DefWindowProc(hwnd, msg, wparam, lparam);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment