Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions packages/desktop_webview_window/windows/utils.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//
// Created by yangbin on 2021/11/11.
// Created by chuishui233 on 2025/09/26.
//

#include "utils.h"

#include <windows.h>

#include "utils.h"

#include <memory>
#include <set>
#include <string>
Expand Down Expand Up @@ -55,8 +55,7 @@ void ClipOrCenterWindowToMonitor(HWND hwnd, UINT flags) {
RECT rc;
GetWindowRect(hwnd, &rc);
ClipOrCenterRectToMonitor(&rc, flags);
SetWindowPos(hwnd, nullptr, rc.left, rc.top, 0, 0,
SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
SetWindowPos(hwnd, nullptr, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}

bool SetWindowBackgroundTransparent(HWND hwnd) {
Expand All @@ -66,7 +65,7 @@ bool SetWindowBackgroundTransparent(HWND hwnd) {

static std::unique_ptr<std::set<LPCWSTR>> class_registered_;

const wchar_t *RegisterWindowClass(LPCWSTR class_name, WNDPROC wnd_proc) {
const wchar_t *RegisterWindowClass(LPCWSTR class_name, WNDPROC wnd_proc, const std::wstring &iconPath) {
if (!class_registered_ || class_registered_->count(class_name) == 0) {
if (!class_registered_) {
class_registered_ = std::make_unique<std::set<LPCWSTR>>();
Expand All @@ -78,8 +77,19 @@ const wchar_t *RegisterWindowClass(LPCWSTR class_name, WNDPROC wnd_proc) {
window_class.cbClsExtra = 0;
window_class.cbWndExtra = 0;
window_class.hInstance = GetModuleHandle(nullptr);
window_class.hIcon = LoadIcon(window_class.hInstance, IDI_APPLICATION);
window_class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);

// Load custom icon if provided, otherwise use default
if (!iconPath.empty()) {
window_class.hIcon = (HICON)LoadImageW(nullptr, iconPath.c_str(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
if (!window_class.hIcon) {
// Fallback to default icon if loading fails
window_class.hIcon = LoadIcon(window_class.hInstance, IDI_APPLICATION);
}
} else {
window_class.hIcon = LoadIcon(window_class.hInstance, IDI_APPLICATION);
}

window_class.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
window_class.lpszMenuName = nullptr;
window_class.lpfnWndProc = wnd_proc;
RegisterClass(&window_class);
Expand All @@ -96,14 +106,4 @@ void UnregisterWindowClass(LPCWSTR class_name) {
UnregisterClass(class_name, nullptr);
}

std::string ConvertLPCWSTRToString(LPCWSTR lpcwszStr) {
int strLength = WideCharToMultiByte(CP_UTF8, 0, lpcwszStr, -1, nullptr, 0,
nullptr, nullptr);
std::string str(strLength, 0);

WideCharToMultiByte(CP_UTF8, 0, lpcwszStr, -1, &str[0], strLength, nullptr,
nullptr);
return str;
}

} // namespace webview_window
7 changes: 2 additions & 5 deletions packages/desktop_webview_window/windows/utils.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//
// Created by yangbin on 2021/11/11.
// Created by chuishui233 on 2025/09/26.
//

#ifndef DESKTOP_WEBVIEW_WINDOW_WINDOWS_UTILS_H_
#define DESKTOP_WEBVIEW_WINDOW_WINDOWS_UTILS_H_

#include <windows.h>
#include "wil/wrl.h"

#include <string>

namespace webview_window {
Expand Down Expand Up @@ -35,12 +34,10 @@ void ClipOrCenterWindowToMonitor(HWND hwnd, UINT flags);

bool SetWindowBackgroundTransparent(HWND hwnd);

const wchar_t *RegisterWindowClass(LPCWSTR class_name, WNDPROC wnd_proc);
const wchar_t *RegisterWindowClass(LPCWSTR class_name, WNDPROC wnd_proc, const std::wstring &iconPath = L"");

void UnregisterWindowClass(LPCWSTR class_name);

std::string ConvertLPCWSTRToString(LPCWSTR lpcwszStr);

} // namespace webview_window

#endif //DESKTOP_WEBVIEW_WINDOW_WINDOWS_UTILS_H_