C# 通行证;这";参数转换为另一个类

C# 通行证;这";参数转换为另一个类,c#,winforms,C#,Winforms,假设,我的表单没有边框,我将使一个图像成为一个自定义标题栏 我想创建一个调用TitleBar的类,有一个方法ApplyTitleBar(控件c); 在project中的任何窗体中,只要对窗体上的任何控件调用ApllyTitleBar(),当鼠标按下并移动时,窗体也会移动 类标题栏中的代码: public class TitleBar { private bool drag = false; // determine if we should be moving the form

假设,我的表单没有边框,我将使一个图像成为一个自定义标题栏

我想创建一个调用TitleBar的类,有一个方法ApplyTitleBar(控件c); 在project中的任何窗体中,只要对窗体上的任何控件调用ApllyTitleBar(),当鼠标按下并移动时,窗体也会移动

类标题栏中的代码:

public class TitleBar
{
    private bool drag = false; // determine if we should be moving the form
    private Point startPoint = new Point(0, 0);

    public void ApplyTitleBar(Control c)
    {
        c.MouseUp += new MouseEventHandler(panelTitleBar_MouseUp);
        c.MouseMove += new MouseEventHandler(panelTitleBar_MouseMove);
        c.MouseDown += new MouseEventHandler(panelTitleBar_MouseDown);

    }

    void panelTitleBar_MouseUp(object sender, MouseEventArgs e)
    {
        this.drag = false;
    }
    void panelTitleBar_MouseDown(object sender, MouseEventArgs e)
    {
        this.startPoint = e.Location;
        this.drag = true;
    }
    void panelTitleBar_MouseMove(object sender, MouseEventArgs e)
    {
        if (this.drag)
        { // if we should be dragging it, we need to figure out some movement
            Point p1 = new Point(e.X, e.Y);
            Point p2 = this.PointToScreen(p1);
            Point p3 = new Point(p2.X - this.startPoint.X,
                                 p2.Y - this.startPoint.Y);
            this.Location = p3;
        }
    }


}
假设在另一种形式中,我有标签lblTitleBar,并希望它将是一个“标题栏”

我知道TitleBar类中的“this”参数不能有方法Location()和PointToScreen(),因为“this”是TitleBar的实例,而不是形式。 有没有其他方法来传递一个类到这个,或这样做的方法

tb.Apply(lblTitleBar,this);

我想你想要的是对表格的引用:

public class TitleBar
{
    private bool drag = false; // determine if we should be moving the form
    private Point startPoint = new Point(0, 0);
    private Form form;

    public void ApplyTitleBar(Control c, Form f)
    {
        c.MouseUp += new MouseEventHandler(panelTitleBar_MouseUp);
        c.MouseMove += new MouseEventHandler(panelTitleBar_MouseMove);
        c.MouseDown += new MouseEventHandler(panelTitleBar_MouseDown);
        this.form = f;

    }
    ....
之后,您可以执行以下操作:

void panelTitleBar_MouseMove(object sender, MouseEventArgs e)
{
    if (this.drag)
    { // if we should be dragging it, we need to figure out some movement
        Point p1 = new Point(e.X, e.Y);
        Point p2 = form.PointToScreen(p1);
        Point p3 = new Point(p2.X - form.startPoint.X,
                             p2.Y - form.startPoint.Y);
        form.Location = p3;
    }
}
并且,假设您从表单中实例化了所有这些,您确实会这样称呼它:

tb.ApplyTitleBar(lblTitleBar,this);

您可以尝试检索包含目标控件的表单

void panelTitleBar_MouseMove(object sender, MouseEventArgs e)
{
    Control control = sender as Control;
    if(null != control)
    {
        Form form = control.FindForm();
        if(null != form)
        {
            if (this.drag)
            { // if we should be dragging it, we need to figure out some movement
                Point p1 = new Point(e.X, e.Y);
                Point p2 = form.PointToScreen(p1);
                Point p3 = new Point(p2.X - this.startPoint.X,
                                     p2.Y - this.startPoint.Y);
                form.Location = p3;
            }
        }
    }
}

我发现你的第一段很难理解——我已经尽了最大的努力编辑了,但是最后的部分——“像形状一样,我的意思是鼠标向下移动,形状会移动”——对我来说太难理解了。您可以尝试重新编写它吗?您的
标题栏
类没有
Apply
方法!?谢谢假设,myForm没有边框,我将使一个图像成为移动表单的标题栏。我想创建一个调用TitleBar的类,有一个方法ApplyTitleBar(控件c);在project中的任何窗体中,只要对窗体上的任何控件调用ApllyTitleBar(),当鼠标按下并移动时,窗体也会移动。
void panelTitleBar_MouseMove(object sender, MouseEventArgs e)
{
    Control control = sender as Control;
    if(null != control)
    {
        Form form = control.FindForm();
        if(null != form)
        {
            if (this.drag)
            { // if we should be dragging it, we need to figure out some movement
                Point p1 = new Point(e.X, e.Y);
                Point p2 = form.PointToScreen(p1);
                Point p3 = new Point(p2.X - this.startPoint.X,
                                     p2.Y - this.startPoint.Y);
                form.Location = p3;
            }
        }
    }
}