gpt4 book ai didi

c# - 如何创建悬停的 C# Winforms 控件

转载 作者:太空狗 更新时间:2023-10-29 17:49:03 25 4
gpt4 key购买 nike

如何创建超出其区域边界的 C# Winforms 控件?比如下拉框。有点像在小型面板中有一个 DropDownBox。

最佳答案

Windows 窗体不能很好地支持窗口,它从根本上与设计器不兼容。这里有一些代码可以帮助您入门。您不能在设计器中使用此控件,它必须在运行时创建。您还必须自己调用其 Dispose() 方法。

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

public class MyListBox : ListBox {
private Control mParent;
private Point mPos;
private bool mInitialized;

public MyListBox(Control parent) {
mParent = parent;
mInitialized = true;
this.SetTopLevel(true);
parent.LocationChanged += new EventHandler(parent_LocationChanged);
mPos = mParent.Location;
}

public new Point Location {
get { return mParent.PointToClient(this.Location); }
set {
Point zero = mParent.PointToScreen(Point.Empty);
base.Location = new Point(zero.X + value.X, zero.Y + value.Y);
}
}

protected override Size DefaultSize {
get {
return mInitialized ? base.DefaultSize : Size.Empty;
}
}

protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
if (this.mInitialized)
base.SetBoundsCore(x, y, width, height, specified);
}

void parent_LocationChanged(object sender, EventArgs e) {
base.Location = new Point(base.Left + mParent.Left - mPos.X, base.Top + mParent.Top - mPos.Y);
mPos = mParent.Location;
}

protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
if (mParent != null && !DesignMode) {
cp.Style = (int)(((long)cp.Style & 0xffff) | 0x90200000);
cp.Parent = mParent.Handle;
Point pos = mParent.PointToScreen(Point.Empty);
cp.X = pos.X;
cp.Y = pos.Y;
cp.Width = base.DefaultSize.Width;
cp.Height = base.DefaultSize.Height;
}
return cp;
}
}
}

关于c# - 如何创建悬停的 C# Winforms 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/353561/

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