应用程序运行时的可调整大小控件

编程入门 行业动态 更新时间:2024-10-07 00:27:04
本文介绍了应用程序运行时的可调整大小控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Windows窗体应用程序? 有没有办法制作可调整大小的控件(如微软Powerpoint中的文本框)?

解决方案

为什么不呢?这些都可以调整大小: http:/ /msdn.microsoft/en-us/library/system.windows.forms.control.size%28v=vs.110%29.aspx [ ^ ]。 如您所见,您可以随时更改尺寸。 唯一的问题是创建一个控件会调整其他控件的大小。它可以是任何东西,甚至滚动条或滑块:-)。 最好不要以任意方式调整控件大小(你要在哪里调整大小)它们?在良好的设计中,这个空间被其他控件所占据!:-))这两个是专为调整大小而设计的,更加合理: msdn.microsoft/en-us/library/system.windows。 forms.splitter%28v = vs.110%29.aspx [ ^ ], msdn.microsoft/en-us/library/system.windows.forms.splitcontainer%28v= vs.110%29.aspx [ ^ ]。
-SA

要在运行时重新调整控件大小,您需要为控件实现三个EventHandler(在此处显示为Form Load EventHandler中的有线连接):

// 你需要跟踪'状态: 私有 void Form1_Load( object sender,EventArgs e) { YourControl.MouseDown + = YourControl_MouseDown; YourControl.MouseUp + = YourControl_MouseUp; YourControl.MouseMove + = YourControl_MouseMove; } 私人 bool IsMouseUp = 真; // 是鼠标上/下? 私人 Point MouseDownPoint; // 点击鼠标的位置 私有尺寸ControlSize; // EventHandlers private void YourControl_MouseDown( object sender,MouseEventArgs e) { IsMouseUp = false ; ControlSize = YourControl.Size; MouseDownPoint = e.Location; } 私有 void YourControl_MouseUp( object sender,MouseEventArgs e) { IsMouseUp = true ; } private void YourControl_MouseMove( object sender,MouseEventArgs e) { if (IsMouseUp)返回; YourControl.Width = ControlSize.Width + e.X - MouseDownPoint.X; YourControl.Height = ControlSize.Height + e.Y - MouseDownPoint.Y; }

这当然是非常简单的重新调整大小:我们保持Control的左上角固定并根据计算的相对偏移量操纵其宽度和高度鼠标移动事件期间鼠标的初始位置和当前位置。 而且,我们没有检查控件如此大部分内容离屏。当然,您可以通过在设计时设置最小/最大尺寸属性来限制控件的重新调整大小,我强烈建议您这样做,这样您的用户就不会意外地隐藏控件! 当您想要检测重新调整大小的方向并实际移动控件以及重新调整大小时,它会变得更加有趣。那是......另一个故事。

I am working on windows Form Application ? Is there any way to make a resizable controls (like textbox in microsoft Powerpoint) ?

解决方案

Why not? The are all resizeable: msdn.microsoft/en-us/library/system.windows.forms.control.size%28v=vs.110%29.aspx[^]. As you can see, you can always change the size. The only problem is to create a control which would resize other control(s). It could be anything, even the scroll bar or the slider :-). It's much better not to resize controls in an arbitrary way (where are you going to resize them? In good design, this space is occupied with other controls! :-)) These two are specifically designed for resizing, in much more reasonable way: msdn.microsoft/en-us/library/system.windows.forms.splitter%28v=vs.110%29.aspx[^], msdn.microsoft/en-us/library/system.windows.forms.splitcontainer%28v=vs.110%29.aspx[^].
—SA

To re-size Controls at run-time you are going to need to implement three EventHandlers for the Control (shown here wired-up in the Form Load EventHandler):

// you'll need to keep track of 'state: private void Form1_Load(object sender, EventArgs e) { YourControl.MouseDown += YourControl_MouseDown; YourControl.MouseUp += YourControl_MouseUp; YourControl.MouseMove += YourControl_MouseMove; } private bool IsMouseUp = true; // is the mouse up/down ? private Point MouseDownPoint; // where the mouse was clicked private Size ControlSize; // EventHandlers private void YourControl_MouseDown(object sender, MouseEventArgs e) { IsMouseUp = false; ControlSize = YourControl.Size; MouseDownPoint = e.Location; } private void YourControl_MouseUp(object sender, MouseEventArgs e) { IsMouseUp = true; } private void YourControl_MouseMove(object sender, MouseEventArgs e) { if (IsMouseUp) return; YourControl.Width = ControlSize.Width + e.X - MouseDownPoint.X; YourControl.Height = ControlSize.Height + e.Y - MouseDownPoint.Y; }

This is, of course, pretty simple re-sizing: we're keeping the left-top corner of the Control "pinned" and manipulating its width and height based on relative offsets calculated from the initial and current position of the mouse during the MouseMove Event. And, we're not doing any checking for things like making the Control so large part of it goes off-screen. You can, of course, limit the re-sizing of the Control by setting the Minimum- / Maximum- Size Properties at design time, and I strongly suggest you do that so your users will not accidentally hide the Control ! It gets much more interesting when you want to detect the direction of re-size and actually move the Control as well as re-size it. That's ... another story.

更多推荐

应用程序运行时的可调整大小控件

本文发布于:2023-11-29 15:39:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646821.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   应用程序   可调整   大小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!