Skip to content

Commit

Permalink
update in issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanjinhuagood committed Jun 23, 2022
1 parent 66afe04 commit 79b730d
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 64 deletions.
6 changes: 5 additions & 1 deletion SoftwareHelper/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ protected override void OnStartup(StartupEventArgs e)
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}


protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
Win32Api.UnRegisterDesktop(true);
}
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
try
Expand Down
80 changes: 63 additions & 17 deletions SoftwareHelper/Helpers/Win32Api.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using SoftwareHelper.Views;
using SoftwareHelper.ViewModels;
using SoftwareHelper.Views;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace SoftwareHelper.Helpers
{
Expand Down Expand Up @@ -183,6 +187,15 @@ internal struct RECT
#endregion


static readonly IntPtr GWL_EXSTYLE = new IntPtr(-20);
static readonly UInt32 WS_EX_TOPMOST = 0x0008;

[DllImport("user32.dll", SetLastError = true)]
static extern UInt32 GetWindowLong(IntPtr hWnd, IntPtr nIndex);
static bool IsTopMost(IntPtr hwnd)
{
return (GetWindowLong(hwnd, GWL_EXSTYLE) & (int)WS_EX_TOPMOST) != 0;
}

[DllImport("shell32.dll")]
public static extern UInt32 SHAppBarMessage(UInt32 dwMessage, ref APPBARDATA pData);
Expand Down Expand Up @@ -220,6 +233,7 @@ public struct RECT1
public int bottom;
}
private static EmbedDeasktopView _window;
private static HwndSource mainWindowSrc;
/// <summary>
/// 注册
/// </summary>
Expand All @@ -229,16 +243,26 @@ public static void RegisterDesktop(EmbedDeasktopView window = null)
if (window != null)
_window = window;
WindowInteropHelper helper = new WindowInteropHelper(_window);
HwndSource mainWindowSrc = (HwndSource)HwndSource.FromHwnd(helper.Handle);

mainWindowSrc = (HwndSource)HwndSource.FromHwnd(helper.Handle);
if (task != null)
{
cancelTokenSource.Cancel();
task.Wait();
task.Dispose();
task = null;
}
MoveWindowDPI();
}
static void MoveWindowDPI(bool isDpi = false)
{
APPBARDATA abd = new APPBARDATA();
abd.cbSize = (uint)Marshal.SizeOf(abd);
abd.hWnd = mainWindowSrc.Handle;
abd.uEdge = 2;
abd.rc.top = 0;

var source = PresentationSource.FromVisual(_window);
var dpiX = source.CompositionTarget.TransformToDevice.M11;
var dpiX = source.CompositionTarget.TransformToDevice.M11;
var dpiY = source.CompositionTarget.TransformToDevice.M22;

_window.Width = 90;
Expand All @@ -256,33 +280,55 @@ public static void RegisterDesktop(EmbedDeasktopView window = null)
else
abd.rc.bottom = workingAreaSize.Height - (desktopSize.Height - workingAreaSize.Height);
abd.rc.left = abd.rc.right - (int)_window.ActualWidth;

//注册新的应用栏,并指定系统应用于向应用栏发送通知消息的消息标识符。
SHAppBarMessage((UInt32)AppBarMessages.New, ref abd);
//请求应用栏的大小和屏幕位置。
SHAppBarMessage((UInt32)AppBarMessages.QueryPos, ref abd);
//设置应用栏的大小和屏幕位置。
SHAppBarMessage((UInt32)AppBarMessages.SetPos, ref abd);
//设置应用所在平面位置。
if (!isDpi)
{
//注册新的应用栏,并指定系统应用于向应用栏发送通知消息的消息标识符。
SHAppBarMessage((UInt32)AppBarMessages.New, ref abd);
//请求应用栏的大小和屏幕位置。
SHAppBarMessage((UInt32)AppBarMessages.QueryPos, ref abd);
//设置应用栏的大小和屏幕位置。
SHAppBarMessage((UInt32)AppBarMessages.SetPos, ref abd);
//设置应用所在平面位置。
}
MoveWindow(abd.hWnd, abd.rc.left, 0, (int)_window.ActualWidth, abd.rc.bottom, 1);
}


static Task task;
static CancellationTokenSource cancelTokenSource;
/// <summary>
/// 卸载
/// </summary>
/// <param name="window"></param>
public static void UnRegisterDesktop()
public static void UnRegisterDesktop(bool isExit = false)
{
WindowInteropHelper helper = new WindowInteropHelper(_window);
HwndSource mainWindowSrc = (HwndSource)HwndSource.FromHwnd(helper.Handle);

if (mainWindowSrc == null) return;
APPBARDATA abd = new APPBARDATA();
abd.cbSize = (uint)Marshal.SizeOf(abd);
abd.hWnd = mainWindowSrc.Handle;

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

_window.ExitEmbedded();
_window.Topmost = true;
if (isExit) return;

cancelTokenSource = new CancellationTokenSource();
task = new Task(() =>
{
while (true)
{
if (cancelTokenSource.IsCancellationRequested) break;
//if (IsTopMost(mainWindowSrc.Handle)) continue;
_window.Dispatcher.BeginInvoke(new Action(delegate
{
_window.Topmost = false;
_window.Topmost = true;
}));
Thread.Sleep(1000);
}
}, cancelTokenSource.Token);
task.Start();

}
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(
Expand Down
4 changes: 2 additions & 2 deletions SoftwareHelper/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("10.4.3.0")]
[assembly: AssemblyFileVersion("10.4.3.0")]
[assembly: AssemblyVersion("0.4.3.0")]
[assembly: AssemblyFileVersion("0.4.3.0")]
3 changes: 3 additions & 0 deletions SoftwareHelper/SoftwareHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
<StartupObject>SoftwareHelper.App</StartupObject>
</PropertyGroup>
<PropertyGroup />
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="GongSolutions.Wpf.DragDrop, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
34 changes: 20 additions & 14 deletions SoftwareHelper/ViewModels/MainVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,40 +301,46 @@ private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
public ICommand ScreenCutCommand => new RelayCommand(obj =>
{
IsOpenContextMenu = false;
Thread.Sleep(1000);
Keyboard.ClearFocus();
var screenCut = new WPFDevelopers.Controls.ScreenCut();
screenCut.ShowDialog();

});
#endregion

#region 方法

private Color color;
private string hex;
private void Timer_Tick(object sender, EventArgs e)
{
var point = new MousePoint.POINT();
var isMouseDown = MousePoint.GetCursorPos(out point);
var color = Win32Api.GetPixelColor(point.X, point.Y);

if (point.X >= desktopWorkingArea.Width)
colorView.Left = point.X - 40;
color = Win32Api.GetPixelColor(point.X, point.Y);
var source = PresentationSource.FromVisual(colorView);
var dpiX = source.CompositionTarget.TransformToDevice.M11;
var dpiY = source.CompositionTarget.TransformToDevice.M22;
if (point.X / dpiX >= desktopWorkingArea.Width)
colorView.Left = point.X / dpiX - 40;
else if (point.X <= 10)
colorView.Left = point.X;
colorView.Left = point.X / dpiX;
else
colorView.Left = point.X;

if (point.Y >= desktopWorkingArea.Height - 40)
colorView.Top = point.Y - 40;
colorView.Left = point.X / dpiX;
if (point.Y / dpiY >= desktopWorkingArea.Height - 40)
colorView.Top = point.Y / dpiY - 40;
else
colorView.Top = point.Y;
colorView.Top = point.Y / dpiY;

colorView.MouseColor = new SolidColorBrush(color);
hex = color.ToString();
if (hex.Length > 7)
hex = hex.Remove(1, 2);
colorView.MouseColorText = hex;
}

