OSDN Git Service

アイコンの動的書き換えを行ってみた。
[wintimer/wintimer.git] / wintimer / icon.cpp
index 01522bf..bdf925c 100644 (file)
 */
 
 #include "StdAfx.h"
+#include "sf_windows.h"
 #include "icon.h"
 
 namespace sf {
-icon::icon()
-{
-}
-icon::~icon()
-{
-}
+
+  icon::icon(icon_holder& ic)
+  {
+    icon_.reset(::CopyIcon(ic.get()));
+    analyze();
+  }
+
+  icon::icon(icon_holder&& ic)
+  {
+    std::swap(ic,icon_);
+    analyze();
+  }
+
+  icon::icon(bitmap_holder& bmp_color,int width,int height) 
+    : width_(width),height_(height)
+  {
+    bitmap_holder hmb(::CreateBitmap(width,height,1,1,NULL));
+    BITMAP bmp;
+    ::GetObjectW(bmp_color.get(),sizeof(BITMAP),&bmp);
+    bits_per_pixel_ = bmp.bmBitsPixel;
+
+    ICONINFO ii;
+    ii.fIcon = TRUE;
+    ii.xHotspot = 0;
+    ii.yHotspot = 0;
+    ii.hbmMask = hmb.get();
+    ii.hbmColor = bmp_color.get();
+    icon_.reset(::CreateIconIndirect(&ii));
+  }
+
+  icon::icon(ID2D1BitmapPtr& ptr)
+  {
+    ID2D1FactoryPtr factory;
+    ptr->GetFactory(&factory);
+
+    D2D1_SIZE_U size(ptr->GetPixelSize());
+    // \83r\83b\83g\83}\83b\83v\83w\83b\83_\82Ì\83Z\83b\83g\83A\83b\83v
+    BITMAPV5HEADER bi = {0};
+    bi.bV5Size           = sizeof(BITMAPV5HEADER);
+    bi.bV5Width           = size.width;
+    bi.bV5Height          = size.height;
+    bi.bV5Planes = 1;
+    bi.bV5BitCount = 32;
+    bi.bV5Compression = BI_BITFIELDS;
+    bi.bV5RedMask   =  0x00FF0000;
+    bi.bV5GreenMask =  0x0000FF00;
+    bi.bV5BlueMask  =  0x000000FF;
+    bi.bV5AlphaMask =  0xFF000000; 
+
+    // \83f\83X\83N\83g\83b\83vHDC\82Ì\8eæ\93¾
+    get_dc dc(NULL);
+    
+    // DIB\83Z\83N\83V\83\87\83\93\82Ì\8dì\90¬
+    void *bits;// \93¾\82ç\82ê\82é\83r\83b\83g\83}\83b\83v
+    bitmap_holder bmp(
+      ::CreateDIBSection(
+        dc.get(),reinterpret_cast<BITMAPINFO *>(&bi),DIB_RGB_COLORS,&bits,NULL,0));
+    { 
+      // \8cÝ\8a·DC\82Ì\8dì\90¬
+      compatible_dc cdc(dc.get());
+      {
+        // \95`\89æ\90æ\82Ö\82Ì\90Ø\82è\91Ö\82¦
+        select_object s(cdc.get(),bmp.get());
+
+        // DC\8cÝ\8a·\83\8c\83\93\83_\81[\83^\81[\83Q\83b\83g\82Ì\83Z\83b\83g\83A\83b\83v
+        D2D1_RENDER_TARGET_PROPERTIES 
+          props = D2D1::RenderTargetProperties(
+          D2D1_RENDER_TARGET_TYPE_DEFAULT,
+          D2D1::PixelFormat(
+              DXGI_FORMAT_B8G8R8A8_UNORM,
+              D2D1_ALPHA_MODE_PREMULTIPLIED),
+          0,
+          0,
+          D2D1_RENDER_TARGET_USAGE_NONE,
+          D2D1_FEATURE_LEVEL_DEFAULT
+        );
+
+        ID2D1DCRenderTargetPtr dcr;
+        throw_if_err<>()(factory->CreateDCRenderTarget(&props,&dcr));
+        RECT rect = {0,0,size.width,size.height};
+        // \8cÝ\8a·DC\82Ö\82Ì\83o\83C\83\93\83h
+        throw_if_err<>()(dcr->BindDC(cdc.get(),&rect));
+        dcr->DrawBitmap(ptr);
+      }
+    }
+    icon(bmp,size.width,size.height);
+  };
+
+
+  icon::icon(boost::filesystem3::wpath& path)
+  {
+    icon_.reset(
+      reinterpret_cast<HICON>(
+        LoadImageW(NULL,path.native().c_str(),IMAGE_ICON,0,0,LR_DEFAULTSIZE | LR_LOADFROMFILE)));
+  }
+
+  icon::~icon()
+  {
+
+  }
+
+  void icon::analyze()
+  {
+    ::ICONINFOEXW info;
+    ::GetIconInfoExW(icon_.get(),&info);
+    BITMAP bmp;
+    ::GetObjectW(info.hbmColor,sizeof(BITMAP),&bmp);
+    width_ = bmp.bmWidth;
+    height_ = bmp.bmHeight;
+    bits_per_pixel_ = bmp.bmBitsPixel;
+  }
+
+  icon& icon::operator= (icon& i)
+  {
+    BOOST_ASSERT(icon_ != i.icon_);
+    if(icon_ == i.icon_) return *this;
+    icon_.reset(::CopyIcon(i.icon_.get()));
+    width_ = i.width_;
+    height_ = i.height_;
+    bits_per_pixel_ = i.bits_per_pixel_;
+    return *this;
+  }
+
+  //icon_ptr icon::create_icon()
+  //{
+  //  // \83r\83b\83g\83}\83b\83v\83w\83b\83_\82Ì\83Z\83b\83g\83A\83b\83v
+  //  BITMAPV5HEADER bi = {0};
+  //  bi.bV5Size           = sizeof(BITMAPV5HEADER);
+  //  bi.bV5Width           = width_;
+  //  bi.bV5Height          = height_;
+  //  bi.bV5Planes = 1;
+  //  bi.bV5BitCount = 32;
+  //  bi.bV5Compression = BI_BITFIELDS;
+  //  bi.bV5RedMask   =  0x00FF0000;
+  //  bi.bV5GreenMask =  0x0000FF00;
+  //  bi.bV5BlueMask  =  0x000000FF;
+  //  bi.bV5AlphaMask =  0xFF000000; 
+
+  //  // \83f\83X\83N\83g\83b\83vHDC\82Ì\8eæ\93¾
+  //  get_dc dc(NULL);
+
+  //  // DIB\83Z\83N\83V\83\87\83\93\82Ì\8dì\90¬
+  //  void *bits;// \93¾\82ç\82ê\82é\83r\83b\83g\83}\83b\83v
+  //  gdi_object<HBITMAP> bmp(
+  //    ::CreateDIBSection(
+  //      dc.get(),reinterpret_cast<BITMAPINFO *>(&bi),DIB_RGB_COLORS,&bits,NULL,0));
+  //  { 
+  //    // \8cÝ\8a·DC\82Ì\8dì\90¬
+  //    compatible_dc cdc(dc.get());
+  //    {
+  //      // \95`\89æ\90æ\82Ö\82Ì\90Ø\82è\91Ö\82¦
+  //      select_object s(cdc.get(),bmp);
+
+  //      // DC\8cÝ\8a·\83\8c\83\93\83_\81[\83^\81[\83Q\83b\83g\82Ì\83Z\83b\83g\83A\83b\83v
+  //      D2D1_RENDER_TARGET_PROPERTIES 
+  //        props = D2D1::RenderTargetProperties(
+  //        D2D1_RENDER_TARGET_TYPE_DEFAULT,
+  //        D2D1::PixelFormat(
+  //            DXGI_FORMAT_B8G8R8A8_UNORM,
+  //            D2D1_ALPHA_MODE_PREMULTIPLIED),
+  //        0,
+  //        0,
+  //        D2D1_RENDER_TARGET_USAGE_NONE,
+  //        D2D1_FEATURE_LEVEL_DEFAULT
+  //      );
+
+  //      ID2D1DCRenderTargetPtr dcr;
+  //      throw_if_err<>()(factory_->CreateDCRenderTarget(&props,&dcr));
+  //      RECT rect = {0,0,w,h};
+  //      // \8cÝ\8a·DC\82Ö\82Ì\83o\83C\83\93\83h
+  //      throw_if_err<>()(dcr->BindDC(cdc.get(),&rect));
+
+  //      // \83u\83\89\83V\82Ì\83Z\83b\83g\83A\83b\83v(\94w\8ci\82Ì\90Ô\8aÛ\81j
+  //      ID2D1SolidColorBrushPtr brush_e;
+  //      throw_if_err<>()(
+  //        dcr->CreateSolidColorBrush(
+  //          D2D1::ColorF(D2D1::ColorF::Red,0.8f), &brush_e));
+
+  //      // \83A\83C\83R\83\93\82É\95`\89æ\82·\82é\95\8e\9a\82Ì\90\90¬
+  //      std::wstring t(L"S.F.\nTimer");
+  //      D2D1_RECT_F l = D2D1::RectF(0.0f,0.0f,width_,height_);
+  //      // Text Format\82Ì\8dì\90¬
+  //      IDWriteTextFormatPtr f;
+  //      write_factory_->CreateTextFormat(
+  //      L"\83\81\83C\83\8a\83I",                // Font family name.
+  //      NULL,                       // Font collection (NULL sets it to use the system font collection).
+  //      DWRITE_FONT_WEIGHT_REGULAR,
+  //      DWRITE_FONT_STYLE_NORMAL,
+  //      DWRITE_FONT_STRETCH_NORMAL,
+  //      10.0f,
+  //      L"ja-jp",
+  //      &f
+  //      );
+  //      f->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
+  //      f->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
+
+  //      // \95\8e\9a\95`\89æ\97p\83u\83\89\83V\82Ì\83Z\83b\83g\83A\83b\83v
+  //      ID2D1SolidColorBrushPtr brush;
+  //      dcr->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &brush);
+  //      // \95`\89æ\8aJ\8en
+  //      dcr->BeginDraw();
+  //      // \83r\83b\83g\83}\83b\83v\83N\83\8a\83A
+  //      dcr->Clear(D2D1::ColorF(D2D1::ColorF::Black,0.0f));
+  //      // \90Ô\8aÛ\82ð\95`\82­
+  //      dcr->FillEllipse(D2D1::Ellipse(D2D1::Point2F(16.0f,16.0f),14,14),brush_e);
+  //      // \83e\83L\83X\83g\82ð\95\\8e¦\82·\82é
+  //      dcr->DrawTextW(t.c_str(),t.size(),f,&l,brush);
+  //      // \95`\89æ\8fI\97¹
+  //      dcr->EndDraw();
+  //    }
+  //  }
+  //}
 }