博客
关于我
WinForm实现无边框窗体的拖动
阅读量:529 次
发布时间:2019-03-08

本文共 1215 字,大约阅读时间需要 4 分钟。

如何实现无边框窗体并添加拖动效果

在开发中,当我们将FormBorderStyle属性设置为None时,窗口将失去边框,无法通过拖动标题栏进行移动。这在某些场景下可能会影响用户体验。为了实现类似于传统窗口的拖动效果,我们可以利用PictureBox覆盖Form,并在其基础上定义拖动逻辑。

以下是实现无边框拖动效果的常用技术:

  • 使用PInvoke技术调用系统API我们可以调用Windows API函数来实现拖动效果。以下是相关API:
  • [DllImport("user32.dll")]public static extern bool ReleaseCapture();

    [DllImport("user32.dll")]public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

    internal static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

    1. 定义常量在代码中定义以下常量:
    2. public const int WM_SYSCOMMAND = 0x0112;public const int SC_MOVE = 0xF010;public const int HTCAPTION = 0x0002;

      public const int WM_NCLBUTTONDOWN = 0xA1;public const int HT_CAPTION = 0x2;public const int MOUSEWHEEL = 0x020A;

      1. 实现拖动事件处理在PictureBox的MouseDown和MouseMove事件中添加以下逻辑:
      2. private void pictureBoxBackground_MouseDown(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left){ReleaseCapture();SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);}}

        private void pictureBoxBackground_MouseMove(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left){ReleaseCapture();SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);}}

        通过以上代码,我们可以实现一个可以拖动的无边框窗体。拖动逻辑通过调用系统API实现,确保了良好的用户体验。

    转载地址:http://bngnz.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现highest response ratio next高响应比优先调度算法(附完整源码)
    查看>>
    Objective-C实现hill climbing爬山法用来寻找函数的最大值算法(附完整源码)
    查看>>
    Objective-C实现Hill密码加解密算法(附完整源码)
    查看>>
    Objective-C实现histogram stretch直方图拉伸算法(附完整源码)
    查看>>
    Objective-C实现Hopcroft算法(附完整源码)
    查看>>
    Objective-C实现horizontal projectile motion平抛运动算法(附完整源码)
    查看>>
    Objective-C实现hornerMethod霍纳法算法(附完整源码)
    查看>>
    Objective-C实现Horn–Schunck光流算法(附完整源码)
    查看>>
    Objective-C实现Http Post请求(附完整源码)
    查看>>
    Objective-C实现http下载文件 (附完整源码)
    查看>>
    Objective-C实现Http协议下载文件(附完整源码)
    查看>>
    Objective-C实现huffman哈夫曼编码算法(附完整源码)
    查看>>
    Objective-C实现ID3贪心算法(附完整源码)
    查看>>
    Objective-C实现IIR 滤波器算法(附完整源码)
    查看>>
    Objective-C实现IIR数字滤波器(附完整源码)
    查看>>
    Objective-C实现insertion sort插入排序算法(附完整源码)
    查看>>
    Objective-C实现integer partition整数分区算法(附完整源码)
    查看>>
    Objective-C实现integerPartition整数划分算法(附完整源码)
    查看>>
    Objective-C实现interpolation search插值搜索算法(附完整源码)
    查看>>
    Objective-C实现Interpolation search插值查找算法(附完整源码)
    查看>>