private void MouseHook_MouseDown(object sender, MouseEventArgs e)
{
Clipboard.SetText(colorView.MouseColor.ToString());
ShowBalloon("提示", $"已复制到剪切板 {colorView.MouseColor.ToString()} SoftwareHelper");
Clipboard.SetText(hex);
ShowBalloon("提示", $"已复制到剪切板 {hex} SoftwareHelper");
if (_timer.IsEnabled)
{
_timer.Stop();
Expand Down
22 changes: 8 additions & 14 deletions SoftwareHelper/Views/EmbedDeasktopView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ private void OnHookKeyDown(object sender, HookEventArgs e)
SetKeyDown(e.Key);
if (IsKeyDown(Key.PrintScreen))
{
var screenCut = new ScreenCut();
screenCut.ShowDialog();
var screenCut = new ScreenCut() {Topmost = true};
screenCut.Activate();
screenCut.Closing += delegate
{
SetKeyUp(Key.PrintScreen);
};
screenCut.ShowDialog();
}
else
{
Expand Down Expand Up @@ -112,16 +117,6 @@ private void IMEUS()
Win32Api.LoadKeyboardLayout("0000409", Win32Api.KLF_ACTIVATE));
}

private void EmbedDeasktopView_KeyUp(object sender, KeyEventArgs e)
{
Thread.Sleep(300);
KeyDownPanel.Visibility = Visibility.Collapsed;
}

private void EmbedDeasktopView_KeyDown(object sender, KeyEventArgs e)
{
}

private void EmbedDeasktopView_Loaded(object sender, RoutedEventArgs e)
{
#region 注释
Expand All @@ -133,10 +128,9 @@ private void EmbedDeasktopView_Loaded(object sender, RoutedEventArgs e)

#endregion
}

private void EmbedDeasktopView_Closing(object sender, CancelEventArgs e)
{
Win32Api.UnRegisterDesktop();
Win32Api.UnRegisterDesktop(true);
}

protected override void OnSourceInitialized(EventArgs e)
Expand Down
32 changes: 20 additions & 12 deletions SoftwareHelper/Views/WindowColor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@
xmlns:shell="https://github.com/WPFDevelopersOrg.WPFDevelopers.Minimal"
mc:Ignorable="d"
Title="WindowColor"
UseLayoutRounding="True" ResizeMode="NoResize" Height="80" Width="80"
UseLayoutRounding="True" ResizeMode="NoResize" Height="60"
SizeToContent="Width"
SnapsToDevicePixels="True" WindowStyle="None" Background="Transparent"
Topmost="True" ShowInTaskbar="False" >
<shell:WindowChrome.WindowChrome>
<shell:WindowChrome GlassFrameThickness="-1" CaptionHeight="0"/>
</shell:WindowChrome.WindowChrome>
<Grid Margin="10">
<!--<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Grid.RowSpan="2" Background="White" Effect="{StaticResource NormalShadowDepth}"/>
<Rectangle Grid.Row="0"
<Border Grid.ColumnSpan="2" Margin="10"
CornerRadius="4"
Background="{StaticResource BaseSolidColorBrush}"
Effect="{StaticResource NormalShadowDepth}">
<Grid Height="30">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0" RadiusX="4" RadiusY="4" Margin="4"
Fill="{Binding MouseColor,RelativeSource={RelativeSource AncestorType={x:Type local:WindowColor}}}"/>
<TextBlock Grid.Row="1" Text="{Binding MouseColor,RelativeSource={RelativeSource AncestorType={x:Type local:WindowColor}}}"/>-->
<Border Background="White" Effect="{StaticResource NormalShadowDepth}" CornerRadius="40"/>
<TextBlock Grid.Column="1" VerticalAlignment="Center"
FontWeight="Black" Margin="4,0,6,0"
FontSize="14"
Text="{Binding MouseColorText,RelativeSource={RelativeSource AncestorType={x:Type local:WindowColor}}}"/>
<!--<Border Background="White" Effect="{StaticResource NormalShadowDepth}" CornerRadius="40"/>
<Ellipse Fill="{Binding MouseColor,RelativeSource={RelativeSource AncestorType={x:Type local:WindowColor}}}"
Stroke="Transparent" StrokeThickness="8"/>
</Grid>
Stroke="Transparent" StrokeThickness="8"/>-->
</Grid>
</Border>
</Window>
12 changes: 11 additions & 1 deletion SoftwareHelper/Views/WindowColor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ public Brush MouseColor
set { SetValue(MouseColorProperty, value); }
}

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



public string MouseColorText
{
get { return (string)GetValue(MouseColorTextProperty); }
set { SetValue(MouseColorTextProperty, value); }
}

public static readonly DependencyProperty MouseColorTextProperty =
DependencyProperty.Register("MouseColorText", typeof(string), typeof(WindowColor), new PropertyMetadata(null));


public WindowColor()
{
InitializeComponent();
Expand Down
7 changes: 4 additions & 3 deletions SoftwareHelper/app.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->

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

</application>
</compatibility>
Expand All @@ -48,12 +48,13 @@
自动缩放。Windows Presentation Foundation (WPF)应用程序自动感知 DPI,无需
选择加入。选择加入此设置的 Windows 窗体应用程序(目标设定为 .NET Framework 4.6 )还应
在其 app.config 中将 "EnableWindowsFormsHighDpiAutoResizing" 设置设置为 "true"。-->

<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitor</dpiAwareness>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
</windowsSettings>
</application>


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

0 comments on commit 79b730d

Please sign in to comment.