这是我的问题:
我通过编程方式向表单添加标签,包括一些属性,以便在运行时通过鼠标右键单击并拖动它们来调整大小。
我的情况是,我通过OpenDialog从给定文件中编程方式添加一个带图像的标签,并且我想将此图像调整大小以填充标签大小,就像我拉伸标签一样。不幸的是,我无法通过访问标签中的image.Size属性在运行时设置大小,因为它是只读属性...有什么建议吗?
这是受影响的代码片段:
我通过编程方式向表单添加标签,包括一些属性,以便在运行时通过鼠标右键单击并拖动它们来调整大小。
我的情况是,我通过OpenDialog从给定文件中编程方式添加一个带图像的标签,并且我想将此图像调整大小以填充标签大小,就像我拉伸标签一样。不幸的是,我无法通过访问标签中的image.Size属性在运行时设置大小,因为它是只读属性...有什么建议吗?
这是受影响的代码片段:
Point _oldPosition;
public static Label _ctrlActiveControl;
if (e.Button == MouseButtons.Right)
{
_ctrlActiveControl.Cursor = Cursors.SizeNWSE;
//Code to resize the control based on the mouse position
Point newPosition = new Point(e.X, e.Y);
_ctrlActiveControl.Width += (newPosition.X - _oldPosition.X);
_ctrlActiveControl.Height += (newPosition.Y - _oldPosition.Y);
//Some security to make sure I don't shrink the control too much
if (_ctrlActiveControl.Width < 10) _ctrlActiveControl.Width = 10;
if (_ctrlActiveControl.Height < 10) _ctrlActiveControl.Height = 10;
//Here I check if the label contains an image and, if so, I should resize
//The image to "Autofill" the label
if (_ctrlActiveControl.Image != null)
{
Image image = _ctrlActiveControl.Image;
image.Size = new Size(_ctrlActiveControl.Width, _ctrlActiveControl.Height);
}
_oldPosition = newPosition;
}
我想知道是否有任何方法可以做到这一点,或者我应该使用其他控件类型(我知道我可以使用其他控件,但是在添加更多变量之前,我想知道是否有任何可用的解决方法)。