Skip to content

Commit 7a9335f

Browse files
authored
Add files via upload
1 parent 40f1c41 commit 7a9335f

30 files changed

+4503
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "pch.h"
2+
#include "AboutDialog.h"
3+
#include "AboutDialog.g.cpp"
4+
#include <microsoft.ui.xaml.window.h>
5+
// 移除以下重复包含的头文件,因为它们已经在 pch.h 中包含
6+
// #include <winrt/Microsoft.UI.Interop.h>
7+
// #include <winrt/Microsoft.UI.Windowing.h>
8+
9+
// 添加Windows API头文件
10+
#include <windowsx.h>
11+
#include <winuser.h>
12+
13+
using namespace winrt;
14+
using namespace Microsoft::UI::Xaml;
15+
using namespace Microsoft::UI::Windowing;
16+
17+
namespace winrt::MemoryCleaner::implementation
18+
{
19+
AboutDialog::AboutDialog()
20+
{
21+
InitializeComponent();
22+
versionTextBlock().Text(L"v 1.2.0");
23+
24+
// 设置自定义标题栏
25+
auto titleBar = AboutDialogTitleBar();
26+
this->SetTitleBar(titleBar);
27+
this->ExtendsContentIntoTitleBar(true);
28+
29+
// 使用IWindowNative接口和AppWindow设置窗口大小
30+
auto windowNative{ this->try_as<::IWindowNative>() };
31+
if (windowNative)
32+
{
33+
HWND hWnd{ nullptr };
34+
windowNative->get_WindowHandle(&hWnd);
35+
36+
if (hWnd)
37+
{
38+
// 获取并存储AppWindow
39+
appWindow = AppWindow::GetFromWindowId(
40+
Microsoft::UI::GetWindowIdFromWindow(hWnd));
41+
42+
if (appWindow)
43+
{
44+
appWindow.Resize({ 640, 480 });
45+
46+
// 使用Windows API禁止调整窗口大小
47+
// 获取当前窗口样式
48+
LONG_PTR style = GetWindowLongPtr(hWnd, GWL_STYLE);
49+
// 移除WS_THICKFRAME样式(允许调整大小的边框)
50+
style &= ~WS_THICKFRAME;
51+
// 移除WS_MAXIMIZEBOX样式(禁用最大化按钮)
52+
style &= ~WS_MAXIMIZEBOX;
53+
// 应用新的窗口样式
54+
SetWindowLongPtr(hWnd, GWL_STYLE, style);
55+
56+
// 刷新窗口
57+
SetWindowPos(hWnd, nullptr, 0, 0, 0, 0,
58+
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
59+
}
60+
}
61+
}
62+
}
63+
64+
void AboutDialog::CloseButtonClick(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& e)
65+
{
66+
Close();
67+
}
68+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include "AboutDialog.g.h"
4+
// 移除以下重复包含的头文件,因为它们已经在 pch.h 中包含
5+
// #include <winrt/Microsoft.UI.Windowing.h>
6+
// #include <winrt/Microsoft.UI.Interop.h>
7+
8+
namespace winrt::MemoryCleaner::implementation
9+
{
10+
struct AboutDialog : AboutDialogT<AboutDialog>
11+
{
12+
AboutDialog();
13+
void CloseButtonClick(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& e);
14+
void Activate();
15+
winrt::Microsoft::UI::Windowing::AppWindow appWindow{ nullptr };
16+
};
17+
}
18+
19+
namespace winrt::MemoryCleaner::factory_implementation
20+
{
21+
struct AboutDialog : AboutDialogT<AboutDialog, implementation::AboutDialog>
22+
{
23+
};
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
namespace MemoryCleaner
3+
{
4+
[default_interface]
5+
runtimeclass AboutDialog : Microsoft.UI.Xaml.Window
6+
{
7+
AboutDialog();
8+
}
9+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<!-- AboutDialog.xaml -->
2+
<Window
3+
x:Class="MemoryCleaner.AboutDialog"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:local="using:MemoryCleaner"
7+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
10+
mc:Ignorable="d"
11+
>
12+
13+
<Grid>
14+
<Grid.RowDefinitions>
15+
<RowDefinition Height="Auto"/>
16+
<RowDefinition Height="*"/>
17+
</Grid.RowDefinitions>
18+
19+
<!-- 自定义标题栏 -->
20+
<muxc:TitleBar x:Name="AboutDialogTitleBar"
21+
Title="About"
22+
Grid.Row="0"
23+
HorizontalAlignment="Left">
24+
</muxc:TitleBar>
25+
26+
<!-- 内容区域 -->
27+
<ScrollViewer Grid.Row="1">
28+
<StackPanel Spacing="10">
29+
<!-- 标题 -->
30+
<TextBlock
31+
x:Name="titleTextBlock"
32+
Text="MemoryCleaner"
33+
FontSize="20"
34+
FontWeight="Bold"
35+
HorizontalAlignment="Center"/>
36+
37+
<!-- 版本号 -->
38+
<TextBlock
39+
x:Name="versionTextBlock"
40+
HorizontalAlignment="Center"/>
41+
42+
<!-- 许可证信息 -->
43+
<TextBlock
44+
Text="This program follows the GPL 3.0 open-source license."
45+
HorizontalAlignment="Center"/>
46+
47+
<!-- GitHub链接 -->
48+
<HyperlinkButton
49+
Content="https://github.com/TheUserWW/MemoryCleaner.git"
50+
NavigateUri="https://github.com/TheUserWW/MemoryCleaner"
51+
HorizontalAlignment="Center"/>
52+
53+
<!-- 许可证文本 -->
54+
<Border
55+
BorderBrush="{ThemeResource ContentDialogBorderThemeBrush}"
56+
BorderThickness="1"
57+
CornerRadius="4"
58+
MaxHeight="136"
59+
HorizontalAlignment="Center"
60+
Margin="5,0">
61+
<TextBlock
62+
TextWrapping="Wrap"
63+
FontSize="11"
64+
Text="This program is free software: you can redistribute it and/or modify&#x0a;it under the terms of the GNU General Public License as published by&#x0a;the Free Software Foundation, either version 3 of the License, or&#x0a;(at your option) any later version.&#x0a;&#x0a;This program is distributed in the hope that it will be useful,&#x0a;but WITHOUT ANY WARRANTY; without even the implied warranty of&#x0a;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the&#x0a;GNU General Public License for more details.&#x0a;&#x0a;You should have received a copy of the GNU General Public License&#x0a;along with this program. If not, see &lt;https://www.gnu.org/licenses/&gt;."/>
65+
</Border>
66+
</StackPanel>
67+
</ScrollViewer>
68+
</Grid>
69+
</Window>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Application
3+
x:Class="MemoryCleaner.App"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:local="using:MemoryCleaner">
7+
<Application.Resources>
8+
<ResourceDictionary>
9+
<ResourceDictionary.MergedDictionaries>
10+
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
11+
<!-- Other merged dictionaries here -->
12+
</ResourceDictionary.MergedDictionaries>
13+
14+
<!-- 移除托盘菜单定义 -->
15+
16+
</ResourceDictionary>
17+
</Application.Resources>
18+
</Application>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include "pch.h"
2+
#include "App.xaml.h"
3+
#include "MainWindow.xaml.h"
4+
#include <winrt/Microsoft.UI.Interop.h> // 添加这行
5+
#include <microsoft.ui.xaml.window.h>
6+
7+
using namespace winrt;
8+
using namespace Microsoft::UI::Xaml;
9+
10+
// To learn more about WinUI, the WinUI project structure,
11+
// and more about our project templates, see: http://aka.ms/winui-project-info.
12+
13+
namespace winrt::MemoryCleaner::implementation
14+
{
15+
// 全局互斥锁句柄
16+
static HANDLE hMutex = NULL;
17+
static const wchar_t* MutexName = L"MemoryCleaner_SingleInstance_Mutex";
18+
19+
/// <summary>
20+
/// 激活已存在的应用程序窗口
21+
/// </summary>
22+
void ActivateExistingWindow()
23+
{
24+
// 查找已存在的窗口
25+
HWND hWnd = FindWindowW(L"MemoryCleanerMainWindow", L"MemoryCleaner");
26+
if (hWnd)
27+
{
28+
// 如果窗口被最小化,则恢复它
29+
if (IsIconic(hWnd))
30+
{
31+
ShowWindow(hWnd, SW_RESTORE);
32+
}
33+
// 将窗口置于前台
34+
SetForegroundWindow(hWnd);
35+
// 激活窗口
36+
SetActiveWindow(hWnd);
37+
}
38+
}
39+
40+
/// <summary>
41+
/// Initializes the singleton application object. This is the first line of authored code
42+
/// executed, and as such is the logical equivalent of main() or WinMain().
43+
/// </summary>
44+
App::App()
45+
{
46+
// 创建互斥锁,确保只有一个实例运行
47+
hMutex = CreateMutexW(NULL, TRUE, MutexName);
48+
if (hMutex == NULL || GetLastError() == ERROR_ALREADY_EXISTS)
49+
{
50+
// 互斥锁已存在,说明已有实例在运行
51+
if (hMutex != NULL)
52+
{
53+
CloseHandle(hMutex);
54+
hMutex = NULL;
55+
}
56+
57+
// 激活已存在的窗口
58+
ActivateExistingWindow();
59+
60+
// 退出当前实例
61+
ExitProcess(0);
62+
return;
63+
}
64+
65+
// Xaml objects should not call InitializeComponent during construction.
66+
// See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent
67+
68+
#if defined _DEBUG && !defined DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
69+
UnhandledException([](IInspectable const&, UnhandledExceptionEventArgs const& e)
70+
{
71+
if (IsDebuggerPresent())
72+
{
73+
auto errorMessage = e.Message();
74+
__debugbreak();
75+
}
76+
});
77+
#endif
78+
}
79+
80+
/// <summary>
81+
/// Invoked when the application is launched.
82+
/// </summary>
83+
/// <param name="e">Details about the launch request and process.</param>
84+
void App::OnLaunched([[maybe_unused]] LaunchActivatedEventArgs const& e)
85+
{
86+
window = make<MainWindow>();
87+
window.Activate();
88+
89+
// 设置窗口类名和标题,以便其他实例可以找到它
90+
if (window)
91+
{
92+
auto windowNative = window.try_as<::IWindowNative>(); // 修改这行
93+
if (windowNative)
94+
{
95+
HWND hWnd = nullptr;
96+
windowNative->get_WindowHandle(&hWnd);
97+
if (hWnd)
98+
{
99+
// 设置窗口类名
100+
SetClassLongW(hWnd, GCLP_HCURSOR, (LONG)LoadCursorW(NULL, IDC_ARROW));
101+
// 设置窗口标题
102+
SetWindowTextW(hWnd, L"MemoryCleaner");
103+
}
104+
}
105+
}
106+
}
107+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include "App.xaml.g.h"
4+
5+
namespace winrt::MemoryCleaner::implementation
6+
{
7+
struct App : AppT<App>
8+
{
9+
App();
10+
11+
void OnLaunched(Microsoft::UI::Xaml::LaunchActivatedEventArgs const&);
12+
13+
private:
14+
winrt::Microsoft::UI::Xaml::Window window{ nullptr };
15+
};
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace MemoryCleaner
2+
{
3+
[default_interface]
4+
runtimeclass MainWindow : Microsoft.UI.Xaml.Window
5+
{
6+
MainWindow();
7+
}
8+
}

0 commit comments

Comments
 (0)