feat: win代码同步

This commit is contained in:
2026-01-13 19:26:03 -08:00
parent 9a907f6531
commit 6ef08c9e7b
12 changed files with 586 additions and 205 deletions
+13 -5
View File
@@ -1,13 +1,23 @@
# Project-level configuration.
cmake_minimum_required(VERSION 3.14)
project(HiFastVPN LANGUAGES CXX)
# 禁用 CMake 开发者警告(特别是 CMP0175 关于 flutter_inappwebview_windows 的警告)
set(CMAKE_WARN_DEPRECATED OFF CACHE BOOL "" FORCE)
set(CMAKE_POLICY_WARNING_CMP0175 OFF)
# 设置 CMake 策略以兼容旧版本插件
# CMP0175: add_custom_command() 拒绝无效参数(用于兼容 flutter_inappwebview_windows 插件)
# 必须在 project() 之前设置,才能传播到所有子目录
if(POLICY CMP0175)
cmake_policy(SET CMP0175 OLD)
endif()
project(HiFastVPN LANGUAGES CXX)
# Explicitly opt in to modern CMake behaviors to avoid warnings with recent
# versions of CMake.
cmake_policy(SET CMP0063 NEW)
# 设置静态链接
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
@@ -24,10 +34,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /FS")
# the on-disk name of your application.
set(BINARY_NAME "HiFastVPN")
# Explicitly opt in to modern CMake behaviors to avoid warnings with recent
# versions of CMake.
cmake_policy(SET CMP0063 NEW)
# Define build configuration option.
get_property(IS_MULTICONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(IS_MULTICONFIG)
@@ -83,6 +89,8 @@ include(flutter/generated_plugins.cmake)
foreach(plugin ${FLUTTER_PLUGIN_LIST})
if(TARGET ${plugin}_plugin)
target_compile_options(${plugin}_plugin PRIVATE /FS)
# 降低优化级别以避免访问违规错误(特别是 flutter_inappwebview_windows
target_compile_options(${plugin}_plugin PRIVATE /Od)
endif()
endforeach(plugin)
-1
View File
@@ -13,7 +13,6 @@ add_executable(${BINARY_NAME} WIN32
"win32_window.cpp"
"${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"
"Runner.rc"
"runner.exe.manifest"
)
# Apply the standard set of build settings. This can be removed for applications
+2
View File
@@ -54,6 +54,8 @@ END
// remains consistent on all systems.
IDI_APP_ICON ICON "resources\\app_icon.ico"
// Manifest resource for admin privileges and compatibility
IDR_MANIFEST1 RT_MANIFEST "runner.exe.manifest"
/////////////////////////////////////////////////////////////////////////////
//
+44 -5
View File
@@ -1,14 +1,53 @@
#include <flutter/dart_project.h>
#include <flutter/flutter_view_controller.h>
#include <windows.h>
#include <shellscalingapi.h>
#include "flutter_window.h"
#include "utils.h"
#include <protocol_handler_windows/protocol_handler_windows_plugin_c_api.h>
// ✅ 为较旧的 Windows SDK 版本提供 DPI 感知常量定义
#ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
#endif
// 动态加载 SetProcessDpiAwarenessContextWindows 10 v1607+
typedef BOOL(WINAPI* SetProcessDpiAwarenessContextFunc)(DPI_AWARENESS_CONTEXT);
typedef HRESULT(WINAPI* SetProcessDpiAwarenessFunc)(PROCESS_DPI_AWARENESS);
int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
_In_ wchar_t *command_line, _In_ int show_command) {
// ✅ 关键修复:启用 PerMonitorV2 DPI 感知,确保高分辨率显示器上字体清晰
// 这必须在创建任何窗口之前调用
HMODULE user32 = LoadLibraryW(L"user32.dll");
if (user32) {
auto set_dpi_aware_context = (SetProcessDpiAwarenessContextFunc)GetProcAddress(
user32, "SetProcessDpiAwarenessContext");
if (set_dpi_aware_context) {
set_dpi_aware_context(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
}
FreeLibrary(user32);
}
// 备用方案:如果 SetProcessDpiAwarenessContext 不可用,尝试 SetProcessDpiAwareness (Vista+)
HMODULE shcore = LoadLibraryW(L"shcore.dll");
if (shcore && !user32) {
auto set_dpi_aware = (SetProcessDpiAwarenessFunc)GetProcAddress(
shcore, "SetProcessDpiAwareness");
if (set_dpi_aware) {
set_dpi_aware(PROCESS_PER_MONITOR_DPI_AWARE);
}
FreeLibrary(shcore);
}
// ✅ 删除此处权限检查:
// 原因:manifest 中已配置 requireAdministratorWindows 会自动检查权限
// 如果权限不足,系统会弹出 UAC 对话框
// 在此处重复检查会导致额外的系统命令执行和黑窗显示
// 权限检查已移到 Dart 端,延迟到应用完全加载后进行(2秒后)
HANDLE hMutexInstance = CreateMutex(NULL, TRUE, L"HiFastVPNMutex");
HWND handle = FindWindowA(NULL, "HiFastVPN");
@@ -27,11 +66,11 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
return 0;
}
// Attach to console when present (e.g., 'flutter run') or create a
// new console when running with a debugger.
if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
CreateAndAttachConsole();
}
// ✅ 改进:完全禁用控制台窗口
// AttachConsole 在某些情况下仍会显示控制台窗口导致黑屏
// BearVPN 是 GUI 应用,不需要控制台输出
// 日志通过 KRInitLogCollector 写入文件,无需控制台
// (已注释掉所有控制台相关代码)
// Initialize COM, so that it is available for use in the library and/or
// plugins.
+19
View File
@@ -1,10 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<!-- 权限请求:应用以管理员权限运行,强制 UAC 提升 -->
<!-- ✅ TUN 模式默认权限方案:使用 requireAdministrator 模式
- 应用启动时自动弹出 UAC 提示(带盾牌图标)
- 用户同意后以管理员身份运行
- TUN 模式默认可用,无需额外权限检查
- 规则/系统代理模式在管理员身份下也可以运行
-->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<!-- requireAdministrator: 强制以管理员身份运行(需要 UAC 提升) -->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<!-- 启用高 DPI 感知,支持不同显示器的缩放 -->
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</windowsSettings>
</application>
<!-- 系统兼容性配置 -->
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 and Windows 11 -->
+22
View File
@@ -4,6 +4,7 @@
#include <io.h>
#include <stdio.h>
#include <windows.h>
#include <shlobj.h> // 用于权限检查
#include <iostream>
@@ -63,3 +64,24 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
}
return utf8_string;
}
// 🔧 检查应用是否以管理员权限运行
bool IsRunAsAdministrator() {
BOOL is_admin = FALSE;
HANDLE token_handle = nullptr;
// 打开当前进程的访问令牌
if (::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token_handle)) {
// 获取令牌中的提升信息
TOKEN_ELEVATION elevation{};
DWORD size = sizeof(elevation);
if (::GetTokenInformation(token_handle, TokenElevation, &elevation, size, &size)) {
is_admin = elevation.TokenIsElevated;
}
::CloseHandle(token_handle);
}
return is_admin == TRUE;
}
+4
View File
@@ -16,4 +16,8 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string);
// encoded in UTF-8. Returns an empty std::vector<std::string> on failure.
std::vector<std::string> GetCommandLineArguments();
// 🔧 检查应用是否以管理员权限运行
// 返回 true 如果当前进程有管理员权限
bool IsRunAsAdministrator();
#endif // RUNNER_UTILS_H_
+5 -1
View File
@@ -157,7 +157,11 @@ bool Win32Window::Create(const std::wstring& title,
return false;
}
// 获取系统主题设置
// ✅ 关键修复:启用非客户端 DPI 缩放,这对 PerMonitor V1 感知模式很关键
// 确保窗口标题栏、边框、菜单栏等在高 DPI 下正确缩放
EnableFullDpiSupportIfAvailable(window);
// 获取系统主题设置
DWORD light_mode;
DWORD light_mode_size = sizeof(light_mode);
LSTATUS result = RegGetValue(HKEY_CURRENT_USER, kGetPreferredBrightnessRegKey,