原帖地址: http://windrealm.org/cairo-gdi/
demo & sdk 下载地址: http://windrealm.org/cairo-gdi/cairo-gdi-demo.zip
本站下载地址: cairo-gdi-demo.zip
离线
原作者是用 dev-cpp 编译的, 但是我下载了这个版本,链接失败: https://whycan.cn/t_2789.html#p22710
下载地址: Dev-Cpp 5.11 TDM-GCC 4.9.2 Setup.exe
用这个版本会链接错误.
离线
/**
cairo-gdi-demo.cpp
Demonstrates how to get Cairo Graphics working with the Windows API and GDI.
Author: Andrew Lim
Email: danteshamest@gmail.com
Site: windrealm.com
*/
#include <windows.h>
#include <cmath>
#include "cairo-win32.h"
/**
Gradient demonstration.
Taken from http://cairographics.org/samples/
*/
void gradientExample( cairo_t* cr ) {
cairo_pattern_t *pat;
pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, 256.0);
cairo_pattern_add_color_stop_rgba (pat, 1, 0, 0, 0, 1);
cairo_pattern_add_color_stop_rgba (pat, 0, 1, 1, 1, 1);
cairo_rectangle (cr, 0, 0, 256, 256);
cairo_set_source (cr, pat);
cairo_fill (cr);
cairo_pattern_destroy (pat);
pat = cairo_pattern_create_radial (115.2, 102.4, 25.6,
102.4, 102.4, 128.0);
cairo_pattern_add_color_stop_rgba (pat, 0, 1, 1, 1, 1);
cairo_pattern_add_color_stop_rgba (pat, 1, 0, 0, 0, 1);
cairo_set_source (cr, pat);
cairo_arc (cr, 128.0, 128.0, 76.8, 0.0, 2 * 3.1415);
cairo_fill (cr);
cairo_pattern_destroy (pat);
}
/**
Changes the dimensions of a window's client area.
*/
void SetClientSize( HWND hwnd, int clientWidth, int clientHeight ) {
if ( IsWindow( hwnd ) ) {
DWORD dwStyle = GetWindowLongPtr( hwnd, GWL_STYLE ) ;
DWORD dwExStyle = GetWindowLongPtr( hwnd, GWL_EXSTYLE ) ;
HMENU menu = GetMenu( hwnd ) ;
RECT rc = { 0, 0, clientWidth, clientHeight } ;
AdjustWindowRectEx( &rc, dwStyle, menu ? TRUE : FALSE, dwExStyle );
SetWindowPos( hwnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top,
SWP_NOZORDER | SWP_NOMOVE ) ;
}
}
/**
Handles WM_PAINT.
*/
LRESULT onPaint( HWND hwnd, WPARAM wParam, LPARAM lParam ) {
PAINTSTRUCT ps ;
HDC hdc = BeginPaint( hwnd, &ps );
// Create the cairo surface and context.
cairo_surface_t *surface = cairo_win32_surface_create (hdc);
cairo_t *cr = cairo_create (surface);
// Draw on the cairo context.
gradientExample( cr );
// Cleanup.
cairo_destroy (cr);
cairo_surface_destroy (surface);
EndPaint( hwnd, &ps );
return 0 ;
}
/**
Handles WM_CLOSE.
*/
LRESULT onClose( HWND hwnd, WPARAM wParam, LPARAM lParam ) {
PostQuitMessage( 0 );
return 0 ;
}
/**
Handles our window's messages.
*/
LRESULT CALLBACK WndProc( HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam ) {
switch(msg) {
case WM_PAINT: return onPaint( hwnd, wParam, lParam );
case WM_CLOSE: return onClose( hwnd, wParam, lParam );
default: return DefWindowProc(hwnd,msg,wParam,lParam);
}
}
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrev, LPSTR args, int nShow ) {
MSG msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT( "CairoGdiWndClass" );
wc.hInstance = hInst ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
HWND hwnd = CreateWindow( wc.lpszClassName,TEXT("Cairo & GDI Demo"),
WS_OVERLAPPEDWINDOW, 0,0,256,256,0,0,hInst,0);
SetClientSize( hwnd, 256, 256 );
ShowWindow( hwnd, SW_SHOWNORMAL );
while( GetMessage(&msg,0,0,0) > 0 ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
费了九牛二虎之力才搞定的VC2017版本: cairo-gdi-demo.7z
最近编辑记录 win32prog (2020-01-01 23:20:52)
离线
dll 导出 lib 的方法: https://blog.csdn.net/wangzhen209/article/details/50764208
离线
static void do_drawing(cairo_t *cr)
{
cairo_set_source_rgb(cr, 0.6, 0.6, 0.6);
cairo_set_line_width(cr, 1);
cairo_rectangle(cr, 20, 20, 120, 80);
cairo_rectangle(cr, 180, 20, 80, 80);
cairo_stroke_preserve(cr);
cairo_fill(cr);
cairo_arc(cr, 330, 60, 40, 0, 2*M_PI);
cairo_stroke_preserve(cr);
cairo_fill(cr);
cairo_arc(cr, 90, 160, 40, M_PI/4, M_PI);
cairo_close_path(cr);
cairo_stroke_preserve(cr);
cairo_fill(cr);
cairo_translate(cr, 220, 180);
cairo_scale(cr, 1, 0.7);
cairo_arc(cr, 0, 0, 50, 0, 2*M_PI);
cairo_stroke_preserve(cr);
cairo_fill(cr);
}
离线
cairo_curve_to(cr, x1, y1, x2, y2, x3, y3);
https://www.cairographics.org/manual/cairo-Paths.html
https://www.cairographics.org/samples/
试了一下, 这里面的 demo 都可以跑, 包括贝塞尔曲线.
离线