您的位置:首页 > 其它

wpf无边框的两种实现方式

2017-03-01 11:29 447 查看
有这样一个需求,用wpf做一个无边框窗体,显示网络摄像头图像,这个窗体能够拖动边框来改变大小。

简单实现
http://download.csdn.net/detail/bornonew/9767005
几点说明

1.一般的网络摄像头SDK都需要把控件指针传到sdk中,但wpf整个窗体共用同一指针,所以需要用WindowsFormsHost来引入PictureBox控件

<WindowsFormsHost Grid.RowSpan="4" Grid.ColumnSpan="2" Panel.ZIndex="1">
<wf:PictureBox DoubleClick="RealPlayWnd_DoubleClick"  x:Name="RealPlayWnd" SizeMode="StretchImage" ></wf:PictureBox>
</WindowsFormsHost>
2.如果不用WindowsFormsHost,网上很多帖子都能实现。但设置了AllowsTransparency=“True” 可以实现窗口拖动,但是会发现网络摄像头显示不出来。去掉后能显示,但是会多出一个系统默认的边框。

如果不需要通过边框来改变大小,则只需要设置

ResizeMode="NoResize"
3.参考这篇实现最终的无边框可拖动窗体 不去设置AllowsTransparency和Resizemode
http://blog.csdn.net/detecyang/article/details/7946237
class NoBorderWnd
{
/// <summary>

/// 带有外边框和标题的windows的样式

/// </summary>

public const long WS_CAPTION = 0x00C00000L;

public const long WS_CAPTION_2 = 0X00C0000L;

// public const long WS_BORDER = 0X0080000L;

/// <summary>

/// window 扩展样式 分层显示

/// </summary>

public const long WS_EX_LAYERED = 0x00080000L;

public const long WS_CHILD = 0x40000000L;

/// <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);

/// <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);

//=================================================================================

/// <summary>
/// 设置窗体为无边框风格
/// </summary>
/// <param name="hWnd"></param>
public static void SetWindowNoBorder(IntPtr hWnd)
{
long oldstyle = NoBorderWnd.GetWindowLong(hWnd, NoBorderWnd.GWL_STYLE);

SetWindowLong(hWnd, GWL_STYLE, oldstyle & (~(WS_CAPTION | WS_CAPTION_2)));
//SetWindowLong(hWnd, GWL_EXSTYLE, WS_CHILD);
}
}
在窗体loaded事件中,加入

NoBorderWnd.SetWindowNoBorder(new WindowInteropHelper(this).Handle);
这种方式 我只在x64下能生效
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息