有没有什么办法可以放大c#中的按钮?
您可以通过mouse-enter-event中的代码更改按钮大小。并在鼠标离开事件中重置它。
不仅尺寸而且位置(如果你不想从左上方放大)。如果按钮被锚定或布局面板中,它可能会变得更复杂。 – Joey
我很困惑。你可以写代码吗? – user1436685
是的,位置也必须改变。 – Tomtom
可能类似于此:
Button.MouseEnter += new EventHandler(delegate(object Sender, EventArgs e) { Button.Size = new Size(Button.Size.Width + 50, Button.Size.Height + 50); } Button.Location = new Point(Button.Location.X - (50/2), Button.Location.Y - (50/2)});
Button.MouseLeave += new EventHandler(delegate(object Sender, EventArgs e) { Button.Size = new Size(Button.Size.Width - 50, Button.Size.Height - 50 }; Button.Location = new Point(Button.Location.X + (50/2), Button.Location.Y + (50/2)});
Button.GotFocus += new EventHandler(delegate(object Sender, EventArgs e) { Button.Size = new Size(Button.Size.Width + 50, Button.Size.Height + 50); } Button.Location = new Point(Button.Location.X - (50/2), Button.Location.Y - (50/2)});
Button.LostFocus += new EventHandler(delegate(object Sender, EventArgs e) { Button.Size = new Size(Button.Size.Width - 50, Button.Size.Height - 50 }; Button.Location = new Point(Button.Location.X + (50/2), Button.Location.Y + (50/2)});
你也回路设置到“This.controls”事件,并且定义每个按钮,然后添加此事件。这是脚本,你可以做任何事情=)
这是它应该工作的方式。不过要小心。如果您选择较大的缩放值,则可能会发生缩放控件与另一个控件叠加的情况。 – Tomtom
你将不得不处理MouseEnter/MouseLeave和GotFocus/LostFocus事件来说明键盘导航。
这样的效果在WPF应用程序中更容易。也许你应该考虑创建一个WPF应用程序,如果你需要视觉效果。检查Scale transform in xaml (in a controltemplate) on a button to perform a "zoom",其中类似的要求通过缩放按钮来处理,方式可以附加到任何您想要的按钮,避免编写代码。
最简单的方法似乎是使用SetBounds
。 Control.Scale
不能正常工作,因为它假设您缩放包含所有子控件的完整窗口,因此将始终从视口的左上角(在本例中为窗口客户机框架)缩放。
Button b;
public Form1()
{
InitializeComponent();
b = new Button();
b.Text = "Hover me";
b.Top = 100;
b.Left = 100;
b.Size = new Size(80, 30);
this.Controls.Add(b);
b.MouseEnter += delegate(object sender, EventArgs e)
{
b.SetBounds(b.Left - 5, b.Top - 2, b.Width + 10, b.Height + 4);
};
b.MouseLeave += delegate(object sender, EventArgs e)
{
b.SetBounds(b.Left + 5, b.Top + 2, b.Width - 10, b.Height - 4);
};
}
plz举个例子。 – user1436685
[你有什么试过吗?](http://whathaveyoutrid.com) –
我认为win表单不支持这种图形效果。试试WPF。 – davioooh
davioooh,它可能不像WPF那样优雅,但是您可以在Windows窗体中轻松完成相同操作。 – Joey