2 #define BOOST_ASSIGN_MAX_PARAMS 7
3 #include <boost/assign.hpp>
4 #include <boost/assign/ptr_list_of.hpp>
5 #include <boost/assign/ptr_list_inserter.hpp>
6 #include <boost/foreach.hpp>
7 #include "toplevel_window.h"
8 #include "sf_windows.h"
11 #define THROW_IFERR(hres) \
12 if (FAILED(hres)) { throw sf::win32_error_exception(hres); }
14 #ifndef HINST_THISCOMPONENT
15 EXTERN_C IMAGE_DOS_HEADER __ImageBase;
16 #define HINST_THISCOMPONENT ((HINSTANCE)&__ImageBase)
22 HRESULT EnableBlurBehind(HWND hwnd)
26 //Create and populate the BlurBehind structre
27 DWM_BLURBEHIND bb = {0};
28 //Enable Blur Behind and Blur Region;
29 bb.dwFlags = DWM_BB_ENABLE;
34 hr = DwmEnableBlurBehindWindow(hwnd, &bb);
42 struct toplevel_window::impl : public base_win32_window
45 impl(const std::wstring& menu_name,const std::wstring& name,bool fit_to_display,float width = 160,float height = 100)
46 : base_win32_window(menu_name,name,fit_to_display,width,height) , wm_task_bar_create_(0),timer_id_(0),result_time_(INTERVAL_SEC1),status_(active)
48 on_render.connect(boost::bind(&impl::render,this));
52 safe_release(factory_);
53 safe_release(write_factory_);
56 LRESULT window_proc(HWND hwnd,boost::uint32_t message, WPARAM wParam, LPARAM lParam){
63 wm_task_bar_create_ = RegisterWindowMessage(L"TaskbarButtonCreated");
64 text(std::wstring(L"ミニタイマー(活動中)"));
65 //MARGINS mgn = {-1,0,0,0};//let,right,top,bottom
66 //HRESULT hr = DwmExtendFrameIntoClientArea(hwnd_, &mgn);
74 // size.width = lParam & 0xFFFF;
75 // size.height = (lParam >> 16) & 0xFFFF; ;
77 // // Note: This method can fail, but it's okay to ignore the
78 // // error here -- it will be repeated on the next call to
80 // render_target_->Resize(size);
89 paint_struct begin_paint(hwnd);
97 // ::ShowWindow(hwnd_,SW_MINIMIZE);
100 case WM_DISPLAYCHANGE:
102 ::InvalidateRect(hwnd, NULL, FALSE);
112 // on_mouse_move(GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam),wParam);
125 taskbar_.progress_value(*this,result_time_,INTERVAL_SEC1);
127 if(result_time_ == 0)
129 ::MessageBeep(MB_ICONEXCLAMATION);
131 result_time_ = INTERVAL_SEC2;
132 taskbar_.progress_state(*this,taskbar::paused);
133 taskbar_.progress_value(*this,result_time_,INTERVAL_SEC2);
134 text(std::wstring(L"ミニタイマー(休憩中)"));
136 ::InvalidateRect(hwnd_,NULL,FALSE);
141 taskbar_.progress_value(*this,result_time_,INTERVAL_SEC2);
142 if(result_time_ == 0)
144 ::MessageBeep(MB_OK);
146 result_time_ = INTERVAL_SEC1;
147 taskbar_.progress_state(*this,taskbar::indeterminate);
148 taskbar_.progress_value(*this,result_time_,INTERVAL_SEC1);
149 text(std::wstring(L"ミニタイマー(活動中)"));
155 case WM_DWMCOMPOSITIONCHANGED:
157 //MARGINS mgn = {-1,0,0,0};//let,right,top,bottom
158 //HRESULT hr = DwmExtendFrameIntoClientArea(hwnd_, &mgn);
161 //::SetTimer(hwnd_,(UINT_PTR)&timer_id_,1000,NULL);
165 if(message == wm_task_bar_create_)
168 taskbar_.overlay_icon(*this,NULL,std::wstring(L"ミニタイマー"));
169 taskbar_.progress_state(*this,taskbar::indeterminate);
170 taskbar_.progress_value(*this,100,100);
172 ::SetTimer(hwnd_,(UINT_PTR)&timer_id_,1000,NULL);
175 if(message == WM_CLOSE)
177 ::KillTimer(hwnd_,(UINT_PTR)&timer_id_);
183 safe_release(render_target_);
185 BOOL ret(::DestroyWindow(hwnd));
186 BOOST_ASSERT(ret != 0);
189 if(message == WM_DESTROY)
191 ::PostQuitMessage(0);
195 return ::DefWindowProcW(hwnd,message,wParam,lParam);
198 virtual void create(){
199 create_device_independent_resources();
205 DwmIsCompositionEnabled (&dwmEnable);
206 if (dwmEnable) EnableBlurBehind(*this);
210 virtual void discard_device()
212 safe_release(render_target_);
215 virtual void create_device(){
216 // 入力_.reset(new input(HINST_THISCOMPONENT,hwnd_));
221 GetClientRect( hwnd_, &rc );
222 boost::uint32_t width = rc.right - rc.left;
223 boost::uint32_t height = rc.bottom - rc.top;
226 //wic_imaging_factory_.CreateInstance(CLSID_WICImagingFactory);
227 // bitmap_ = load_bitmap_from_file(render_target_,wic_imaging_factory_,L"myship.png");
233 GetClientRect(hwnd_, &rc);
235 D2D1_SIZE_U size = D2D1::SizeU(
240 const D2D1_PIXEL_FORMAT format =
241 D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,
242 D2D1_ALPHA_MODE_PREMULTIPLIED);
244 const D2D1_RENDER_TARGET_PROPERTIES target_prop =
245 D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT,format);
247 THROW_IFERR(factory_->CreateHwndRenderTarget(
249 D2D1::HwndRenderTargetProperties(hwnd_, size,D2D1_PRESENT_OPTIONS_IMMEDIATELY),
252 // Create a DC render target
253 //D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
254 // D2D1_RENDER_TARGET_TYPE_DEFAULT,
255 // D2D1::PixelFormat(
256 // DXGI_FORMAT_B8G8R8A8_UNORM,
257 // D2D1_ALPHA_MODE_IGNORE
259 // D2D1_RENDER_TARGET_USAGE_GDI_COMPATIBLE
262 //THROW_IFERR(factory_->CreateDCRenderTarget(
269 virtual void create_device_independent_resources(){
270 // Direct2DFactory の生成
273 #if defined(DEBUG) || defined(_DEBUG)
274 D2D1_FACTORY_OPTIONS options;
275 options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION ;
276 THROW_IFERR(D2D1CreateFactory(
277 D2D1_FACTORY_TYPE_SINGLE_THREADED,
282 THROW_IFERR(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &factory_));
288 THROW_IFERR(::DWriteCreateFactory(
289 DWRITE_FACTORY_TYPE_SHARED,
290 __uuidof(IDWriteFactory),
291 reinterpret_cast<IUnknown**>(&write_factory_)
296 //wic_imaging_factory_.CreateInstance(CLSID_WICImagingFactory);
298 //thunk_proc_ = (WNDPROC)thunk_.getCode();
299 layout_rect_ = D2D1::RectF(0.0f,0.0f,width_,height_);
301 THROW_IFERR(write_factory_->CreateTextFormat(
302 L"メイリオ", // Font family name.
303 NULL, // Font collection (NULL sets it to use the system font collection).
304 DWRITE_FONT_WEIGHT_REGULAR,
305 DWRITE_FONT_STYLE_NORMAL,
306 DWRITE_FONT_STRETCH_NORMAL,
315 static float t = 0.0f;
319 // Retrieve the size of the render target.
320 D2D1_SIZE_F renderTargetSize = render_target_->GetSize();
322 render_target_->BeginDraw();
323 render_target_->Clear(D2D1::ColorF(0,0.0f));
324 // render_target_->FillRectangle(D2D1::RectF(0.0f,0.0f,renderTargetSize.width,renderTargetSize.height),);
326 // (D2D1::ColorF(0,1.0f));
327 render_target_->SetTransform(D2D1::Matrix3x2F::Identity());
328 ID2D1SolidColorBrushPtr brush;
329 render_target_->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::OrangeRed), &brush);
331 std::wstring m((boost::wformat(L"残り%d分%d秒") % (result_time_ / 60) % (result_time_ % 60)).str());
332 render_target_->DrawTextW(
338 THROW_IFERR(render_target_->EndDraw());
339 } catch(sf::win32_error_exception& err)
341 if(err.hresult() == D2DERR_RECREATE_TARGET)
358 WS_EX_APPWINDOW | WS_EX_TOPMOST,
364 static_cast<boost::uint32_t>(ceil(width_ /** dpiX / 96.f*/)),
365 static_cast<boost::uint32_t>(ceil(height_ /** dpiY / 96.f*/)),
375 long wm_task_bar_create_;
376 sf::taskbar taskbar_;
378 static const int INTERVAL_SEC1 = 60 * 15;// 15分
379 static const int INTERVAL_SEC2 = 30;// 30秒
387 ID2D1FactoryPtr factory_;
388 ID2D1HwndRenderTargetPtr render_target_;
389 IDWriteFactoryPtr write_factory_;
390 IWICImagingFactoryPtr wic_imaging_factory_;
392 timer_status status_;
393 IDWriteTextFormatPtr write_text_format_;
394 D2D1_RECT_F layout_rect_;
398 // Creates a Direct2D bitmap from the specified
401 ID2D1BitmapPtr load_bitmap_from_file(
402 ID2D1HwndRenderTargetPtr render_target,
403 IWICImagingFactoryPtr wic_factory,
405 boost::uint32_t destination_width,
406 boost::uint32_t destination_height
411 IWICBitmapDecoderPtr decoder;
412 IWICBitmapFrameDecodePtr decoder_source;
413 IWICStreamPtr stream;
414 IWICFormatConverterPtr converter;
415 IWICBitmapScalerPtr scaler;
416 ID2D1BitmapPtr bitmap;
418 THROW_IFERR(wic_factory->CreateDecoderFromFilename(
422 WICDecodeMetadataCacheOnLoad,
426 // Create the initial frame.
427 THROW_IFERR(decoder->GetFrame(0, &decoder_source));
429 // Convert the image format to 32bppPBGRA
430 // (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
431 THROW_IFERR(hr = wic_factory->CreateFormatConverter(&converter));
433 // If a new width or height was specified, create an
434 // IWICBitmapScaler and use it to resize the image.
435 if (destination_width != 0 || destination_height != 0)
437 boost::uint32_t originalWidth, originalHeight;
438 THROW_IFERR(decoder_source->GetSize((UINT*)&originalWidth, (UINT*)&originalHeight));
439 if (destination_width == 0)
441 FLOAT scalar = static_cast<FLOAT>(destination_height) / static_cast<FLOAT>(originalHeight);
442 destination_width = static_cast<boost::uint32_t>(scalar * static_cast<FLOAT>(originalWidth));
444 else if (destination_height == 0)
446 FLOAT scalar = static_cast<FLOAT>(destination_width) / static_cast<FLOAT>(originalWidth);
447 destination_height = static_cast<boost::uint32_t>(scalar * static_cast<FLOAT>(originalHeight));
450 THROW_IFERR(wic_factory->CreateBitmapScaler(&scaler));
451 THROW_IFERR(scaler->Initialize(
455 WICBitmapInterpolationModeCubic
457 THROW_IFERR(converter->Initialize(
458 scaler.GetInterfacePtr(),
459 GUID_WICPixelFormat32bppPBGRA,
460 WICBitmapDitherTypeNone,
463 WICBitmapPaletteTypeMedianCut
466 else // Don't scale the image.
468 THROW_IFERR(converter->Initialize(
469 decoder_source.GetInterfacePtr(),
470 GUID_WICPixelFormat32bppPBGRA,
471 WICBitmapDitherTypeNone,
474 WICBitmapPaletteTypeMedianCut
478 // Create a Direct2D bitmap from the WIC bitmap.
479 THROW_IFERR(render_target->CreateBitmapFromWicBitmap(
480 converter.GetInterfacePtr(),
488 toplevel_window::toplevel_window(const std::wstring& menu_name,const std::wstring& name,bool fit_to_display,float width ,float height)
489 : impl_(new impl(menu_name,name,fit_to_display,width,height))
494 void * toplevel_window::raw_handle(){return impl_->raw_handle();};
495 void toplevel_window::create(){impl_->create();};
496 void toplevel_window::show(boost::uint32_t show_flag){impl_->show(show_flag);};
497 void toplevel_window::text(std::wstring& text){impl_->text(text);};
498 void toplevel_window::update(){impl_->update();};
500 toplevel_window_ptr create_toplevel_window
502 const std::wstring& menu_name,
503 const std::wstring& name,
504 const boost::uint32_t show_flag,
510 toplevel_window* p = new toplevel_window(menu_name,name,fit_to_display,width,height);
514 return toplevel_window_ptr(p);