WPF中不規(guī)則窗體與WindowsFormsHost控件兼容問題的解決方法
本文實例講述了WPF中不規(guī)則窗體與WindowsFormsHost控件兼容問題的解決方法。分享給大家供大家參考。具體方法如下:
這里首先說明一下,有關(guān)WPF中不規(guī)則窗體與WindowsFormsHost控件不兼容的問題,網(wǎng)上給出的很多解決方案不能滿足所有的情況,是有特定條件的,比如有一篇《WPF中不規(guī)則窗體與WebBrowser控件的兼容問題解決辦法》(感興趣的朋友可以自己百度一下這篇文章)。該網(wǎng)友的解決辦法也是別出心裁的,為什么這樣說呢,他的webBrowser控件的是單獨放在一個Form中,讓這個Form與WPF中的一個Bord控件進行關(guān)聯(lián),進行同步移動,但是在移動的時候會出現(xiàn)閃爍,并且還會出現(xiàn)運動的白點,用戶體驗肯定不好。
OK,繞了一大圈,還是言歸正傳吧,為什么會出現(xiàn)該問題呢,是什么原因?qū)е略赪PF中設(shè)置了透明窗體之后,嵌入WinForm中的控件會顯示不了呢。一開始我以為是沒有正常加載,還要我有UISPY,通過這個軟件,我捕獲了一下當前運行的程序,發(fā)現(xiàn)我在WPF中內(nèi)嵌的WinForm控件已經(jīng)加載上了,只是沒有看到而已罷了。很郁悶啊。
悲催的程序,頭疼啊,是什么原因?qū)е碌哪?,網(wǎng)上查資料,找到了http://msdn.microsoft.com/zh-cn/library/aa970688.aspx ,讓我了解了不少知識。由于項目要用到透明窗體還要制作圓角窗體,說以本來打算不改變WPF中對window的設(shè)置,即不改變WindowStyle=“None” 和AllowTransparent = “True”這些設(shè)置,想在在WindowsFormsHost上做一些設(shè)置,發(fā)現(xiàn)這條路走不通。浪費了不少時間。
此路不通只有換思路了,那么把AllowTransparent =“false” ,然后就可以顯示,呵呵……當然還要修改啊,WPF的窗體多難看啊,外邊有一個邊框。怎么搞去啊,怎樣辦,這也是一個問題啊。想用WPF的特性,悲劇了,好像沒有相關(guān)的方法啊。
OK,路還是有的,程序員就是來解決辦法的,怎么辦,只能調(diào)用Windows的API,把最外層的那層邊框被去掉了。那么需要什么呢,思路是有了,對吧,那么就行動吧,google 和百度一通,發(fā)現(xiàn)還真有不少例子,c++的例子最全面,可以參考一下。那么就整理了一下需要這些函數(shù):
SetWindowLong 設(shè)置值window的樣式
GetWindowLong 獲取window的樣式
SetWindowRgn 設(shè)置window的工作區(qū)
CreateRoundRectRgn 創(chuàng)建帶有圓角的區(qū)域
SetLayeredWindowAttributes 設(shè)置層次窗體,進行透明度的設(shè)置
直接百度,百科有對他們的詳細解釋,不過給出的是C++的解釋,那么需要你對C++的東西進行轉(zhuǎn)化成C#的東西,有關(guān)C#如何調(diào)用C++的DLL文件,百度和google中有你想要的答案,我就補多少了,不過要注意類型的轉(zhuǎn)化和字符
集的轉(zhuǎn)化。
下面我把我轉(zhuǎn)化好的函數(shù)給大家貼上來,以饗讀者。
{
/// <summary>
/// 帶有外邊框和標題的windows的樣式
/// </summary>
public const long WS_CAPTION = 0X00C0000L;
// public const long WS_BORDER = 0X0080000L;
/// <summary>
/// window 擴展樣式 分層顯示
/// </summary>
public const long WS_EX_LAYERED = 0x00080000L;
/// <summary>
/// 帶有alpha的樣式
/// </summary>
public const long LWA_ALPHA = 0x00000002L;
/// <summary>
/// 顏色設(shè)置
/// </summary>
public const long LWA_COLORKEY = 0x00000001L;
/// <summary>
/// window的基本樣式
/// </summary>
public const int GWL_STYLE = -16;
/// <summary>
/// window的擴展樣式
/// </summary>
public const int GWL_EXSTYLE = -20;
/// <summary>
/// 設(shè)置窗體的樣式
/// </summary>
/// <param name="handle">操作窗體的句柄</param>
/// <param name="oldStyle">進行設(shè)置窗體的樣式類型.</param>
/// <param name="newStyle">新樣式</param>
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern void SetWindowLong(IntPtr handle, int oldStyle, long newStyle);
/// <summary>
/// 獲取窗體指定的樣式.
/// </summary>
/// <param name="handle">操作窗體的句柄</param>
/// <param name="style">要進行返回的樣式</param>
/// <returns>當前window的樣式</returns>
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern long GetWindowLong(IntPtr handle, int style);
/// <summary>
/// 設(shè)置窗體的工作區(qū)域.
/// </summary>
/// <param name="handle">操作窗體的句柄.</param>
/// <param name="handleRegion">操作窗體區(qū)域的句柄.</param>
/// <param name="regraw">if set to <c>true</c> [regraw].</param>
/// <returns>返回值</returns>
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern int SetWindowRgn(IntPtr handle, IntPtr handleRegion, bool regraw);
/// <summary>
/// 創(chuàng)建帶有圓角的區(qū)域.
/// </summary>
/// <param name="x1">左上角坐標的X值.</param>
/// <param name="y1">左上角坐標的Y值.</param>
/// <param name="x2">右下角坐標的X值.</param>
/// <param name="y2">右下角坐標的Y值.</param>
/// <param name="width">圓角橢圓的width.</param>
/// <param name="height">圓角橢圓的height.</param>
/// <returns>hRgn的句柄</returns>
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int width, int height);
/// <summary>
/// Sets the layered window attributes.
/// </summary>
/// <param name="handle">要進行操作的窗口句柄</param>
/// <param name="colorKey">RGB的值</param>
/// <param name="alpha">Alpha的值,透明度</param>
/// <param name="flags">附帶參數(shù)</param>
/// <returns>true or false</returns>
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern bool SetLayeredWindowAttributes(IntPtr handle, ulong colorKey, byte alpha, long flags);
}
下面的問題就是如何進行操作了,首先在進行嵌入WinForm控件的WPF窗體中添加一個Load事件,在事件中添加如下代碼:
IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
// 獲得窗體的 樣式
long oldstyle = NativeMethods.GetWindowLong(hwnd, NativeMethods.GWL_STYLE);
// 更改窗體的樣式為無邊框窗體
NativeMethods.SetWindowLong(hwnd, NativeMethods.GWL_STYLE, oldstyle & ~NativeMethods.WS_CAPTION);
// SetWindowLong(hwnd, GWL_EXSTYLE, oldstyle & ~WS_EX_LAYERED);
// 1 | 2 << 8 | 3 << 16 r=1,g=2,b=3 詳見winuse.h文件
// 設(shè)置窗體為透明窗體
NativeMethods.SetLayeredWindowAttributes(hwnd, 1 | 2 << 8 | 3 << 16, 0, NativeMethods.LWA_ALPHA);
// 創(chuàng)建圓角窗體 12 這個值可以根據(jù)自身項目進行設(shè)置
NativeMethods.SetWindowRgn(hwnd, NativeMethods.CreateRoundRectRgn(0, 0, Convert.ToInt32(this.ActualWidth), Convert.ToInt32(this.ActualHeight), 12, 12), true);
還有就是窗體大小改變之后還要重畫圓角窗體,否則出現(xiàn)很不理想的顯示效果,添加如下事件代碼,解決窗體大小改變的時候,重畫窗體的圓角區(qū)域:
/// Handles the SizeChanged event of the DesktopShell control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.SizeChangedEventArgs"/> instance containing the event data.</param>
private void DesktopShell_SizeChanged(object sender, SizeChangedEventArgs e)
{
// 獲取窗體句柄
IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
// 創(chuàng)建圓角窗體
NativeMethods.SetWindowRgn(hwnd,NativeMethods.CreateRoundRectRgn(0, 0, Convert.ToInt32(this.ActualWidth), Convert.ToInt32(this.ActualHeight), 12, 12), true);
}
至此問題就全部解決了,希望本文所述對大家的WPF程序設(shè)計有所幫助。
相關(guān)文章
C#實現(xiàn)判斷文件夾存在與否并創(chuàng)建文件夾的方法
這篇文章主要介紹了C#實現(xiàn)判斷文件夾存在與否并創(chuàng)建文件夾的方法,涉及C#針對文件及目錄的判斷與創(chuàng)建操作相關(guān)技巧,需要的朋友可以參考下2017-02-02