Skip to content

Commit 2b110cf

Browse files
committedMay 21, 2024
优化代码
1 parent 5f38825 commit 2b110cf

File tree

6 files changed

+181
-153
lines changed

6 files changed

+181
-153
lines changed
 

‎.editorconfig

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[*.cs]
2+
3+
# SYSLIB1054: Use 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
4+
dotnet_diagnostic.SYSLIB1054.severity = none
5+
csharp_indent_labels = one_less_than_current
6+
csharp_space_around_binary_operators = before_and_after
7+
csharp_using_directive_placement = outside_namespace:silent
8+
csharp_prefer_simple_using_statement = true:suggestion
9+
csharp_prefer_braces = true:silent
10+
csharp_style_namespace_declarations = block_scoped:silent
11+
csharp_style_prefer_method_group_conversion = true:silent
12+
csharp_style_prefer_top_level_statements = true:silent
13+
csharp_style_prefer_primary_constructors = true:suggestion
14+
csharp_style_expression_bodied_methods = false:silent
15+
csharp_style_expression_bodied_constructors = false:silent
16+
csharp_style_expression_bodied_operators = false:silent
17+
csharp_style_expression_bodied_properties = true:silent
18+
csharp_style_expression_bodied_indexers = true:silent
19+
csharp_style_expression_bodied_accessors = true:silent
20+
csharp_style_expression_bodied_lambdas = true:silent
21+
csharp_style_expression_bodied_local_functions = false:silent
22+
csharp_style_throw_expression = true:suggestion
23+
csharp_style_prefer_null_check_over_type_check = true:suggestion
24+
csharp_prefer_simple_default_expression = true:suggestion
25+
csharp_style_prefer_local_over_anonymous_function = true:suggestion
26+
csharp_style_prefer_index_operator = true:suggestion
27+
csharp_style_prefer_range_operator = true:suggestion
28+
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
29+
csharp_style_prefer_tuple_swap = true:suggestion
30+
csharp_style_prefer_utf8_string_literals = true:suggestion
31+
csharp_style_inlined_variable_declaration = true:suggestion
32+
csharp_style_deconstructed_variable_declaration = true:suggestion
33+
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
34+
35+
[*.{cs,vb}]
36+
#### Naming styles ####
37+
38+
# Naming rules
39+
40+
dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
41+
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
42+
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
43+
44+
dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
45+
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
46+
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case
47+
48+
dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
49+
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
50+
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case
51+
52+
# Symbol specifications
53+
54+
dotnet_naming_symbols.interface.applicable_kinds = interface
55+
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
56+
dotnet_naming_symbols.interface.required_modifiers =
57+
58+
dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
59+
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
60+
dotnet_naming_symbols.types.required_modifiers =
61+
62+
dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
63+
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
64+
dotnet_naming_symbols.non_field_members.required_modifiers =
65+
66+
# Naming styles
67+
68+
dotnet_naming_style.begins_with_i.required_prefix = I
69+
dotnet_naming_style.begins_with_i.required_suffix =
70+
dotnet_naming_style.begins_with_i.word_separator =
71+
dotnet_naming_style.begins_with_i.capitalization = pascal_case
72+
73+
dotnet_naming_style.pascal_case.required_prefix =
74+
dotnet_naming_style.pascal_case.required_suffix =
75+
dotnet_naming_style.pascal_case.word_separator =
76+
dotnet_naming_style.pascal_case.capitalization = pascal_case
77+
78+
dotnet_naming_style.pascal_case.required_prefix =
79+
dotnet_naming_style.pascal_case.required_suffix =
80+
dotnet_naming_style.pascal_case.word_separator =
81+
dotnet_naming_style.pascal_case.capitalization = pascal_case
82+
dotnet_style_operator_placement_when_wrapping = beginning_of_line
83+
tab_width = 4
84+
indent_size = 4
85+
end_of_line = crlf
86+
dotnet_style_coalesce_expression = true:suggestion
87+
dotnet_style_null_propagation = true:suggestion
88+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
89+
dotnet_style_prefer_auto_properties = true:silent
90+
dotnet_style_object_initializer = true:suggestion
91+
dotnet_style_collection_initializer = true:suggestion
92+
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
93+
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
94+
dotnet_style_prefer_conditional_expression_over_return = true:silent
95+
dotnet_style_explicit_tuple_names = true:suggestion
96+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
97+
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
98+
dotnet_style_prefer_compound_assignment = true:suggestion
99+
dotnet_style_prefer_simplified_interpolation = true:suggestion
100+
dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion
101+
dotnet_style_namespace_match_folder = true:suggestion

