zoukankan      html  css  js  c++  java
  • 调用API 实现 窗体 拖动

    自定义 做一个窗体 实现 鼠标拖动

    View Code
    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);
  • 相关阅读:
    Java基础复习(1)
    mybatis中Oracle分页语句的写法
    Spring Security 入门原理及实战
    Java中的基本类型和包装类型区别
    Apache Shiro简单介绍
    linux常用命令介绍
    Spring Cloud的简单介绍
    服务端跳转和客户端跳转
    使用ajax向后台发送请求跳转页面无效的原因
    js css html加载顺序
  • 原文地址:https://www.cnblogs.com/suli/p/2986649.html
Copyright © 2011-2022 走看看