كيفية نقل نموذج Windows عند تعيين خاصية Formborderstyle إلى بلا؟

StackOverflow https://stackoverflow.com/questions/1241812

  •  12-09-2019
  •  | 
  •  

سؤال

باستخدام C #.
أحاول نقل Form بدون شريط العنوان الخاص به.
لقد وجدت مقال حول هذا الموضوع: http://www.codeproject.com/kb/cs/csharpmovewindow.aspx.

إنه يعمل طالما أنا لا أعدد FormBorderStyle كما None.

هل هناك طريقة لجعلها تعمل مع هذه الخاصية مجموعة None?

هل كانت مفيدة؟

المحلول

أعرف أن هذا السؤال قد تجاوز عمره عام، لكنني كنت أبحث عن محاولة تذكر كيف قمت بذلك في الماضي. لذلك لمرجع أي شخص آخر، الطريقة الأسرع والأقل تعقيدا، ثم الرابط أعلاه هو تجاوز دالة WNDProC.

/*
Constants in Windows API
0x84 = WM_NCHITTEST - Mouse Capture Test
0x1 = HTCLIENT - Application Client Area
0x2 = HTCAPTION - Application Title Bar

This function intercepts all the commands sent to the application. 
It checks to see of the message is a mouse click in the application. 
It passes the action to the base action by default. It reassigns 
the action to the title bar if it occured in the client area
to allow the drag and move behavior.
*/

protected override void WndProc(ref Message m)
{
    switch(m.Msg)
    {
        case 0x84:
            base.WndProc(ref m);
            if ((int)m.Result == 0x1)
                m.Result = (IntPtr)0x2;
            return;
    }

    base.WndProc(ref m);
}

سيتيح ذلك نقل أي نموذج من خلال النقر والسحب داخل منطقة العميل.

نصائح أخرى

هذه هي أفضل طريقة وجدتها. إنها "طريقة .NET"، Wihout باستخدام WNDProc. عليك فقط التعامل مع أحداث Mousedown و Mousemove و MouseP من الأسطح التي تريد أن تكون مهذقا.

private bool dragging = false;
private Point dragCursorPoint;
private Point dragFormPoint;

private void FormMain_MouseDown(object sender, MouseEventArgs e)
{
    dragging = true;
    dragCursorPoint = Cursor.Position;
    dragFormPoint = this.Location;
}

private void FormMain_MouseMove(object sender, MouseEventArgs e)
{
    if (dragging)
    {
        Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
        this.Location = Point.Add(dragFormPoint, new Size(dif));
    }
}

private void FormMain_MouseUp(object sender, MouseEventArgs e)
{
    dragging = false;
}

كان لدي نفس السؤال منذ فترة طويلة وأثناء البحث عن الإجابة وجدت الكود أدناه (لا تتذكر الموقع) وهنا ما أقوم به:

    Point mousedownpoint = Point.Empty;

    private void Form_MouseDown(object sender, MouseEventArgs e)
    {
        mousedownpoint = new Point(e.X, e.Y);
    }

    private void Form_MouseMove(object sender, MouseEventArgs e)
    {

        if (mousedownpoint.IsEmpty)
            return;
        Form f = sender as Form;
        f.Location = new Point(f.Location.X + (e.X - mousedownpoint.X), f.Location.Y + (e.Y - mousedownpoint.Y));

    }

    private void Form_MouseUp(object sender, MouseEventArgs e)
    {
        mousedownpoint = Point.Empty;
    }

أولا، سيتعين علينا استخدام خدمات الترابط عبر استخدام مساحة الاسم

using System.Runtime.InteropServices;

سيكون الشيء التالي هو تحديد الرسائل التي ستعتني بنقل النموذج. سيكون لدينا هذه مثل متغيرات الأعضاء في الفصل

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

وأخيرا سوف نكتب التعليمات البرمجية لإرسال الرسالة كلما كان المستخدم يضغط على زر الماوس. سيتم تغيير موضع النموذج وفقا لحركة الماوس إذا احتفظ المستخدم بالضغط على زر الماوس.

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    ReleaseCapture();
    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 
}

إحالة هذا الرابط نموذج السحب

الشكر ل راحول راجات سينغ

نقطة mousedownpoint = point.empty؛

    private void Form_MouseDown(object sender, MouseEventArgs e)
    {
        mousedownpoint = new Point(e.X, e.Y);
    }

    private void Form_MouseMove(object sender, MouseEventArgs e)
    {

        if (mousedownpoint.IsEmpty)
            return;
        Form f = sender as Form;
        f.Location = new Point(f.Location.X + (e.X - mousedownpoint.X), f.Location.Y + (e.Y - mousedownpoint.Y));

    }

    private void Form_MouseUp(object sender, MouseEventArgs e)
    {
        mousedownpoint = Point.Empty;
    }

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        Form_MouseDown(this, e);
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        Form_MouseUp(this, e);
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        Form_MouseMove(this, e);
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top