Formborderstyle 속성이 없을 때 Windows 양식을 이동하는 방법은 무엇입니까?

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

  •  12-09-2019
  •  | 
  •  

문제

C#사용.
나는 a를 움직이려고 노력하고있다 Form 타이틀 바없이.
그것에 관한 기사를 찾았습니다. http://www.codeproject.com/kb/cs/csharpmovewindow.aspx

설정하지 않는 한 작동합니다 FormBorderStyle ~처럼 None.

이 속성 세트와 함께 작동하게하는 방법이 있습니까? None?

도움이 되었습니까?

해결책

나는이 질문이 1 년이 넘었다는 것을 알고 있지만 과거에 어떻게했는지 기억하려고 노력하고있었습니다. 따라서 다른 사람의 참조의 경우, 가장 빠르고 덜 복잡한 방법은 위의 링크가 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);
}

이렇게하면 클라이언트 영역 내에서 클릭하고 드래그하여 모든 양식을 이동할 수 있습니다.

다른 팁

여기에서 내가 찾은 가장 좋은 방법입니다. WNDPROC를 사용하는 ".NET Way", Wihout입니다. 드래그 가능하고 싶은 표면의 Mousedown, MouseMove 및 Mouseup 이벤트를 처리하면됩니다.

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

먼저 네임 스페이스를 사용하여 Interop 서비스를 사용해야합니다.

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

이 링크를 참조하십시오 드래그 가능한 형태

크레딧 라훌 라 자트-싱

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

    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