Skip to content

Commit

Permalink
修复缺陷,增加高斯模糊
Browse files Browse the repository at this point in the history
  • Loading branch information
silentmoooon committed Sep 29, 2017
1 parent ff6b42c commit 10549d0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 26 deletions.
19 changes: 4 additions & 15 deletions ClipOne/util/WinAPIHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using static ClipOne.view.MainWindow;

namespace ClipOne.util
{
Expand Down Expand Up @@ -82,25 +83,13 @@ public POINT(int x, int y)
public static extern IntPtr GetDesktopWindow();
[DllImport("user32", EntryPoint = "GetTopWindow")]
public static extern IntPtr GetTopWindow();

/*wCmd指定结果窗口与源窗口的关系,它们建立在下述常数基础上:
GW_CHILD
寻找源窗口的第一个子窗口
GW_HWNDFIRST
为一个源子窗口寻找第一个兄弟(同级)窗口,或寻找第一个顶级窗口
GW_HWNDLAST
为一个源子窗口寻找最后一个兄弟(同级)窗口,或寻找最后一个顶级窗口
GW_HWNDNEXT
为源窗口寻找下一个兄弟窗口
GW_HWNDPREV
为源窗口寻找前一个兄弟窗口
GW_OWNER
寻找窗口的所有者
*/

[DllImport("user32.dll", EntryPoint = "GetWindow")]//获取窗体句柄,hwnd为源窗口句柄
public static extern IntPtr GetWindow(
IntPtr hwnd,
int wCmd
);
[DllImport("user32.dll")]
internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
}
}
87 changes: 76 additions & 11 deletions ClipOne/view/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -53,7 +54,15 @@ public partial class MainWindow : Window
/// </summary>
ChromiumWebBrowser webView;

/// <summary>
/// 隐藏时将left设置为该值
/// </summary>
private int HideLeftValue = -10000;

/// <summary>
/// 透明度转换比例
/// </summary>
private double OpacityRatio = 0.06;

/// <summary>
/// 供浏览器JS回调的接口
Expand Down Expand Up @@ -157,7 +166,7 @@ public MainWindow()

private void Window_Loaded(object sender, RoutedEventArgs e)
{

if (!Directory.Exists(cacheDir))
{
Directory.CreateDirectory(cacheDir);
Expand Down Expand Up @@ -196,7 +205,9 @@ private void Window_Loaded(object sender, RoutedEventArgs e)

//初始化预览窗口
InitPreviewForm();




}


Expand Down Expand Up @@ -334,9 +345,10 @@ private void InitialTray()
opaSet.Click += (sender, e) =>
{
isNotAllowHide = true;
DiyShow();
ShowWindowAndList();

OpacitySet os = new OpacitySet(this, (1 - opacityValue) / 0.02);
OpacitySet os = new OpacitySet(this, (1 - opacityValue) / OpacityRatio);

os.Topmost = true;
os.ShowDialog();
Expand Down Expand Up @@ -573,7 +585,7 @@ private void SaveSettings()
/// <param name="value"></param>
public void ChangeOpacity(double value)
{
opacityValue = 1 - value * 0.02;
opacityValue = 1 - value * OpacityRatio;
this.Opacity = opacityValue;
settingsMap["opacity"] = opacityValue.ToString();
SaveSettings();
Expand Down Expand Up @@ -714,7 +726,7 @@ public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bo
if (hotkeyAtom == wParam.ToInt32())
{

if (this.Opacity != opacityValue) {
if (this.Left == HideLeftValue) {
activeHwnd = WinAPIHelper.GetForegroundWindow();
DiyShow();
}
Expand Down Expand Up @@ -1076,16 +1088,35 @@ private void Window_SourceInitialized(object sender, EventArgs e)
exStyle |= (int)0x00000080;
WinAPIHelper.SetWindowLong(wpfHwnd, -20, exStyle);

//高斯模糊
var accent = new AccentPolicy();
accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND;

var accentStructSize = Marshal.SizeOf(accent);

var accentPtr = Marshal.AllocHGlobal(accentStructSize);
Marshal.StructureToPtr(accent, accentPtr, false);

var data = new WindowCompositionAttributeData();
data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY;
data.SizeOfData = accentStructSize;
data.Data = accentPtr;

WinAPIHelper.SetWindowCompositionAttribute(wpfHwnd, ref data);

Marshal.FreeHGlobal(accentPtr);
DiyHide();


}

//显示窗体,透明度为事先设置的值.
private void DiyShow()
{
this.Topmost = true;
this.Activate();
this.Opacity = opacityValue;
//this.Opacity = opacityValue;




Expand All @@ -1097,13 +1128,15 @@ private void DiyShow()
private void DiyHide()
{
this.Topmost = false;

WinAPIHelper.SetForegroundWindow(activeHwnd);
if (activeHwnd != IntPtr.Zero) {
WinAPIHelper.SetForegroundWindow(activeHwnd);
}
webView?.GetBrowser()?.MainFrame.ExecuteJavaScriptAsync("hide()");
this.Opacity = 0;
// this.Hide();
//this.Opacity = 0;
this.Left = HideLeftValue;
// this.Hide();
}



internal class MenuHandler : IContextMenuHandler
Expand Down Expand Up @@ -1136,10 +1169,42 @@ public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame
return false;
}
}
internal enum AccentState
{
ACCENT_DISABLED = 0,
ACCENT_ENABLE_GRADIENT = 1,
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
ACCENT_ENABLE_BLURBEHIND = 3,
ACCENT_INVALID_STATE = 4
}

[StructLayout(LayoutKind.Sequential)]
internal struct AccentPolicy
{
public AccentState AccentState;
public int AccentFlags;
public int GradientColor;
public int AnimationId;
}

[StructLayout(LayoutKind.Sequential)]
internal struct WindowCompositionAttributeData
{
public WindowCompositionAttribute Attribute;
public IntPtr Data;
public int SizeOfData;
}

internal enum WindowCompositionAttribute
{
// ...
WCA_ACCENT_POLICY = 19
// ...
}

}



}

1 change: 1 addition & 0 deletions ClipOne/view/OpacitySet.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public OpacitySet(MainWindow window,double opacityValue)
InitializeComponent();
this.window = window;
sliderOpa.Value = opacityValue;

}

private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
Expand Down

0 comments on commit 10549d0

Please sign in to comment.