您的位置:首页 > 其它

WPF/WinForm 关于窗体大小变化的消息机制处理

2013-03-27 21:39 381 查看
因为我去掉了WPF窗体的默认边框,所以要实现窗体随鼠标拖拉而进行缩放和最大化处理时不遮挡任务栏,那么就得动态调用win32的api,去利用消息机制处理这类操作。

下面这段代码是网上找的,是处理窗体最大化时不遮挡任务栏,我这里主要对代码进行注释分析,方便小白理解(话说我也是小白)

另:[StructLayout(LayoutKind.Sequential)]这个特性,在控件开发或者定义API函数的参数时常用

private const int WM_SYSCOMMAND = 0x112;

private System.Windows.Interop.HwndSource _HwndSource;

public static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case 0x0024:/* WM_GETMINMAXINFO  这个是Windows消息代码,是指窗口大小或位置的改变,每个不同的消息都会有对应的编码 */
//然后对应的操作,在wpf中你得借助HwndSource类来捕获消息,winform中你只要重载该函数就可以
WmGetMinMaxInfo(hwnd, lParam);
handled = true;
break;
default: break;
}
return (System.IntPtr)0;
}

private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam)
{

MinMaxInfo mmi = (MinMaxInfo)Marshal.PtrToStructure(lParam, typeof(MinMaxInfo));

// 最大化的大小和位置调整,以适应在正确的显示器的工作区中
int MONITOR_DEFAULTTONEAREST = 0x00000002;
System.IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);

if (monitor != System.IntPtr.Zero)
{

MONITORINFO monitorInfo = new MONITORINFO();
GetMonitorInfo(monitor, monitorInfo);
RECT rcWorkArea = monitorInfo.rcWork;
RECT rcMonitorArea = monitorInfo.rcMonitor;
mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
mmi.ptMinTrackSize.x = 400;//宽度
mmi.ptMinTrackSize.y = 300;//高度
}

Marshal.StructureToPtr(mmi, lParam, true);
}

/// <summary>
/// POINTAPI:POINT 这是告诉编译器这个结构使用顺序布局,在内存里先后排序
/// 定义点的x、y轴点,作为MinMaxInfo的属性类型
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
/// <summary>
/// x坐标点
/// </summary>
public int x;
/// <summary>
/// y坐标点
/// </summary>
public int y;

/// <summary>
/// 由x、y构造的点
/// </summary>
public POINT(int x, int y)
{
this.x = x;
this.y = y;
}
}

/// <summary>
/// 最大化的默认位置和尺寸,以及默认的最小和最大跟踪尺寸的一个MINMAXINFO结构的指针
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct MinMaxInfo
{
public POINT ptReserved;
/// <summary>
/// 主显示器的最大高(宽)度
/// </summary>
public POINT ptMaxSize;
/// <summary>
/// 显示的最大高宽度
/// </summary>
public POINT ptMaxPosition;
/// <summary>
/// 最小的高宽度,可用于限定的最小区域
/// </summary>
public POINT ptMinTrackSize;
/// <summary>
/// 最大的高宽度
/// </summary>
public POINT ptMaxTrackSize;
};

[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
public static readonly RECT Empty = new RECT();

/// <summary> Win32窗口宽度 </summary>
public int Width
{
get { return Math.Abs(right - left); }  // Abs needed for BIDI OS
}
/// <summary> Win32窗口高度 </summary>
public int Height
{
get { return bottom - top; }
}
/// <summary> Win32窗口 </summary>
public RECT(int left, int top, int right, int bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
/// <summary> Win32窗口 </summary>
public RECT(RECT rcSrc)
{
this.left = rcSrc.left;
this.top = rcSrc.top;
this.right = rcSrc.right;
this.bottom = rcSrc.bottom;
}
}

/// <summary>
/// 包含显示器信息的类型
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MONITORINFO
{
/// <summary>
/// 必须指定MONITORINFO结构中的cbSize,大小为 sizeof(MONITORINFO) 或者sizeof(MONITORINFOEX)
/// </summary>
public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));

public RECT rcMonitor = new RECT();//主显示屏

public RECT rcWork = new RECT();//工作显示区

public int dwFlags = 0;
}

/// <summary>
/// GetMonitorInfo函数返回一个显示器的信息
/// </summary>
/// <param name="hMonitor">显示器的句柄</param>
/// <param name="lpmi">显示器信息(输出参数)</param>
/// <returns></returns>
[DllImport("user32")]
internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);

/// <summary>
/// 获取到屏幕上具有最大交集区域的边界矩形的一个指定的窗口句柄。(能处理多显示器的问题)
/// </summary>
/// <param name="handle">窗口句柄</param>
/// <param name="flags">可能为null、最接近的窗口、主显示屏</param>
/// <returns></returns>
[DllImport("User32")]
internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);


这样,只要在你要处理的窗口调用WndProc函数就可以了

this.SourceInitialized += delegate(object sender, EventArgs e)
{
this._HwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
this._HwndSource.AddHook(Win32.WndProc);
};


另外,如果只是在主显示器最大化处理有一句更简洁的代码实现:

this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);

最后想说几点,因为身边有人会疑虑.NET平台下这么多技术,不想在一棵树上吊死,想做个综合性的十字人才。我汗~我现在是在给公司做播放器的客户端界面,使用的wpf,内核使用的是vlc,目前也是在开发过程中,其实我对wpf根本不懂,但是技术都这样,你不可能所有的都会,但是语法基本相通,很多知识点我都是网上copy的,自己整整改改“变成”自己想要的,看懂别人的代码。有时候会想这种“偷”到底对自己有没有好处呢。不用质疑太多,新技术层出不穷,你跟到死都学不完,把自己擅长的弄精通,别的方面在你用的时候边做边学就可以了。

好了,希望我的文章对各位看官有用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: