brent's hut

Use Skia in MFC or Win32 application

What is Skia

Skia is an open source 2D graphics library which provides common APIs that work across a variety of hardware and software platforms. It serves as the graphics engine for Google Chrome and Chrome OS, Android, Mozilla Firefox and Firefox OS, and many other products.

How to download and build Skia

Just follow https://skia.org/user/download, and do it step by step, we will need a static build for use lately.

How to use Skia in a MFC application

1. Create your MFC application

2. Add Skia path to "VC++ Directories" of the project



3. Add Skia library paths to "Additional Library Directories"



4. Add "uafxcw.lib;LIBCMT.lib;opengl32.lib;glu32.lib;skia.lib;sk_app.GLWindowContext.obj;sk_app.GLWindowContext_win.obj" to "Additional Dependencies". Add "uafxcw.lib;LIBCMT.lib;" to "Ignore Specific Default Libraries."



5. In XXXDlg.h:
#include "tools\sk_app\DisplayParams.h"
#include "tools\sk_app\WindowContext.h"

protected:
HICON m_hIcon;
sk_app::DisplayParams m_paras;
std::unique_ptr<sk_app::WindowContext> m_skaContext;

6. In XXXDlg.cpp:

#include "include\core\SkPoint.h"
#include "include\core\SkFont.h"
#include "include\core\SkCanvas.h"
#include "include\core\SkSurface.h"
#include "tools\sk_app\win\WindowContextFactory_win.h"
using namespace sk_app;

BOOL XXXDlg::OnInitDialog()
{
m_skaContext = window_context_factory::MakeGLForWin(m_hWnd, m_paras);
return TRUE;
}

void XXXDlg::OnSize(UINT nType, int cx, int cy)
{
if(m_skaContext)
m_skaContext->resize(cx, cy);
}

void XXXDlg::OnPaint()
{
sk_sp<SkSurface> backbuffer = m_skaContext->getBackbufferSurface();
if (backbuffer) {
SkCanvas* c = backbuffer->getCanvas();
c->clear(SK_ColorWHITE);
SkPaint p(SkColors::kBlack);
c->drawLine(SkPoint::Make(0, 0), SkPoint::Make(m_skaContext->width(), m_skaContext->height()), p);

static const char message[] = "Hello World";

SkFont font;
font.setSubpixel(true);
font.setSize(20);
c->drawSimpleText(message, strlen(message), SkTextEncoding::kUTF8, 0, m_skaContext->height() -2, font, p);
backbuffer->flush();

m_skaContext->swapBuffers();
}
}

7. If everything is OK, the result will be:


posted on 2020-02-07 07:53 brent 阅读(186) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理