WPF 解决 WindowsFormsHost不兼容问题
2012-02-23 13:10
330 查看
public class NativeMethods { /// <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> /// 颜色设置 /// </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> /// 设置窗体的样式 /// </summary> /// <param name="handle">操作窗体的句柄</param> /// <param name="oldStyle">进行设置窗体的样式类型.</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> /// 设置窗体的工作区域. /// </summary> /// <param name="handle">操作窗体的句柄.</param> /// <param name="handleRegion">操作窗体区域的句柄.</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> /// 创建带有圆角的区域. /// </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); [System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern IntPtr CreateRectRgn(int x1, int y1, int x2, int y2); /// <summary> /// 该CombineRgn函数和存储结合了两个地区在第三地区的结果。结合这两个地区是根据指定的模式。 /// </summary> /// <param name="hDestRgn1">处理到一个新的区域与其他区域相结合的两个定义尺寸。 (此区域之前,必须存在CombineRgn被调用。)</param> /// <param name="hSrcRgn1">句柄两个区域首先要结合起来。</param> /// <param name="hDestRgn2">句柄两个区域的第二个结合起来。</param> /// <param name="hSrcRgn2">指定的模式表明这两个地区如何将被合并。这个参数可以是以下值之一。</param> /// RGN_AND hDestRgn被设置为两个源区域的交集 //RGN_COPY hDestRgn被设置为hSrcRgn1的拷贝 //RGN_DIFF hDestRgn被设置为hSrcRgn1中与hSrcRgn2不相交的部分 //RGN_OR hDestRgn被设置为两个区域的并集 //RGN_XOR hDestRgn被设置为除两个源区域OR之外的部分 /// <returns>句柄</returns> [System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern IntPtr CombineRgn(IntPtr hDestRgn1, IntPtr hSrcRgn1, IntPtr hDestRgn2, int hSrcRgn2); /// <summary> /// Sets the layered window attributes. /// </summary> /// <param name="handle">要进行操作的窗口句柄</param> /// <param name="colorKey">RGB的值</param> /// <param name="alpha">Alpha的值,透明度</param> /// <param name="flags">附带参数</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); }
[/code]
下面的问题就是如何进行操作了,首先在进行嵌入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文件 // 设置窗体为透明窗体 NativeMethods.SetLayeredWindowAttributes(hwnd, 1 | 2 << 8 | 3 << 16, 0, NativeMethods.LWA_ALPHA); // 创建圆角窗体 12 这个值可以根据自身项目进行设置 NativeMethods.SetWindowRgn(hwnd, NativeMethods.CreateRoundRectRgn(0, 0, Convert.ToInt32(this.ActualWidth), Convert.ToInt32(this.ActualHeight), 12, 12), true);
原文: http://blog.csdn.net/wziyx513225244/article/details/6988494
1.1 API常量参考(Constants Reference)
http://www.papazhu.com/post/Constants_Reference.php?page=1&part=7
相关文章推荐
- WPF中不规则窗体与WindowsFormsHost控件兼容问题的解决方法
- WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
- WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
- WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
- WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
- WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
- WPF中不规则窗体与WebBrowser控件的兼容问题解决办法
- 解决wpf中使用WinformsHost显示滚动条的问题
- WPF中不规则窗体与WebBrowser控件的兼容问题解决办法 推荐
- WPF中不规则窗体与WebBrowser控件的兼容问题解决办法
- WPF中去边框窗体与WebBrowser控件的兼容问题解决办法
- 在WPF中使用WindowsFormsHost带来的问题及解决方案
- 由于Spring3.0.5、Hibernante3.2.7、Struts2.1版本不兼容导致的问题及其解决办法。
- 终于解决selection.createRange()兼容IE8问题
- WEB前端开发人员须知的常见浏览器兼容问题及解决技巧
- png透明问题解决方法(png图片ie6不兼容问题)
- 解决 Android模拟器无法上网问题——Host is unresolved (转)
- CSS Hack技术解决多浏览器兼容问题
- 已解决:EditText被底部固定栏挡住,使用AndroidBug5497Workaround出现兼容问题
- 解决django 0.96版本与PostgreSQL 8.3.1不兼容的问题