‎CSharpLearn.sln

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ VisualStudioVersion = 17.9.34728.123
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{40B52BEA-8476-4E1E-AB1D-75AD800BCE8B}"
77
ProjectSection(SolutionItems) = preProject
8+
.editorconfig = .editorconfig
89
Readme.md = Readme.md
910
EndProjectSection
1011
EndProject
1112
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CaliburnMicroLearn", "CaliburnMicroLearn\CaliburnMicroLearn.csproj", "{0A479337-64A1-45FD-845F-B341B1010EC0}"
1213
EndProject
13-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPFCustomControl", "WPFCustomControl\WPFCustomControl.csproj", "{F252ADDE-AF5F-4899-9C71-432E69A11EBA}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WPFCustomControl", "WPFCustomControl\WPFCustomControl.csproj", "{F252ADDE-AF5F-4899-9C71-432E69A11EBA}"
1415
EndProject
1516
Global
1617
GlobalSection(SolutionConfigurationPlatforms) = preSolution

‎CaliburnMicroLearn/LoggerUtils/Logger.cs

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using log4net;
2+
using System.IO;
23
using System.Reflection;
34
using System.Runtime.CompilerServices;
45

@@ -25,10 +26,10 @@ static Logger()
2526
/// <param name="msg">具体的log信息</param>
2627
/// <param name="callerMember">调用的函数名字</param>
2728
/// <param name="callerLineNumber">调用的具体行数</param>
28-
public static void Debug(string msg, [CallerFilePath] string callerFile = "",
29-
[CallerMemberName] string callerMember = "", [CallerLineNumber] int callerLineNumber = 0)
29+
public static void Debug(String msg, [CallerFilePath] String callerFile = "",
30+
[CallerMemberName] String callerMember = "", [CallerLineNumber] Int32 callerLineNumber = 0)
3031
{
31-
LogManager.GetLogger(callerFile.Split('.')[^2].Split('\\')[^1] + ":" + callerMember + ":" + callerLineNumber).Debug(msg);
32+
LogManager.GetLogger(Path.GetFileNameWithoutExtension(callerFile) + ":" + callerMember + ":" + callerLineNumber).Debug(msg);
3233
}
3334

3435
/// <summary>
@@ -37,10 +38,10 @@ public static void Debug(string msg, [CallerFilePath] string callerFile = "",
3738
/// <param name="msg">具体的log信息</param>
3839
/// <param name="callerMember">调用的函数名字</param>
3940
/// <param name="callerLineNumber">调用的具体行数</param>
40-
public static void Info(string msg, [CallerFilePath] string callerFile = "",
41-
[CallerMemberName] string callerMember = "", [CallerLineNumber] int callerLineNumber = 0)
41+
public static void Info(String msg, [CallerFilePath] String callerFile = "",
42+
[CallerMemberName] String callerMember = "", [CallerLineNumber] Int32 callerLineNumber = 0)
4243
{
43-
LogManager.GetLogger(callerFile.Split('.')[^2].Split('\\')[^1] + ":" + callerMember + ":" + callerLineNumber).Info(msg);
44+
LogManager.GetLogger(Path.GetFileNameWithoutExtension(callerFile) + ":" + callerMember + ":" + callerLineNumber).Info(msg);
4445
}
4546

4647
/// <summary>
@@ -49,10 +50,10 @@ public static void Info(string msg, [CallerFilePath] string callerFile = "",
4950
/// <param name="msg">具体的log信息</param>
5051
/// <param name="callerMember">调用的函数名字</param>
5152
/// <param name="callerLineNumber">调用的具体行数</param>
52-
public static void Warn(string msg, [CallerFilePath] string callerFile = "",
53-
[CallerMemberName] string callerMember = "", [CallerLineNumber] int callerLineNumber = 0)
53+
public static void Warn(String msg, [CallerFilePath] String callerFile = "",
54+
[CallerMemberName] String callerMember = "", [CallerLineNumber] Int32 callerLineNumber = 0)
5455
{
55-
LogManager.GetLogger(callerFile.Split('.')[^2].Split('\\')[^1] + ":" + callerMember + ":" + callerLineNumber).Warn(msg);
56+
LogManager.GetLogger(Path.GetFileNameWithoutExtension(callerFile) + ":" + callerMember + ":" + callerLineNumber).Warn(msg);
5657
}
5758

5859
/// <summary>
@@ -61,10 +62,10 @@ public static void Warn(string msg, [CallerFilePath] string callerFile = "",
6162
/// <param name="msg">具体的log信息</param>
6263
/// <param name="callerMember">调用的函数名字</param>
6364
/// <param name="callerLineNumber">调用的具体行数</param>
64-
public static void Error(string msg, [CallerFilePath] string callerFile = "",
65-
[CallerMemberName] string callerMember = "", [CallerLineNumber] int callerLineNumber = 0)
65+
public static void Error(String msg, [CallerFilePath] String callerFile = "",
66+
[CallerMemberName] String callerMember = "", [CallerLineNumber] Int32 callerLineNumber = 0)
6667
{
67-
LogManager.GetLogger(callerFile.Split('.')[^2].Split('\\')[^1] + ":" + callerMember + ":" + callerLineNumber).Error(msg);
68+
LogManager.GetLogger(Path.GetFileNameWithoutExtension(callerFile) + ":" + callerMember + ":" + callerLineNumber).Error(msg);
6869
}
6970

7071
/// <summary>
@@ -74,9 +75,9 @@ public static void Error(string msg, [CallerFilePath] string callerFile = "",
7475
/// <param name="e">抛的异常</param>
7576
/// <param name="callerMember">调用的函数名字</param>
7677
/// <param name="callerLineNumber">调用的具体行数</param>
77-
public static void Error(string msg, Exception e, [CallerFilePath] string callerFile = "",
78-
[CallerMemberName] string callerMember = "", [CallerLineNumber] int callerLineNumber = 0)
78+
public static void Error(String msg, Exception e, [CallerFilePath] String callerFile = "",
79+
[CallerMemberName] String callerMember = "", [CallerLineNumber] Int32 callerLineNumber = 0)
7980
{
80-
LogManager.GetLogger(callerFile.Split('.')[^2].Split('\\')[^1] + ":" + callerMember + ":" + callerLineNumber).Error(msg, e);
81+
LogManager.GetLogger(Path.GetFileNameWithoutExtension(callerFile) + ":" + callerMember + ":" + callerLineNumber).Error(msg, e);
8182
}
8283
}
+17-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using log4net;
2+
using System.IO;
23
using System.Reflection;
34
using System.Runtime.CompilerServices;
45

@@ -15,7 +16,7 @@ public class Logger
1516
static Logger()
1617
{
1718
Assembly assembly = Assembly.GetExecutingAssembly();
18-
var xml = assembly.GetManifestResourceStream("CaliburnMicroLearn.LoggerUtils.log4net.config");
19+
var xml = assembly.GetManifestResourceStream("WPFCustomControl.LoggerUtils.log4net.config");
1920
log4net.Config.XmlConfigurator.Configure(xml);
2021
}
2122

@@ -25,10 +26,10 @@ static Logger()
2526
/// <param name="msg">具体的log信息</param>
2627
/// <param name="callerMember">调用的函数名字</param>
2728
/// <param name="callerLineNumber">调用的具体行数</param>
28-
public static void Debug(string msg, [CallerFilePath] string callerFile = "",
29-
[CallerMemberName] string callerMember = "", [CallerLineNumber] int callerLineNumber = 0)
29+
public static void Debug(String msg, [CallerFilePath] String callerFile = "",
30+
[CallerMemberName] String callerMember = "", [CallerLineNumber] Int32 callerLineNumber = 0)
3031
{
31-
LogManager.GetLogger(callerFile.Split('.')[^2].Split('\\')[^1] + ":" + callerMember + ":" + callerLineNumber).Debug(msg);
32+
LogManager.GetLogger(Path.GetFileNameWithoutExtension(callerFile) + ":" + callerMember + ":" + callerLineNumber).Debug(msg);
3233
}
3334

3435
/// <summary>
@@ -37,10 +38,10 @@ public static void Debug(string msg, [CallerFilePath] string callerFile = "",
3738
/// <param name="msg">具体的log信息</param>
3839
/// <param name="callerMember">调用的函数名字</param>
3940
/// <param name="callerLineNumber">调用的具体行数</param>
40-
public static void Info(string msg, [CallerFilePath] string callerFile = "",
41-
[CallerMemberName] string callerMember = "", [CallerLineNumber] int callerLineNumber = 0)
41+
public static void Info(String msg, [CallerFilePath] String callerFile = "",
42+
[CallerMemberName] String callerMember = "", [CallerLineNumber] Int32 callerLineNumber = 0)
4243
{
43-
LogManager.GetLogger(callerFile.Split('.')[^2].Split('\\')[^1] + ":" + callerMember + ":" + callerLineNumber).Info(msg);
44+
LogManager.GetLogger(Path.GetFileNameWithoutExtension(callerFile) + ":" + callerMember + ":" + callerLineNumber).Info(msg);
4445
}
4546

4647
/// <summary>
@@ -49,10 +50,10 @@ public static void Info(string msg, [CallerFilePath] string callerFile = "",
4950
/// <param name="msg">具体的log信息</param>
5051
/// <param name="callerMember">调用的函数名字</param>
5152
/// <param name="callerLineNumber">调用的具体行数</param>
52-
public static void Warn(string msg, [CallerFilePath] string callerFile = "",
53-
[CallerMemberName] string callerMember = "", [CallerLineNumber] int callerLineNumber = 0)
53+
public static void Warn(String msg, [CallerFilePath] String callerFile = "",
54+
[CallerMemberName] String callerMember = "", [CallerLineNumber] Int32 callerLineNumber = 0)
5455
{
55-
LogManager.GetLogger(callerFile.Split('.')[^2].Split('\\')[^1] + ":" + callerMember + ":" + callerLineNumber).Warn(msg);
56+
LogManager.GetLogger(Path.GetFileNameWithoutExtension(callerFile) + ":" + callerMember + ":" + callerLineNumber).Warn(msg);
5657
}
5758

5859
/// <summary>
@@ -61,10 +62,10 @@ public static void Warn(string msg, [CallerFilePath] string callerFile = "",
6162
/// <param name="msg">具体的log信息</param>
6263
/// <param name="callerMember">调用的函数名字</param>
6364
/// <param name="callerLineNumber">调用的具体行数</param>
64-
public static void Error(string msg, [CallerFilePath] string callerFile = "",
65-
[CallerMemberName] string callerMember = "", [CallerLineNumber] int callerLineNumber = 0)
65+
public static void Error(String msg, [CallerFilePath] String callerFile = "",
66+
[CallerMemberName] String callerMember = "", [CallerLineNumber] Int32 callerLineNumber = 0)
6667
{
67-
LogManager.GetLogger(callerFile.Split('.')[^2].Split('\\')[^1] + ":" + callerMember + ":" + callerLineNumber).Error(msg);
68+
LogManager.GetLogger(Path.GetFileNameWithoutExtension(callerFile) + ":" + callerMember + ":" + callerLineNumber).Error(msg);
6869
}
6970

7071
/// <summary>
@@ -74,9 +75,9 @@ public static void Error(string msg, [CallerFilePath] string callerFile = "",
7475
/// <param name="e">抛的异常</param>
7576
/// <param name="callerMember">调用的函数名字</param>
7677
/// <param name="callerLineNumber">调用的具体行数</param>
77-
public static void Error(string msg, Exception e, [CallerFilePath] string callerFile = "",
78-
[CallerMemberName] string callerMember = "", [CallerLineNumber] int callerLineNumber = 0)
78+
public static void Error(String msg, Exception e, [CallerFilePath] String callerFile = "",
79+
[CallerMemberName] String callerMember = "", [CallerLineNumber] Int32 callerLineNumber = 0)
7980
{
80-
LogManager.GetLogger(callerFile.Split('.')[^2].Split('\\')[^1] + ":" + callerMember + ":" + callerLineNumber).Error(msg, e);
81+
LogManager.GetLogger(Path.GetFileNameWithoutExtension(callerFile) + ":" + callerMember + ":" + callerLineNumber).Error(msg, e);
8182
}
8283
}

‎WPFCustomControl/Readme.md

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
# 框架建立
22
1. 使用CaliburnMicro的模板建立工程
3-
2. 创建CustomControls文件夹,里面放各种自定义控件。其中cs文件是继承自原生控件的各种类,
3+
2. 创建Themes文件夹,里面放各种自定义控件。其中cs文件是继承自原生控件的各种类,
44
Styles文件夹中是各个控件对应的Style。Generic.xaml是创建自定义控件必须存在的,名字也不能改,
55
里面就用来引用其他控件的Style.xaml
66
```xaml
77
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
88
<ResourceDictionary.MergedDictionaries>
9-
<ResourceDictionary Source="pack://application:,,,/WPFCustomControl;component/CustomControls/Styles/RWindowStyle.xaml" />
9+
<ResourceDictionary Source="pack://application:,,,/WPFCustomControl;component/Themes/Styles/CustomWindowStyle.xaml" />
1010
</ResourceDictionary.MergedDictionaries>
1111
</ResourceDictionary>
1212
```
13-
ShellView中的Window也可以换成RWindow
13+
ShellView中的Window也可以换成CustomWindow
1414
```xaml
15-
<customControls:RWindow x:Class="WPFCustomControl.Views.ShellView"
16-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
17-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
18-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
19-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
20-
xmlns:local="clr-namespace:WPFCustomControl.Views"
21-
xmlns:customControls="clr-namespace:WPFCustomControl.CustomControls"
22-
xmlns:cal="http://caliburnmicro.com"
23-
mc:Ignorable="d"
24-
Title="CaliburnMicro"
25-
xmlns:viewModels="clr-namespace:WPFCustomControl.ViewModels"
26-
d:DataContext="{d:DesignInstance Type=viewModels:ShellViewModel,IsDesignTimeCreatable=True}"
27-
Height="450"
28-
Width="800">
29-
30-
</customControls:RWindow>
15+
<customControls:CustomWindow x:Class="WPFCustomControl.Views.ShellView"
16+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
17+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
18+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
19+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
20+
xmlns:customControls="clr-namespace:WPFCustomControl.Themes"
21+
xmlns:cal="http://caliburnmicro.com"
22+
mc:Ignorable="d"
23+
xmlns:viewModels="clr-namespace:WPFCustomControl.ViewModels"
24+
d:DataContext="{d:DesignInstance Type=viewModels:ShellViewModel,IsDesignTimeCreatable=False}"
25+
Width="800"
26+
Height="600">
27+
</customControls:CustomWindow>
3128
```
32-
# RWindow
29+
# CustomWindow
30+
第一个实现的自定义控件就是自定义窗口。主要需要实现以下功能:
31+
1. 自定义标题栏
32+
2. 最小化、最大化、关闭、拖动
33+
3. 圆角
34+
4. 不规则形状窗口

‎WPFCustomControl/Themes/CustomWindow.cs

+23-101
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Windows.Controls;
55
using System.Windows.Input;
66
using System.Windows.Interop;
7-
using WPFCustomControl.LoggerUtils;
87

98
namespace WPFCustomControl.Themes;
109

@@ -15,16 +14,17 @@ static CustomWindow()
1514
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWindow), new FrameworkPropertyMetadata(typeof(CustomWindow)));
1615
}
1716

17+
[DllImport("user32")]
18+
internal static extern IntPtr MonitorFromWindow([In] IntPtr handle, [In] Int32 flags);
19+
1820
[DllImport("user32", EntryPoint = "GetMonitorInfoW", ExactSpelling = true, CharSet = CharSet.Unicode)]
1921
[return: MarshalAs(UnmanagedType.Bool)]
2022
internal static extern bool GetMonitorInfo([In] IntPtr hMonitor, [Out] MONITORINFO lpmi);
2123

22-
[DllImport("user32")]
23-
internal static extern IntPtr MonitorFromWindow([In] IntPtr handle, [In] int flags);
24-
2524
[DllImport("user32.dll")]
2625
[return: MarshalAs(UnmanagedType.Bool)]
27-
internal static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
26+
internal static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
27+
Int32 X, Int32 Y, Int32 cx, Int32 cy, UInt32 uFlags);
2828

2929
/// <summary>
3030
/// 这个窗口的handle
@@ -42,9 +42,9 @@ public CustomWindow()
4242
CornerRadius = new CornerRadius(0),
4343
GlassFrameThickness = new Thickness(0),
4444
};
45-
//用WindowChrome的方式来制作自定义窗口
45+
//用WindowChrome的方式来制作自定义窗口,这样可以保留Resize的功能
4646
System.Windows.Shell.WindowChrome.SetWindowChrome(this, windowChrome);
47-
AllowsTransparency = false;
47+
//设置为无边框窗口,不然最大化时显示不正确
4848
WindowStyle = WindowStyle.None;
4949
}
5050

@@ -162,117 +162,39 @@ protected override void OnStateChanged(EventArgs e)
162162
base.OnStateChanged(e);
163163
if (WindowState == WindowState.Maximized)
164164
{
165+
//根据显示器找到左上角的坐标以及窗口最大化时的宽和高,再用win32的函数设置位置
165166
IntPtr monitor = MonitorFromWindow(_handle, 0x00000002);
166167
var monitorInfo = new MONITORINFO();
167168
GetMonitorInfo(monitor, monitorInfo);
168169
var x = monitorInfo.rcMonitor.left;
169170
var y = monitorInfo.rcMonitor.top;
170171
var cx = Math.Abs(monitorInfo.rcMonitor.right - x);
171172
var cy = Math.Abs(monitorInfo.rcMonitor.bottom - y);
172-
Logger.Debug($"x:{x},y:{y},cx:{cx},cy:{cy}");
173173
SetWindowPos(_handle, new IntPtr(-2), x, y, cx, cy, 0x0040);
174174
}
175-
BorderThickness = new Thickness(0);
176175
}
177176
}
178177

178+
/// <summary>
179+
/// 传入win32函数需要的结构体
180+
/// </summary>
179181
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
180182
internal class MONITORINFO
181183
{
182-
public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
183-
public RECT rcMonitor = new RECT();
184-
public RECT rcWork = new RECT();
185-
public int dwFlags = 0;
186-
187-
public enum MonitorOptions : uint
188-
{
189-
MONITOR_DEFAULTTONULL = 0x00000000,
190-
MONITOR_DEFAULTTOPRIMARY = 0x00000001,
191-
MONITOR_DEFAULTTONEAREST = 0x00000002
192-
}
184+
public Int32 cbSize = Marshal.SizeOf(typeof(MONITORINFO));
185+
public RECT rcMonitor = new();
186+
public RECT rcWork = new();
187+
public Int32 dwFlags = 0;
193188
}
194189

190+
/// <summary>
191+
/// 传入win32函数需要的结构体
192+
/// </summary>
195193
[StructLayout(LayoutKind.Sequential, Pack = 0)]
196-
internal struct RECT
197-
{
198-
public int left;
199-
public int top;
200-
public int right;
201-
public int bottom;
202-
203-
public static readonly RECT Empty = new RECT();
204-
205-
public int Width
206-
{
207-
get { return Math.Abs(right - left); } // Abs needed for BIDI OS
208-
}
209-
210-
public int Height
211-
{
212-
get { return bottom - top; }
213-
}
214-
215-
public RECT(int left, int top, int right, int bottom)
216-
{
217-
this.left = left;
218-
this.top = top;
219-
this.right = right;
220-
this.bottom = bottom;
221-
}
222-
223-
public RECT(RECT rcSrc)
224-
{
225-
left = rcSrc.left;
226-
top = rcSrc.top;
227-
right = rcSrc.right;
228-
bottom = rcSrc.bottom;
229-
}
230-
231-
public bool IsEmpty
232-
{
233-
get
234-
{
235-
// BUGBUG : On Bidi OS (hebrew arabic) left > right
236-
return left >= right || top >= bottom;
237-
}
238-
}
239-
240-
public override string ToString()
241-
{
242-
if (this == Empty)
243-
return "RECT {Empty}";
244-
return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }";
245-
}
246-
247-
/// <summary> Determine if 2 RECT are equal (deep compare) </summary>
248-
public override bool Equals(object obj)
249-
{
250-
if (!(obj is Rect)) { return false; }
251-
return (this == (RECT)obj);
252-
}
253-
254-
/// <summary>Return the HashCode for this struct (not garanteed to be unique)</summary>
255-
public override int GetHashCode()
256-
{
257-
return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
258-
}
259-
260-
public static bool operator ==(RECT rect1, RECT rect2)
261-
{
262-
return (rect1.left == rect2.left && rect1.top == rect2.top && rect1.right == rect2.right && rect1.bottom == rect2.bottom);
263-
}
264-
265-
public static bool operator !=(RECT rect1, RECT rect2)
266-
{
267-
return !(rect1 == rect2);
268-
}
269-
}
270-
271-
[StructLayout(LayoutKind.Sequential)]
272-
internal struct MARGINS
194+
internal struct RECT(Int32 left, Int32 top, Int32 right, Int32 bottom)
273195
{
274-
public int leftWidth;
275-
public int rightWidth;
276-
public int topHeight;
277-
public int bottomHeight;
196+
public Int32 left = left;
197+
public Int32 top = top;
198+
public Int32 right = right;
199+
public Int32 bottom = bottom;
278200
}

0 commit comments

Comments
 (0)
Please sign in to comment.