通过面板移动无边框表格 [英] Move form without border through a panel

查看:111
本文介绍了通过面板移动无边框表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决问题的方法,但是现在我无法获得想要完成的任何成功代码.因此,我有一个无边框的表单,其中填充了2个自定义面板,因此用户无法单击框架,因此,我实现了一个代码,当用户单击面板时,这将调用一个函数在我的表单中,该参数通过参数接收鼠标的事件. 这是我的面板的代码(请注意,框架中的两个面板都是同一个类,只是两个不同的实例)

I been searching around for a solution for my problem but for now I wasn't able to get any sucessfull code for what I want to do. So, I have a form without border that is filled with 2 custom panels, so there is no way to the user click on the frame, thinking in that, I implement a code that when user click on a panel, this will call a function on my form that recive by parameter the event of the mouse. This is the code of my Panel (note that both of my panels in the frame are the same class, it's just 2 diferent instances)

 public class MyPanel : System.Windows.Forms.Panel{

                       (...)

     private void MyPanel_MouseDown(object sender, MouseEventArgs e)
     {
         BarraSms.getInstance().mouseDown(e);
     }

     private void MyPanel_MouseMove(object sender, MouseEventArgs e)
     {
         BarraSms.getInstance().mouseMove(e);
     }

}

这是我表格的代码:

 public partial class BarraSms : Form
{
  private Point mousePoint;

             (...)

   public void mouseDown(MouseEventArgs e) {

        mousePoint = new Point(-e.X, -e.Y);

    }

    public void mouseMove(MouseEventArgs e) {

        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Point mousePos = Control.MousePosition;
            mousePos.Offset(mousePoint .X, mousePoint .Y);
            this.Location = mousePos;
        }

    }
}

有什么我想念的吗? 预先感谢您的帮助.

Is there something that I'm missing? Thank you in advance for the help.

工作代码(更新),问题通过以下方式解决:x4rf41

The working code (update), problem solved by: x4rf41

MyPanel类:

MouseMove += MyPanel_MouseMove; // added in class constructer

BarraSms类(窗体)

public partial class BarraSms : Form
{
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;


    [DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture(); 

     public void mouseMove(MouseEventArgs e) {

        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_NCLBUTTONDOWN, new IntPtr(HT_CAPTION), IntPtr.Zero);
            Point loc = this.Location;

            writeCoordToBin(loc.X, loc.Y);

        }

    }

}

推荐答案

使用Windows api函数有更好的解决方案. 使用它的方式,当您快速移动表单并且鼠标从面板中移出时,您将遇到问题.我遇到了完全相同的问题.

There is much better solution for that using the windows api function. The way you use it, you will have a problem when you move the form very fast and the mouse goes out of the panel. I had the exact same problem.

尝试一下:

using System.Runtime.InteropServices;

并在您的Form类中

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

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();        

public void mouseMove(MouseEventArgs e) 
{
   if (e.Button == System.Windows.Forms.MouseButtons.Left)
   {
      ReleaseCapture();
      SendMessage(this.Handle,  WM_NCLBUTTONDOWN, new IntPtr(HT_CAPTION), IntPtr.Zero);
   }
}

为此不需要mouseDown事件

no need for a mouseDown event for this

这篇关于通过面板移动无边框表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