C# WinForms 无边框窗体拖动:实现包含多个 Fill TableLayoutPanel 的窗体移动
要实现无边框窗体中包含多个填充整个窗体的 'TableLayoutPanel' 的拖动功能,你可以利用鼠标事件来跟踪鼠标的移动,并相应地更新窗体的位置。以下是一个示例代码:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApp
{
public partial class MainForm : Form
{
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HT_CAPTION = 0x2;
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
private static extern bool ReleaseCapture();
public MainForm()
{
InitializeComponent();
// 关联多个填充整个窗体的 TableLayoutPanel 的 MouseDown 事件
tableLayout1.MouseDown += new MouseEventHandler(Form_MouseDown);
tableLayout2.MouseDown += new MouseEventHandler(Form_MouseDown);
tableLayout3.MouseDown += new MouseEventHandler(Form_MouseDown);
// ... 其他 TableLayoutPanel
}
private void Form_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
}
}
在上述示例代码中,我们将拖动操作关联到了多个填充整个窗体的 'TableLayoutPanel' 的 'MouseDown' 事件。当鼠标按下左键时,通过调用 'ReleaseCapture()' 方法释放对鼠标的捕获,然后使用 'SendMessage()' 方法发送一个消息给窗体,通知它执行拖动操作。
请确保将示例代码中的 'tableLayout1'、'tableLayout2'、'tableLayout3' 替换为你实际使用的 'TableLayoutPanel' 控件的名称,并在窗体设计器中将每个 'TableLayoutPanel' 的 'MouseDown' 事件与 'Form_MouseDown' 事件处理程序关联起来。
通过以上代码,你可以实现无边框窗体中包含多个填充整个窗体的 'TableLayoutPanel' 的拖动功能。当鼠标按下并拖动其中任何一个 'TableLayoutPanel' 时,整个窗体都会跟随鼠标移动。
原文地址: https://www.cveoy.top/t/topic/ocF 著作权归作者所有。请勿转载和采集!