Skip to content

Instantly share code, notes, and snippets.

@llamadonica
Created December 20, 2012 14:49
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save llamadonica/4345686 to your computer and use it in GitHub Desktop.
/*
* This example code is placed in the public domain
*
* Author: Owen Taylor <otaylor at redhat.com>, Red Hat Inc.
*/
#include <cairo-win32.h>
#define TITLE TEXT("Cairo test")
#define BORDER_WIDTH 75.
#define SNIPPET_WIDTH 300.
#define SNIPPET_HEIGHT 300.
#define FONT_SIZE 24
static void
on_paint (HDC hdc)
{
cairo_surface_t *surface;
cairo_t *cr;
double line_width;
cairo_font_extents_t font_extents;
surface = cairo_win32_surface_create (hdc);
cr = cairo_create (surface);
line_width = cairo_get_line_width (cr);
/* Draw a box bordering the snippet */
cairo_rectangle (cr,
BORDER_WIDTH - line_width / 2, BORDER_WIDTH - line_width / 2,
SNIPPET_WIDTH + line_width, SNIPPET_WIDTH + line_width);
cairo_stroke (cr);
/* And some text */
cairo_select_font_face (cr, "Arial", CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, FONT_SIZE);
cairo_font_extents (cr, &font_extents);
cairo_move_to (cr,
BORDER_WIDTH,
BORDER_WIDTH + SNIPPET_WIDTH + font_extents.ascent);
cairo_show_text (cr, "This is some example text!");
cairo_destroy (cr);
cairo_surface_destroy (surface);
}
/* The WinMain and window procedure are loosely based on a example
* from the Microsoft documentation.
*/
LRESULT CALLBACK
WndProc (HWND window,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
PAINTSTRUCT paint_struct;
HDC dc;
switch(message) {
case WM_CHAR:
switch (wParam) {
case 'q':
case 'Q':
PostQuitMessage (0);
return 0;
break;
}
break;
case WM_PAINT:
dc = BeginPaint (window, &paint_struct);
on_paint (dc);
EndPaint (window, &paint_struct);
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return 0;
default:
;
}
return DefWindowProc (window, message, wParam, lParam);
}
#define WINDOW_STYLE WS_OVERLAPPEDWINDOW & ~(WS_MAXIMIZEBOX | WS_THICKFRAME)
INT WINAPI
WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR lpCmdLine,
INT iCmdShow)
{
HWND window;
MSG message;
WNDCLASS window_class;
RECT rect;
window_class.style = CS_HREDRAW | CS_VREDRAW;
window_class.lpfnWndProc = WndProc;
window_class.cbClsExtra = 0;
window_class.cbWndExtra = 0;
window_class.hInstance = hInstance;
window_class.hIcon = LoadIcon (NULL, IDI_APPLICATION);
window_class.hCursor = LoadCursor (NULL, IDC_ARROW);
window_class.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
window_class.lpszMenuName = NULL;
window_class.lpszClassName = TITLE;
RegisterClass (&window_class);
/* Compute the window size to give us the desired client area */
rect.left = 0;
rect.top = 0;
rect.right = SNIPPET_WIDTH + 2 * BORDER_WIDTH;
rect.bottom = SNIPPET_WIDTH + 2 * BORDER_WIDTH;
AdjustWindowRect (&rect, WINDOW_STYLE, FALSE /* no menu */);
window = CreateWindow (TITLE, /* Class name */
TITLE, /* Window name */
WINDOW_STYLE,
CW_USEDEFAULT, CW_USEDEFAULT, /* initial position */
rect.right - rect.left, rect.bottom - rect.top, /* initial size */
NULL, /* Parent */
NULL, /* Menu */
hInstance,
NULL); /* WM_CREATE lpParam */
ShowWindow (window, iCmdShow);
UpdateWindow (window);
while (GetMessage (&message, NULL, 0, 0)) {
TranslateMessage (&message);
DispatchMessage (&message);
}
return message.wParam;
}
/************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment