diff --git a/src/kiwano/platform/Runner.h b/src/kiwano/platform/Runner.h index 68743408..9e7c2f0d 100644 --- a/src/kiwano/platform/Runner.h +++ b/src/kiwano/platform/Runner.h @@ -43,7 +43,7 @@ struct Settings uint32_t width; ///< 窗口宽度 uint32_t height; ///< 窗口高度 String title; ///< 窗口标题 - uint32_t icon; ///< 窗口图标 + Icon icon; ///< 窗口图标 bool resizable; ///< 窗口大小可调整 bool fullscreen; ///< 窗口全屏 Color bg_color; ///< 窗口背景色 diff --git a/src/kiwano/platform/Window.h b/src/kiwano/platform/Window.h index 5a6c89b6..cae2b49b 100644 --- a/src/kiwano/platform/Window.h +++ b/src/kiwano/platform/Window.h @@ -56,6 +56,31 @@ struct Resolution uint32_t refresh_rate; ///< 刷新率 }; +/** + * \~chinese + * @brief 图标 + */ +struct Icon +{ + Icon() = default; + + Icon(const String& file_path) + : file_path(file_path) + { + } + + String file_path; ///< 文件路径 + +#if defined(KGE_PLATFORM_WINDOWS) + uint32_t resource_id = 0; ///< 资源ID,仅在windows上生效 + + Icon(uint32_t resource_id) + : resource_id(resource_id) + { + } +#endif +}; + #if defined(KGE_PLATFORM_WINDOWS) typedef HWND WindowHandle; @@ -79,7 +104,7 @@ public: * @param resizable 窗口大小可拉伸 * @throw kiwano::SystemError 窗口创建失败时抛出 */ - static WindowPtr Create(const String& title, uint32_t width, uint32_t height, uint32_t icon = 0, + static WindowPtr Create(const String& title, uint32_t width, uint32_t height, Icon icon = Icon(), bool resizable = false, bool fullscreen = false); /** diff --git a/src/kiwano/platform/win32/WindowImpl.cpp b/src/kiwano/platform/win32/WindowImpl.cpp index 21634135..ced5351e 100644 --- a/src/kiwano/platform/win32/WindowImpl.cpp +++ b/src/kiwano/platform/win32/WindowImpl.cpp @@ -48,7 +48,7 @@ public: virtual ~WindowWin32Impl(); - void Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, bool fullscreen); + void Init(const String& title, uint32_t width, uint32_t height, Icon icon, bool resizable, bool fullscreen); void SetTitle(const String& title) override; @@ -87,7 +87,7 @@ private: std::array key_map_; }; -WindowPtr Window::Create(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, +WindowPtr Window::Create(const String& title, uint32_t width, uint32_t height, Icon icon, bool resizable, bool fullscreen) { WindowWin32ImplPtr ptr = memory::New(); @@ -205,7 +205,7 @@ WindowWin32Impl::~WindowWin32Impl() ::timeEndPeriod(0); } -void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, +void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, Icon icon, bool resizable, bool fullscreen) { HINSTANCE hinst = GetModuleHandle(nullptr); @@ -222,11 +222,16 @@ void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, wcex.lpszMenuName = nullptr; wcex.hCursor = ::LoadCursor(hinst, IDC_ARROW); - if (icon) + if (icon.resource_id != 0 && IS_INTRESOURCE(icon.resource_id)) { - wcex.hIcon = (HICON)::LoadImage(hinst, MAKEINTRESOURCE(icon), IMAGE_ICON, 0, 0, + wcex.hIcon = (HICON)::LoadImage(hinst, MAKEINTRESOURCE(icon.resource_id), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE); } + else if (!icon.file_path.empty()) + { + wcex.hIcon = (HICON)::LoadImageA(NULL, icon.file_path.c_str(), IMAGE_ICON, 0, 0, + LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE); + } ::RegisterClassExA(&wcex);