gpt4 book ai didi

c# - 如何在 Winform 应用程序中限制父控件内的子控件?

转载 作者:行者123 更新时间:2023-12-02 04:53:16 26 4
gpt4 key购买 nike

我在 Panel 控件中动态创建了一个 Label 控件。我正在使用鼠标事件移动标签控件。那个时候标签控件移到面板控件之外。我该如何限制它?

最佳答案

您可以利用 Cursor.Clip 来满足您的要求(尽管我们可以在 MouseMove 事件处理程序中手动处理):

    Point downPoint;
//MouseDown event handler for your label1
private void label1_MouseDown(object sender, MouseEventArgs e){
downPoint = e.Location;
//this is the most important code to make it works
Cursor.Clip = yourPanel.RectangleToScreen(new Rectangle(e.X, e.Y, yourPanel.ClientSize.Width - label1.Width, yourPanel.ClientSize.Height - label1.Height));
}
//MouseMove event handler for your label1
private void label1_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
label1.Left += e.X - downPoint.X;
label1.Top += e.Y - downPoint.Y;
}
}
//MouseUp event handler for your label1
private void label1_MouseUp(object sender, MouseEventArgs e){
Cursor.Clip = Rectangle.Empty;
}

关于c# - 如何在 Winform 应用程序中限制父控件内的子控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18436687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com