Вопрос

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);
     }

}

And this is the code of my form:

 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.

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

The MyPanel Class :

MouseMove += MyPanel_MouseMove; // added in class constructer

the BarraSms class (Form)

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);

        }

    }

}
Это было полезно?

Решение

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.

try this:

using System.Runtime.InteropServices;

and in your Form class

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);
   }
}

no need for a mouseDown event for this

Другие советы

Your private methods in MyPanel cannot be called by the framework. You need to declare them as follows:

protected override void OnMouseDown(MouseEventArgs e)
{
    var parent = this.Parent as BarraSms;
    parent.mouseDown(e);
}

protected override void OnMouseMove(MouseEventArgs e)
{
    var parent = this.Parent as BarraSms;
    parent.mouseMove(e);
}

Try this:

public void mouseMove(MouseEventArgs e) {

    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        Point currentPos = Location;
        currentPos.Offset(e.X + mousePoint.X, e.Y + mousePoint.Y);
        this.Location = currentPos;
    }
    //Or simply use Location.Offset(e.X + mousePoint.X, e.Y + mousePoint.Y);
}

You have to use Location (the current location of the form), not the Control.MousePosition which is the location of mouse on screen.

UPDATE: Looks like you don't even know how to register event handlers, try modifying your panel class like this:

public class MyPanel : System.Windows.Forms.Panel{
  //(...)
 protected override void OnMouseDown(MouseEventArgs e)
 {
     base.OnMouseDown(e);
     BarraSms.getInstance().mouseDown(e);
 }

 protected override void OnMouseMove(MouseEventArgs e)
 {
     base.OnMouseMove(e);
     BarraSms.getInstance().mouseMove(e);
 }

}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top