Skip to content

Instantly share code, notes, and snippets.

@maxdeliso
Created July 8, 2012 00:53
Show Gist options
  • Save maxdeliso/3068819 to your computer and use it in GitHub Desktop.
Save maxdeliso/3068819 to your computer and use it in GitHub Desktop.
xCon
#include <Windows.h>
#include <string>
#include <iostream>
#include "xConWindow.h"
static void parseCmdline(LPCSTR cmdline) {
std::string cmd = std::string(cmdline);
bool switchPresent = false;
for(unsigned int i = 0; i < cmd.length(); ++i) {
if(isalpha(cmd[i]) && switchPresent ) {
const char cchar = tolower(cmd[i]);
switch(cchar) {
case 'v':
case 'd':
AllocConsole();
break;
default:
break;
}
} else switchPresent = (cmd[i] == '-');
}
}
int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
(void)hPrevInstance;
parseCmdline(lpCmdLine);
return xCon::xConMessageHandler(xCon::xConWindow(nCmdShow, hInstance)).run();
}
#include <Windows.h>
#include <iostream>
#include "xConWindow.h"
#include "xConMessageHandler.h"
namespace xCon {
xConMessageHandler::xConMessageHandler(const xConWindow& xWinRef) : xWin(xWinRef) {
}
int xConMessageHandler::run() {
BOOL bRet;
MSG msg;
while( (bRet = GetMessage( &msg, xWin.hwnd, 0, 0 )) != 0) {
if (bRet == -1) {
DWORD err = GetLastError();
std::cerr << "GetMessage failed: " << err << std::endl;
if(err == ERROR_INVALID_WINDOW_HANDLE) {
}
} else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return EXIT_SUCCESS;
}
}
#pragma once
#include <Windows.h>
#include <vector>
#include "xConWindow.h"
namespace xCon{
class xConWindow;
class xConMessageHandler {
const xConWindow& xWin;
public:
xConMessageHandler(const xConWindow& xWin);
int run();
};
}
#include <Windows.h>
#include <iostream>
#include "xConWindow.h"
namespace xCon {
const LPWSTR xConWindow::xConClass = TEXT("xConClass");
const LPWSTR xConWindow::xConMenu = TEXT("xConMenu");
const LPWSTR xConWindow::xConTitle = TEXT("xCon");
const UINT xConWindow::xConStyle = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
const DWORD xConWindow::xConStyleEx = WS_EX_OVERLAPPEDWINDOW | WS_EX_RIGHTSCROLLBAR;
BOOL xConWindow::registerClass() {
WNDCLASSEX wcx;
wcx.cbSize = sizeof(wcx);
wcx.style = xConStyle;
wcx.lpfnWndProc = xConProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = inst;
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = NULL;
wcx.lpszMenuName = NULL;
wcx.lpszClassName = xConClass;
wcx.hIconSm = NULL;
if(RegisterClassEx(&wcx)) {
std::cerr << "registered window class ok\n";
return TRUE;
} else {
std::cerr << "couldn't register window class\n";
return FALSE;
}
}
BOOL xConWindow::createWindow() {
hwnd = CreateWindowEx(
xConStyleEx,
xConClass,
xConTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
(HWND) NULL,
(HMENU) NULL,
inst,
(LPVOID) this);
if(hwnd != NULL) {
std::cerr << "created window\n";
return TRUE;
} else {
DWORD errCode = GetLastError();
std::cerr << "window creation failed with errcode: " << errCode << std::endl;
return FALSE;
}
}
LRESULT CALLBACK xConWindow::xConProc(HWND hwnd, UINT uint, WPARAM wparam, LPARAM lparam) {
static xConWindow* thisWindow = NULL;
switch(uint) {
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hwnd, &ps);
if(ps.fErase) {
/* TODO: draw background */
}
/* TODO: draw the foreground */
EndPaint(hwnd, &ps);
return 0L;
case WM_CREATE:
thisWindow = reinterpret_cast<xConWindow*> (reinterpret_cast<CREATESTRUCT*>(lparam)->lpCreateParams);
std::cout << "in window procedure with handle to xWindow: " << thisWindow << std::endl;
return 0;
case WM_CLOSE:
PostQuitMessage(0);
std::cout << "caught close event, so posted a quit message to the message handler\n";
return 0;
default:
std::cout << "sending unhandled message " << uint << " to default window proc\n";
return DefWindowProc(hwnd, uint, wparam, lparam);
}
}
xConWindow::xConWindow(int nCmdShow, HINSTANCE inst ) {
this->inst = inst;
if( registerClass() && createWindow() ) {
ShowWindow(hwnd, nCmdShow);
if( UpdateWindow(hwnd) ) {
std::cout << "updated window\n";
}
} else {
std::cerr << "initialization failed";
}
}
}
#pragma once
#include <Windows.h>
#include "xConMessageHandler.h"
namespace xCon{
class xConMessageHandler;
class xConWindow {
friend class xConMessageHandler;
static const LPWSTR xConClass;
static const LPWSTR xConMenu;
static const LPWSTR xConTitle;
static const UINT xConStyle;
static const DWORD xConStyleEx;
HINSTANCE inst;
HWND hwnd;
BOOL registerClass();
BOOL createWindow();
public:
static LRESULT CALLBACK xConProc(HWND, UINT, WPARAM, LPARAM);
xConWindow(int nCmdShow, HINSTANCE inst);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment