WhyCan Forum(哇酷开发者社区)

我们能随心所欲"有问题百度(谷歌)一下", 这得感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn

您尚未登录。

#1 2020-01-01 22:57:28

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

找到一个 win32 gdi & cairo 绘图引擎的 demo

离线

#2 2020-01-01 23:06:38

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: 找到一个 win32 gdi & cairo 绘图引擎的 demo

原作者是用 dev-cpp 编译的, 但是我下载了这个版本,链接失败: https://whycan.cn/t_2789.html#p22710

下载地址: Dev-Cpp 5.11 TDM-GCC 4.9.2 Setup.exe

用这个版本会链接错误.

离线

#3 2020-01-01 23:19:53

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: 找到一个 win32 gdi & cairo 绘图引擎的 demo

QQ截图20200101232025.png

/**
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)

离线

#4 2020-01-01 23:24:57

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: 找到一个 win32 gdi & cairo 绘图引擎的 demo

离线

#5 2020-01-01 23:30:36

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: 找到一个 win32 gdi & cairo 绘图引擎的 demo

QQ截图20200101232910.png

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);
}

http://zetcode.com/gfx/cairo/shapesfills/

离线

#6 2020-01-01 23:45:47

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: 找到一个 win32 gdi & cairo 绘图引擎的 demo

QQ截图20200101234503.png

cairo_curve_to(cr, x1, y1, x2, y2, x3, y3);

https://www.cairographics.org/manual/cairo-Paths.html

https://www.cairographics.org/samples/

试了一下, 这里面的 demo 都可以跑, 包括贝塞尔曲线.

https://www.jianshu.com/p/38aaef672e8f

离线

#7 2020-01-03 14:59:55

红白机
会员
注册时间: 2020-01-02
累计积分: 133

Re: 找到一个 win32 gdi & cairo 绘图引擎的 demo

居然发现有同道中人 ^_^

线条抗锯齿关闭:
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);

字体抗锯齿关闭:
cairo_font_options_set_antialias(options, CAIRO_ANTIALIAS_NONE);

离线

#8 2020-01-03 15:38:59

红白机
会员
注册时间: 2020-01-02
累计积分: 133

Re: 找到一个 win32 gdi & cairo 绘图引擎的 demo

	double xc = 20.0;
	double yc = 20.0;
	double radius = 200.0;
	double angle1 = 0.0  * (M_PI / 180.0);  /* angles are specified */
	double angle2 = 45.0 * (M_PI / 180.0);  /* in radians           */

	cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);

	cairo_set_line_width(cr, 1.0);
	cairo_arc(cr, xc, yc, radius, angle1, angle2);
	cairo_stroke(cr);

	/* draw helping lines */
	cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.6);
	cairo_set_line_width(cr, 1.0);

	cairo_arc(cr, xc, yc, 3.0, 0, 2 * M_PI);
	cairo_fill(cr);

	cairo_arc(cr, xc, yc, radius, angle1, angle1);
	cairo_line_to(cr, xc, yc);

	cairo_arc(cr, xc, yc, radius, angle2, angle2);
	cairo_line_to(cr, xc, yc);
	//cairo_fill(cr);

	cairo_stroke(cr);
	cairo_fill(cr);

2020-01-03_153811.png

参考1 https://www.cairographics.org/samples/
参考2 https://www.cairographics.org/tutorial/

最近编辑记录 红白机 (2020-01-03 15:40:26)

离线

#9 2020-01-03 17:05:24

红白机
会员
注册时间: 2020-01-02
累计积分: 133

Re: 找到一个 win32 gdi & cairo 绘图引擎的 demo

2020-01-03_170314.png


	//画图区域限制在 圆心(128,128) 半径36.8 的圆内
	cairo_arc(cr, 128.0, 128.0, 36.8, 0, 2 * M_PI);
	cairo_clip(cr);

	//填充一个矩形区域
	cairo_new_path(cr);  	
	cairo_rectangle(cr, 0, 0, 256, 256);
	cairo_set_source_rgba(cr, 0.3, 0.3, 0.3, 0.1);
	cairo_fill(cr);

	//画 x
	cairo_set_source_rgba(cr, 0, 1, 0, 0.5);
	cairo_move_to(cr, 0, 0);
	cairo_line_to(cr, 256, 256);
	cairo_move_to(cr, 256, 0);
	cairo_line_to(cr, 0, 256);
	cairo_set_line_width(cr, 10.0);
	cairo_stroke(cr);

参考1 https://www.cairographics.org/samples/
参考2 https://www.cairographics.org/tutorial/
参考3 https://github.com/apachecn/zetcode-zh/
参考4 https://github.com/apachecn/zetcode-zh/blob/master/docs/graph/30.md

最近编辑记录 红白机 (2020-01-03 17:05:58)

离线

页脚

工信部备案:粤ICP备20025096号-1 Powered by FluxBB