应用程序运行时的可调整大小控件 [英] Resizeable controls on application runtime

查看:78
本文介绍了应用程序运行时的可调整大小控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows窗体应用程序?



有没有办法制作可调整大小的控件(如微软Powerpoint中的文本框)?

解决方案

为什么不呢?这些都可以调整大小: http:/ /msdn.microsoft.com/en-us/library/system.windows.forms.control.size%28v=vs.110%29.aspx [ ^ ]。



如您所见,您可以随时更改尺寸。



唯一的问题是创建一个控件会调整其他控件的大小。它可以是任何东西,甚至滚动条或滑块:-)。



最好不要以任意方式调整控件大小(你要在哪里调整大小)它们?在良好的设计中,这个空间被其他控件所占据!:-))这两个是专为调整大小而设计的,更加合理:

http://msdn.microsoft.com/en-us/library/system.windows。 forms.splitter%28v = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.splitcontainer%28v= vs.110%29.aspx [ ^ ]。



-SA


< blockquote>要在运行时重新调整控件大小,您需要为控件实现三个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: http://msdn.microsoft.com/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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.splitter%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/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.


这篇关于应用程序运行时的可调整大小控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