Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simplest possible C win32 app to draw line?

1,717 views
Skip to first unread message

Dan Kegel

unread,
Jun 16, 2012, 5:03:51 PM6/16/12
to
What is the absolute simplest win32 program that draws a line in a window?

I tried

#include <windows.h>
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg;
HWND hwnd;
HDC hdc;
RECT rect;
WNDCLASS wc;

ZeroMemory(&wc, sizeof(wc));
wc.lpfnWndProc = DefWindowProc;
wc.hInstance = hinst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "Foobar";
RegisterClass(&wc);
hwnd = CreateWindow(
"Foobar",
"Foobar",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
512, 512,
NULL,
NULL,
hinst,
0);
hdc = GetDC(hwnd);
GetClientRect(hwnd, &rect);
FillRect(hdc, &rect, (HBRUSH) (COLOR_WINDOW+1));
MoveToEx(hdc, 100, 100, NULL);
LineTo(hdc, 200, 200);
ReleaseDC(hwnd, hdc);
ShowWindow(hwnd, nShowCmd);
Sleep(5000);
return 0;
}

but the window was not shown. Is the problem that I
don't have a window proc? I was hoping I could just
draw immediately without having to use the event system.

Thanks,
Dan

moos...@gmail.com

unread,
Jun 16, 2013, 1:55:54 AM6/16/13
to
Try all those codes in a WP_PAINT message and create hdc with BeginPaint and Release HDC with EndPaint . this should work !
0 new messages