#define WIN32_LEAN_AND_MEAN #include #include #include const wchar_t *windowClass = L"cairo_window"; const double alpha = 1.0; void cairoPaint(HDC hdc) { cairo_surface_t *paintSurface = cairo_win32_surface_create(hdc); cairo_t *cr = cairo_create(paintSurface); cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, alpha); cairo_rectangle(cr, 10.0, 10.0, 50.0, 50.0); cairo_fill(cr); cairo_destroy(cr); cairo_surface_destroy(paintSurface); } LRESULT CALLBACK windowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); cairoPaint(hdc); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } void registerWindowClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = windowProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = NULL; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = windowClass; wcex.hIconSm = NULL; RegisterClassEx(&wcex); } BOOL initInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd = CreateWindow( windowClass, L"Cairo Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 400, 200, NULL, NULL, hInstance, NULL); if (!hWnd) return FALSE; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { registerWindowClass(hInstance); if (!initInstance(hInstance, nCmdShow)) return FALSE; MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; }