LoginSignup
1
2

More than 5 years have passed since last update.

画像表示を含む自分用アプリの雛形(3) Direct2D化-2, WIC, DirectWriteの利用

Last updated at Posted at 2018-11-15

Direct2Dで画像表示がきれいになってくると、GDI描画の文字の汚さが目についてきます。
描画位置もピクセル単位なので、文字をアニメーションで動かしてみるとカクカクした動きで残念になりますね><

また、PNG, JPGの画像ファイルへの対応もしたいところです。

ということで、前回からの雛形にてDirectWriteとWICを利用することにします。

参照:)
画像表示を含む自分用アプリの雛形(1)
画像表示を含む自分用アプリの雛形(2)

ヘッダーにincudeファイルの設定追加

//// DirectWrite
#include <dwrite.h>
#pragma comment(lib, "dwrite.lib")
//// WIC
#include <wincodec.h>
#pragma comment(lib, "windowscodecs.lib")

グローバル変数の追加

文字を書くためには、ブラシとフォントが必要になります。

IWICImagingFactory    *pWIC_Factory;

IDWriteFactory        *pDWR_Factory;
ID2D1SolidColorBrush  *pSCBrush;
IDWriteTextFormat     *pFont;

Initialize()/Uninitialize()に初期化/終了コードを追加

  DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown **>(&pDWR_Factory));
  CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pWIC_Factory));
...
  pDCRT->CreateSolidColorBrush(D2D1::ColorF(0xff0000, 1.f), &pSCBrush); // ColorF(RGB,A)
  pDWR_Factory->CreateTextFormat(L"Consolas", NULL,
                                 DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
                                 10.f*(96.f/72.f), L"en-US", &pFont);
  pWIC_Factory->Release();
  pDWR_Factory->Release();

WICを使ってPNGファイルをD2DBitmapに変換

      IStream *pIStream;
      HRESULT hr=SHCreateStreamOnFile(ffilename, STGM_READ, &pIStream);
      if(hr==S_OK){
        IWICBitmapDecoder *pDecoder=NULL;
        IWICBitmapFrameDecode *pDecodedFrame=NULL;

        pWIC_Factory->CreateDecoderFromStream(pIStream, NULL, WICDecodeMetadataCacheOnLoad, &pDecoder);
        pDecoder->GetFrame(0, &pDecodedFrame);

        IWICBitmapSource *pWIC_BS=NULL;
        WICPixelFormatGUID WIC_PixelFormat=GUID_WICPixelFormat32bppPBGRA;
        WICConvertBitmapSource(WIC_PixelFormat, pDecodedFrame, &pWIC_BS);

        pDecodedFrame->Release();
        pDecoder->Release();
        pIStream->Release();

        pWIC_BS->GetSize((UINT *)&ImageWidth1, (UINT *)&ImageHeight1);
        pDCRT->CreateBitmapFromWicBitmap(pWIC_BS, &pD2D_Bitmap);
        pWIC_BS->Release();
      }

DirectWriteでテキスト描画

D2DBitmap描画後、DrawTextします。例によってマルチバイト仕様^^;

  case WM_PAINT:
    {
      if(pD2D_Bitmap!=NULL){
        pDCRT->BeginDraw();
        pDCRT->SetTransform(D2D1::Matrix3x2F::Identity());
        pDCRT->DrawBitmap(pD2D_Bitmap, D2D1::RectF(0.f, 0.f, (float)ImageWidth1, (float)ImageHeight1),
                          1.f, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR);
        wchar_t wbuf[256]; MultiByteToWideChar(CP_ACP, 0, "test", -1, wbuf, 256);
        pDCRT->DrawText(wbuf, wcslen(wbuf), pFont, D2D1::RectF(0, 0, 100, 20), pSCBrush);
        pDCRT->EndDraw();
      }
      HDC hdc=GetDC(hDlgImg);
      BitBlt(hdc, 0, 0, FrameWidth, FrameHeight, hFrameDC, 0, 0, SRCCOPY);
      ReleaseDC(hDlgImg, hdc);
      ValidateRect(hDlgImg, NULL);
   }
   break; 
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2