Skip to content

Instantly share code, notes, and snippets.

@PBfordev
Last active August 5, 2021 14:21
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 PBfordev/498381e200dd402360c6c637ca8f7d64 to your computer and use it in GitHub Desktop.
Save PBfordev/498381e200dd402360c6c637ca8f7d64 to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <tchar.h>
void FatalError(LPCTSTR errorMessage)
{
MessageBox(NULL, errorMessage, _T("Fatal error"), MB_OK | MB_ICONERROR);
exit(-1);
}
void Paint(HWND hWnd)
{
const DWORD penStyle = PS_GEOMETRIC | PS_DOT | PS_JOIN_ROUND | PS_ENDCAP_ROUND;
const DWORD penWidth = 1;
PAINTSTRUCT ps;
HDC dc;
HPEN pen;
LOGBRUSH logBrush{0};
RECT clipRect;
logBrush.lbColor = RGB(0, 0, 0);
logBrush.lbStyle = BS_SOLID;
logBrush.lbHatch = 0;
pen = ExtCreatePen(penStyle, penWidth, &logBrush, 0, NULL);
if ( !pen )
FatalError(_T("Could not create the pen."));
dc = BeginPaint(hWnd, &ps);
GetClipBox(dc, &clipRect);
if ( SelectObject(dc, pen) == HGDI_ERROR )
FatalError(_T("Could not select the pen."));
const int lineLength = 150000;
const int x = 20;
const int y = 20;
const int yGap = 50;
const int numLines = 2;
for ( int i = 0; i < numLines; ++i )
{
MoveToEx(dc, x, y + (i * yGap), NULL);
LineTo(dc, x + lineLength, y + (i * yGap));
}
EndPaint(hWnd, &ps);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch ( message )
{
case WM_PAINT:
Paint(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
// create the app window
LPCTSTR className = _T("SampleFrame");
WNDCLASSEX wcex{0};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszClassName = className;
if ( !RegisterClassEx(&wcex) )
FatalError(_T("Could not register the class for the application window."));
HWND hFrame = CreateWindow(className, _T("Sample Frame"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if ( !hFrame )
FatalError(_T("Could not create the application window."));
ShowWindow(hFrame, nCmdShow);
UpdateWindow(hFrame);
// run the message pump
MSG msg;
while ( GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment