Skip to content

Commit 79b730d

Browse files
committed
update in issues.
1 parent 66afe04 commit 79b730d

File tree

9 files changed

+136
-64
lines changed

9 files changed

+136
-64
lines changed

SoftwareHelper/App.xaml.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ protected override void OnStartup(StartupEventArgs e)
7171
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
7272
}
7373

74-
74+
protected override void OnExit(ExitEventArgs e)
75+
{
76+
base.OnExit(e);
77+
Win32Api.UnRegisterDesktop(true);
78+
}
7579
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
7680
{
7781
try

SoftwareHelper/Helpers/Win32Api.cs

+63-17
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
using SoftwareHelper.Views;
1+
using SoftwareHelper.ViewModels;
2+
using SoftwareHelper.Views;
23
using System;
34
using System.Drawing;
45
using System.Runtime.InteropServices;
56
using System.Text;
7+
using System.Threading;
8+
using System.Threading.Tasks;
69
using System.Windows;
710
using System.Windows.Input;
811
using System.Windows.Interop;
912
using System.Windows.Media;
1013
using System.Windows.Media.Animation;
1114
using System.Windows.Media.Imaging;
15+
using System.Windows.Threading;
1216

1317
namespace SoftwareHelper.Helpers
1418
{
@@ -183,6 +187,15 @@ internal struct RECT
183187
#endregion
184188

185189

190+
static readonly IntPtr GWL_EXSTYLE = new IntPtr(-20);
191+
static readonly UInt32 WS_EX_TOPMOST = 0x0008;
192+
193+
[DllImport("user32.dll", SetLastError = true)]
194+
static extern UInt32 GetWindowLong(IntPtr hWnd, IntPtr nIndex);
195+
static bool IsTopMost(IntPtr hwnd)
196+
{
197+
return (GetWindowLong(hwnd, GWL_EXSTYLE) & (int)WS_EX_TOPMOST) != 0;
198+
}
186199

187200
[DllImport("shell32.dll")]
188201
public static extern UInt32 SHAppBarMessage(UInt32 dwMessage, ref APPBARDATA pData);
@@ -220,6 +233,7 @@ public struct RECT1
220233
public int bottom;
221234
}
222235
private static EmbedDeasktopView _window;
236+
private static HwndSource mainWindowSrc;
223237
/// <summary>
224238
/// 注册
225239
/// </summary>
@@ -229,16 +243,26 @@ public static void RegisterDesktop(EmbedDeasktopView window = null)
229243
if (window != null)
230244
_window = window;
231245
WindowInteropHelper helper = new WindowInteropHelper(_window);
232-
HwndSource mainWindowSrc = (HwndSource)HwndSource.FromHwnd(helper.Handle);
233-
246+
mainWindowSrc = (HwndSource)HwndSource.FromHwnd(helper.Handle);
247+
if (task != null)
248+
{
249+
cancelTokenSource.Cancel();
250+
task.Wait();
251+
task.Dispose();
252+
task = null;
253+
}
254+
MoveWindowDPI();
255+
}
256+
static void MoveWindowDPI(bool isDpi = false)
257+
{
234258
APPBARDATA abd = new APPBARDATA();
235259
abd.cbSize = (uint)Marshal.SizeOf(abd);
236260
abd.hWnd = mainWindowSrc.Handle;
237261
abd.uEdge = 2;
238262
abd.rc.top = 0;
239263

240264
var source = PresentationSource.FromVisual(_window);
241-
var dpiX = source.CompositionTarget.TransformToDevice.M11;
265+
var dpiX = source.CompositionTarget.TransformToDevice.M11;
242266
var dpiY = source.CompositionTarget.TransformToDevice.M22;
243267

244268
_window.Width = 90;
@@ -256,33 +280,55 @@ public static void RegisterDesktop(EmbedDeasktopView window = null)
256280
else
257281
abd.rc.bottom = workingAreaSize.Height - (desktopSize.Height - workingAreaSize.Height);
258282
abd.rc.left = abd.rc.right - (int)_window.ActualWidth;
259-
260-
//注册新的应用栏,并指定系统应用于向应用栏发送通知消息的消息标识符。
261-
SHAppBarMessage((UInt32)AppBarMessages.New, ref abd);
262-
//请求应用栏的大小和屏幕位置。
263-
SHAppBarMessage((UInt32)AppBarMessages.QueryPos, ref abd);
264-
//设置应用栏的大小和屏幕位置。
265-
SHAppBarMessage((UInt32)AppBarMessages.SetPos, ref abd);
266-
//设置应用所在平面位置。
283+
if (!isDpi)
284+
{
285+
//注册新的应用栏,并指定系统应用于向应用栏发送通知消息的消息标识符。
286+
SHAppBarMessage((UInt32)AppBarMessages.New, ref abd);
287+
//请求应用栏的大小和屏幕位置。
288+
SHAppBarMessage((UInt32)AppBarMessages.QueryPos, ref abd);
289+
//设置应用栏的大小和屏幕位置。
290+
SHAppBarMessage((UInt32)AppBarMessages.SetPos, ref abd);
291+
//设置应用所在平面位置。
292+
}
267293
MoveWindow(abd.hWnd, abd.rc.left, 0, (int)_window.ActualWidth, abd.rc.bottom, 1);
268294
}
295+
296+
297+
static Task task;
298+
static CancellationTokenSource cancelTokenSource;
269299
/// <summary>
270300
/// 卸载
271301
/// </summary>
272302
/// <param name="window"></param>
273-
public static void UnRegisterDesktop()
303+
public static void UnRegisterDesktop(bool isExit = false)
274304
{
275-
WindowInteropHelper helper = new WindowInteropHelper(_window);
276-
HwndSource mainWindowSrc = (HwndSource)HwndSource.FromHwnd(helper.Handle);
277-
305+
if (mainWindowSrc == null) return;
278306
APPBARDATA abd = new APPBARDATA();
279307
abd.cbSize = (uint)Marshal.SizeOf(abd);
280308
abd.hWnd = mainWindowSrc.Handle;
281309

282310
SHAppBarMessage((UInt32)AppBarMessages.Remove, ref abd);
283311

284312
_window.ExitEmbedded();
285-
_window.Topmost = true;
313+
if (isExit) return;
314+
315+
cancelTokenSource = new CancellationTokenSource();
316+
task = new Task(() =>
317+
{
318+
while (true)
319+
{
320+
if (cancelTokenSource.IsCancellationRequested) break;
321+
//if (IsTopMost(mainWindowSrc.Handle)) continue;
322+
_window.Dispatcher.BeginInvoke(new Action(delegate
323+
{
324+
_window.Topmost = false;
325+
_window.Topmost = true;
326+
}));
327+
Thread.Sleep(1000);
328+
}
329+
}, cancelTokenSource.Token);
330+
task.Start();
331+
286332
}
287333
[DllImport("gdi32.dll")]
288334
static extern int GetDeviceCaps(

SoftwareHelper/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@
5151
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
5252
// 方法是按如下所示使用“*”:
5353
// [assembly: AssemblyVersion("1.0.*")]
54-
[assembly: AssemblyVersion("10.4.3.0")]
55-
[assembly: AssemblyFileVersion("10.4.3.0")]
54+
[assembly: AssemblyVersion("0.4.3.0")]
55+
[assembly: AssemblyFileVersion("0.4.3.0")]

SoftwareHelper/SoftwareHelper.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
<StartupObject>SoftwareHelper.App</StartupObject>
6464
</PropertyGroup>
6565
<PropertyGroup />
66+
<PropertyGroup>
67+
<ApplicationManifest>app.manifest</ApplicationManifest>
68+
</PropertyGroup>
6669
<ItemGroup>
6770
<Reference Include="GongSolutions.Wpf.DragDrop, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
6871
<SpecificVersion>False</SpecificVersion>

SoftwareHelper/ViewModels/MainVM.cs

+20-14
Original file line numberDiff line numberDiff line change
@@ -301,40 +301,46 @@ private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
301301
public ICommand ScreenCutCommand => new RelayCommand(obj =>
302302
{
303303
IsOpenContextMenu = false;
304-
Thread.Sleep(1000);
304+
Keyboard.ClearFocus();
305305
var screenCut = new WPFDevelopers.Controls.ScreenCut();
306306
screenCut.ShowDialog();
307307

308308
});
309309
#endregion
310310

311311
#region 方法
312-
312+
private Color color;
313+
private string hex;
313314
private void Timer_Tick(object sender, EventArgs e)
314315
{
315316
var point = new MousePoint.POINT();
316317
var isMouseDown = MousePoint.GetCursorPos(out point);
317-
var color = Win32Api.GetPixelColor(point.X, point.Y);
318-
319-
if (point.X >= desktopWorkingArea.Width)
320-
colorView.Left = point.X - 40;
318+
color = Win32Api.GetPixelColor(point.X, point.Y);
319+
var source = PresentationSource.FromVisual(colorView);
320+
var dpiX = source.CompositionTarget.TransformToDevice.M11;
321+
var dpiY = source.CompositionTarget.TransformToDevice.M22;
322+
if (point.X / dpiX >= desktopWorkingArea.Width)
323+
colorView.Left = point.X / dpiX - 40;
321324
else if (point.X <= 10)
322-
colorView.Left = point.X;
325+
colorView.Left = point.X / dpiX;
323326
else
324-
colorView.Left = point.X;
325-
326-
if (point.Y >= desktopWorkingArea.Height - 40)
327-
colorView.Top = point.Y - 40;
327+
colorView.Left = point.X / dpiX;
328+
if (point.Y / dpiY >= desktopWorkingArea.Height - 40)
329+
colorView.Top = point.Y / dpiY - 40;
328330
else
329-
colorView.Top = point.Y;
331+
colorView.Top = point.Y / dpiY;
330332

331333
colorView.MouseColor = new SolidColorBrush(color);
334+
hex = color.ToString();
335+
if (hex.Length > 7)
336+
hex = hex.Remove(1, 2);
337+
colorView.MouseColorText = hex;
332338
}
333339

334340
private void MouseHook_MouseDown(object sender, MouseEventArgs e)
335341
{
336-
Clipboard.SetText(colorView.MouseColor.ToString());
337-
ShowBalloon("提示", $"已复制到剪切板 {colorView.MouseColor.ToString()} SoftwareHelper");
342+
Clipboard.SetText(hex);
343+
ShowBalloon("提示", $"已复制到剪切板 {hex} SoftwareHelper");
338344
if (_timer.IsEnabled)
339345
{
340346
_timer.Stop();

SoftwareHelper/Views/EmbedDeasktopView.xaml.cs

+8-14
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ private void OnHookKeyDown(object sender, HookEventArgs e)
5050
SetKeyDown(e.Key);
5151
if (IsKeyDown(Key.PrintScreen))
5252
{
53-
var screenCut = new ScreenCut();
54-
screenCut.ShowDialog();
53+
var screenCut = new ScreenCut() {Topmost = true};
54+
screenCut.Activate();
55+
screenCut.Closing += delegate
56+
{
57+
SetKeyUp(Key.PrintScreen);
58+
};
59+
screenCut.ShowDialog();
5560
}
5661
else
5762
{
@@ -112,16 +117,6 @@ private void IMEUS()
112117
Win32Api.LoadKeyboardLayout("0000409", Win32Api.KLF_ACTIVATE));
113118
}
114119

115-
private void EmbedDeasktopView_KeyUp(object sender, KeyEventArgs e)
116-
{
117-
Thread.Sleep(300);
118-
KeyDownPanel.Visibility = Visibility.Collapsed;
119-
}
120-
121-
private void EmbedDeasktopView_KeyDown(object sender, KeyEventArgs e)
122-
{
123-
}
124-
125120
private void EmbedDeasktopView_Loaded(object sender, RoutedEventArgs e)
126121
{
127122
#region 注释
@@ -133,10 +128,9 @@ private void EmbedDeasktopView_Loaded(object sender, RoutedEventArgs e)
133128

134129
#endregion
135130
}
136-
137131
private void EmbedDeasktopView_Closing(object sender, CancelEventArgs e)
138132
{
139-
Win32Api.UnRegisterDesktop();
133+
Win32Api.UnRegisterDesktop(true);
140134
}
141135

142136
protected override void OnSourceInitialized(EventArgs e)

SoftwareHelper/Views/WindowColor.xaml

+20-12
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,31 @@
77
xmlns:shell="https://github.com/WPFDevelopersOrg.WPFDevelopers.Minimal"
88
mc:Ignorable="d"
99
Title="WindowColor"
10-
UseLayoutRounding="True" ResizeMode="NoResize" Height="80" Width="80"
10+
UseLayoutRounding="True" ResizeMode="NoResize" Height="60"
11+
SizeToContent="Width"
1112
SnapsToDevicePixels="True" WindowStyle="None" Background="Transparent"
1213
Topmost="True" ShowInTaskbar="False" >
1314
<shell:WindowChrome.WindowChrome>
1415
<shell:WindowChrome GlassFrameThickness="-1" CaptionHeight="0"/>
1516
</shell:WindowChrome.WindowChrome>
16-
<Grid Margin="10">
17-
<!--<Grid.RowDefinitions>
18-
<RowDefinition/>
19-
<RowDefinition Height="Auto"/>
20-
</Grid.RowDefinitions>
21-
<Border Grid.RowSpan="2" Background="White" Effect="{StaticResource NormalShadowDepth}"/>
22-
<Rectangle Grid.Row="0"
17+
<Border Grid.ColumnSpan="2" Margin="10"
18+
CornerRadius="4"
19+
Background="{StaticResource BaseSolidColorBrush}"
20+
Effect="{StaticResource NormalShadowDepth}">
21+
<Grid Height="30">
22+
<Grid.ColumnDefinitions>
23+
<ColumnDefinition Width="40"/>
24+
<ColumnDefinition Width="Auto"/>
25+
</Grid.ColumnDefinitions>
26+
<Rectangle Grid.Column="0" RadiusX="4" RadiusY="4" Margin="4"
2327
Fill="{Binding MouseColor,RelativeSource={RelativeSource AncestorType={x:Type local:WindowColor}}}"/>
24-
<TextBlock Grid.Row="1" Text="{Binding MouseColor,RelativeSource={RelativeSource AncestorType={x:Type local:WindowColor}}}"/>-->
25-
<Border Background="White" Effect="{StaticResource NormalShadowDepth}" CornerRadius="40"/>
28+
<TextBlock Grid.Column="1" VerticalAlignment="Center"
29+
FontWeight="Black" Margin="4,0,6,0"
30+
FontSize="14"
31+
Text="{Binding MouseColorText,RelativeSource={RelativeSource AncestorType={x:Type local:WindowColor}}}"/>
32+
<!--<Border Background="White" Effect="{StaticResource NormalShadowDepth}" CornerRadius="40"/>
2633
<Ellipse Fill="{Binding MouseColor,RelativeSource={RelativeSource AncestorType={x:Type local:WindowColor}}}"
27-
Stroke="Transparent" StrokeThickness="8"/>
28-
</Grid>
34+
Stroke="Transparent" StrokeThickness="8"/>-->
35+
</Grid>
36+
</Border>
2937
</Window>

SoftwareHelper/Views/WindowColor.xaml.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ public Brush MouseColor
1515
set { SetValue(MouseColorProperty, value); }
1616
}
1717

18-
// Using a DependencyProperty as the backing store for MouseColor. This enables animation, styling, binding, etc...
1918
public static readonly DependencyProperty MouseColorProperty =
2019
DependencyProperty.Register("MouseColor", typeof(Brush), typeof(WindowColor), new PropertyMetadata(null));
2120

2221

22+
23+
public string MouseColorText
24+
{
25+
get { return (string)GetValue(MouseColorTextProperty); }
26+
set { SetValue(MouseColorTextProperty, value); }
27+
}
28+
29+
public static readonly DependencyProperty MouseColorTextProperty =
30+
DependencyProperty.Register("MouseColorText", typeof(string), typeof(WindowColor), new PropertyMetadata(null));
31+
32+
2333
public WindowColor()
2434
{
2535
InitializeComponent();

SoftwareHelper/app.manifest

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
4040

4141
<!-- Windows 10 -->
42-
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
42+
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
4343

4444
</application>
4545
</compatibility>
@@ -48,12 +48,13 @@
4848
自动缩放。Windows Presentation Foundation (WPF)应用程序自动感知 DPI,无需
4949
选择加入。选择加入此设置的 Windows 窗体应用程序(目标设定为 .NET Framework 4.6 )还应
5050
在其 app.config 中将 "EnableWindowsFormsHighDpiAutoResizing" 设置设置为 "true"。-->
51+
5152
<application xmlns="urn:schemas-microsoft-com:asm.v3">
5253
<windowsSettings>
53-
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitor</dpiAwareness>
54-
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
54+
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
5555
</windowsSettings>
5656
</application>
57+
5758

5859
<!-- 启用 Windows 公共控件和对话框的主题(Windows XP 和更高版本) -->
5960
<!--

0 commit comments

Comments
 (0)