2009-08-06 30 views
14

Sử dụng C#.
Tôi đang cố di chuyển một số Form mà không có thanh tiêu đề của nó.
tôi thấy một bài viết về nó trên: http://www.codeproject.com/KB/cs/csharpmovewindow.aspxCách di chuyển Biểu mẫu Windows khi thuộc tính FormBorderStyle của nó được đặt thành Không?

Nó hoạt động miễn là tôi không đặt FormBorderStyle như None.

Có cách nào để làm cho nó hoạt động với thuộc tính này được đặt là None không?

+0

Nó làm việc cho tôi với FormBorderStyle được đặt thành Không. Đây là trên Server 2008, VS2008 với .NET 3.5. Bạn đang sử dụng phiên bản .NET và hệ điều hành nào? –

+0

@Michael McCloskey - Tôi đang sử dụng windows 7 rc, vs2008 với .net 3.5. – Moon

+0

@Michael McClosKey - nevermind! nó hoạt dộng bây giờ. Tôi không biết chuyện gì đã xảy ra. – Moon

Trả lời

36

Tôi biết câu hỏi này đã hơn một tuổi, nhưng tôi đang tìm cách nhớ cách tôi đã thực hiện nó trong quá khứ. Vì vậy, để tham khảo của bất kỳ ai khác, cách nhanh nhất và ít phức tạp hơn thì liên kết ở trên là ghi đè hàm 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); 
} 

Điều này sẽ cho phép bất kỳ biểu mẫu nào được di chuyển bằng cách nhấp và kéo trong vùng khách hàng.

+0

bạn có thể giải thích điều này một chút không? – masfenix

+0

Đã thêm một số chi tiết về những gì nó làm – LizB

+0

tại sao tôi không thể kéo biểu mẫu khi sự kiện được tổ chức trên menuStrip? – CMA

35

Đây là cách tốt nhất tôi đã tìm thấy. Đó là một cách ".NET", sử dụng WndProc. Bạn chỉ cần xử lý các sự kiện MouseDown, MouseMove và MouseUp của các bề mặt bạn muốn có thể kéo được.

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

Đầu tiên chúng ta sẽ phải sử dụng các dịch vụ interop bằng cách sử dụng không gian tên như

using System.Runtime.InteropServices; 

Điều tiếp theo sẽ được xác định thông điệp rằng sẽ chăm sóc di chuyển form. Chúng tôi sẽ có các biến thành viên lớp học

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

và cuối cùng chúng tôi sẽ viết mã để gửi thư bất cứ khi nào người dùng nhấn nút chuột. Biểu mẫu sẽ được định vị lại theo chuyển động của chuột nếu người dùng giữ nút chuột được nhấn.

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

Tham khảo link này Dragable form

Tín dụng cho rahul-rajat-singh

1

Tôi có cùng một câu hỏi một thời gian trước và trong khi tìm kiếm câu trả lời tôi đã tìm thấy đoạn code dưới đây (không nhớ trang web) và Đây là những gì tôi làm:

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; 
    } 
Các vấn đề liên quan