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
![](/images/cppblog_com/aqazero/SkiaMfc.include.png)
3. Add Skia library paths to "Additional Library Directories"
![](/images/cppblog_com/aqazero/SkiaMfc.libDir.png)
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."
![](/images/cppblog_com/aqazero/SkiaMfc.libAdd.png)
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:
![](/images/cppblog_com/aqazero/SkiaMfc.result.png)