如何使窗口可拖动(C# Winforms)?

发布于 2024-09-28 10:45:11 字数 169 浏览 2 评论 0原文

我有形式。我已启用表单的透明度,并删除了它的标题栏和边框。在其中我创建了一个自定义用户界面,它具有与窗口相同的功能。基本上,我的想法是创建自定义窗口。

一切都按预期工作,但只有窗口拖动不起作用。我不知道如何启用它。我用谷歌搜索了这个。但我没有找到任何对我有用的信息。

请帮我实现这个窗口拖动。

I have form. I have enabled the transparency on the form and I have removed it's Title Bar and Border. Inside that i have created a Custom UI, Which have the same features like a window. Basically, my idea is to create custom window.

Everything is working as expected but only the windows dragging is not working. I am not sure how to enable it. I googled for this. But i didn't find any useful info for me.

Please help me to implement this window dragging.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-柠檬树下少年和吉他 2024-10-05 10:45:11

我通过捕获 mousedown (在 mouseup 上取消捕获)然后 mousemove 来实现此行为。

只需移动表单坐标(左、上),相当于鼠标移动的量(这些事件具有鼠标移动的量)。

这对我来说效果很好。

I've implemented this behavior by capturing mousedown (uncapture on mouseup), and then mousemove.

Just move the form co-ordinates (left, top), equivalent amounts to the mouse movement (those events have the amount the mouse moved).

This worked fine for me.

烈酒灼喉 2024-10-05 10:45:11
class YourForm : Form
{
     private const int WM_NCHITTEST = 0x84;
     private const int HTCLIENT = 0x1;
     private const int HTCAPTION = 0x2;

     ///
     /// Handling the window messages 
     ///
     protected override void WndProc(ref Message message)
     {
          base.WndProc(ref message);

          if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
               message.Result = (IntPtr)HTCAPTION;
     }
}
class YourForm : Form
{
     private const int WM_NCHITTEST = 0x84;
     private const int HTCLIENT = 0x1;
     private const int HTCAPTION = 0x2;

     ///
     /// Handling the window messages 
     ///
     protected override void WndProc(ref Message message)
     {
          base.WndProc(ref message);

          if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
               message.Result = (IntPtr)HTCAPTION;
     }
}
肥爪爪 2024-10-05 10:45:11

最简单的方法是处理 WM_NCHITTEST 消息并为自定义窗口的部分返回 HTCAPTION,其工作方式类似于普通窗口中的标题栏。 Windows 将完成剩下的工作。

The easiest way is to process WM_NCHITTEST message and return HTCAPTION for the portions of your custom window which work like the title bar does in a normal window. Windows will do the rest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文