mfc 控件大小 调整大小_在运行时调整控件大小的简单技巧

mfc 控件大小 调整大小

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods.

本文介绍了一种在运行时调整控件大小的简单方法。 它包括现成的源代码和完整的示例演示应用程序。 我们还将讨论C#扩展方法。

Demo

Introduction

介绍

In one of my applications I needed a control that could be resizable at run time. Just like the canvas in MSPaint, you can resize it inside the host window. After a quick search on the internet I noticed that most solutions treating this simple issue as a beast.  An example of a "complex implementation" is in the EE Solution, here.

在我的一个应用程序中,我需要一个可以在运行时调整大小的控件。 就像MSPaint中的画布一样,您可以在主机窗口中调整其大小。 在互联网上快速搜索之后,我注意到大多数解决方案都将这个简单的问题视为野兽。 “复杂实现”的一个示例在EE解决方案中( 此处)

Using SendMessage is an intuitive temptation.  We could try the technique I descried in my previous article on how to drag a form -- wehe we simply flood mouse movement to the caption of the host form.  However, the parameters for resizing message scattered to eight directions and you'd have to calculate the delta in each direction to make it work.  That’s why the code tends to become so bulky.

使用SendMessage是一种直观的诱惑。 我们可以尝试在上一篇文章中介绍的有关如何拖动表单的技术-我们只是将鼠标移动泛滥到宿主表单的标题上。 但是,用于调整邮件大小的参数分散到八个方向,您必须计算每个方向上的增量才能使其正常工作。 这就是为什么代码倾向于变得如此庞大的原因。

Using PictureBox as decoration

使用PictureBox作为装饰

When a control is runtime resizable, you'll certainly want to provide some visual indication to users. You can manually draw an “edge” on the control using graphic functions, and manually change the cursor type when the mouse moves into a range of control’s edge. It’s not hard to imagine that pages of source code might be needed to make it work smoothly.  I would rather simply use some control components to achieve the functionality.  My implementation is simple:

当控件在运行时可调整大小时,您当然希望向用户提供一些视觉指示。 您可以使用图形功能在控件上手动绘制“边缘”,并在鼠标移至控件边缘范围内时手动更改光标类型。 不难想象可能需要源代码页面才能使其顺利运行。 我宁愿简单地使用一些控制组件来实现功能。 我的实现很简单:

1. Place three PictureBox controls into the target control you want it to be resizable.

1.将三个PictureBox控件放入您希望其可调整大小的目标控件中。

2. Arrange one picture box to the right edge of target control. Its height equals to the height of the

2.将一个图片框排列在目标控件的右边缘。 它的高度等于

    target control minus 3. And set its anchor style as top, bottom, right.

目标控件减去3.并将其锚点样式设置为上,下,右。

3. Arrange the second box to the bottom edge of the target control. Its width equals to the width

3.将第二个框排列在目标控件的底部边缘。 其宽度等于宽度

    of the target control minus 3. And set its anchor style as left, right, bottom.

目标控件的减号3。并将其锚点样式设置为左,右,下。

4. Place the third picture box at the right bottom corner of the target control. Both of its width

4.将第三个图片框放在目标控件的右下角。 两者的宽度

    and height is 3.  Set its anchor style as right, bottom.

高度为3。将锚定样式设置为右,下。

5. Finally subscribe to the MouseMove of each picture box. Add the following codes in the

5.最后,订阅每个图片框的

    event handler:

事件处理程序:

if (e.Button == System.Windows.Forms.MouseButtons.Left) 
{ 
    controltobeResized.Width += e.X; 
    controltobeResized.Height += e.Y; 
    if (controltobeResized.Width < decoration) 
    { 
        controltobeResized.Width = decoration; 
    } 
    if (controltobeResized.Height < decoration) 
    { 
        controltobeResized.Height = decoration; 
    } 
}

Now in the runtime, when you move your mouse over a picture box, the cursor will change its shape accordingly.  So there is your visible user feedback.   Press down the left button to begin to resize the control.  The actual implementation is even easier than it sounds (see the attached code).

现在,在运行时中,将鼠标移到图片框上时,光标将相应地更改其形状。 因此,您将看到可见的用户反馈。 按下左按钮开始调整控件的大小。 实际实现甚至比听起来容易得多(请参阅随附的代码)。

Reuse the code

重用代码

Life is good, and it can be even better.

生活是美好的,甚至可以变得更好。

I put the operations described above in a separate class named as TCResize, which of course contains the three picture boxes and the hook to their events. The only thing you need to do is to include the class in your project and add one line of code to create an instance with the target control as parameter:

我将上述操作放在了一个单独的名为TCResize的类中,该类当然包含三个图片框及其事件挂钩。 您唯一需要做的就是将类包含在项目中,并添加一行代码以使用目标控件作为参数来创建实例:

TCResize resizabletreeView = new TCResize(this.treeView1);
TCResize, is attached.

Extension Methods

扩展方法

In my application, I need to know the exact size of the target control.  While the decorative picture boxes occupied a little edge area of the target control, each time when I ask for the dimension information, I need to exclude the dimension of the decoration components. Prior to C# 3.0 (Visual Studio 2008), you may have to derive a class to override the build-in methods. Then it definitely breaks the concise way of reuse. Fortunately, the concept of an Extension Method (introduced in C# 2.0 and fully supported in C# 3.0) provides great flexibility to extent the behaviours of an “old time” class.

在我的应用程序中,我需要知道目标控件的确切大小。 尽管装饰画框仅占目标控件的边缘区域,但是每次我要求尺寸信息时,都需要排除装饰组件的尺寸。 在C#3.0(Visual Studio 2008)之前,您可能必须派生一个类以覆盖内置方法。 然后,它肯定会破坏简洁的重用方式。 幸运的是, 扩展方法 (在C#2.0中引入并在C#3.0中得到完全支持)的概念为扩展 “旧时”类的行为提供了极大的灵活性。

The syntax of creating Extension Methods is widely introduced on the Internet resources.  Briefly, you need to declare it as static methods in a top level static class. Using this keyword prefix to the type of the parameter passed into method.  In short: You can create extension methods on a base class and all its derived ones will pick up the methods automatically. The best part is that you can improve all of the derived classes simultaneously by extending their common base class!

Internet资源上广泛介绍了创建扩展方法的语法。 简要地说,您需要在顶级静态类中将其声明为静态方法。 使用此关键字前缀作为传递给方法的参数的类型。 简而言之:您可以在基类上创建扩展方法,其所有派生方法都会自动选择方法。 最好的部分是,您可以通过扩展它们的公共基类来同时改进所有派生类!

Thus we add some get and set methods to the base Control class. The following code snippet is a sample method. Extension properties have not been supported.

因此,我们将一些get和set方法添加到基本Control类。 以下代码段是一个示例方法。 不支持扩展属性。

public static int GetClientWidth(this Control theControl) 
        { 
            return theControl.Width - TCResize.Decoration; 
        }

Extension topics

扩展主题

Attached there is also a sample application TCPaint.exe (run on .net framework 3.5). It is developed to demonstrate several tricks published and those to be published. I originally just wanted to create something really simple like Windows mspaint.exe. After a couple of weeks of work, it has evolved into an application that provides unlimited steps of undo and redo, as well as editing of strokes and screen capture. The development work is still ongoing.  Bugs and butterflies are inevitable. It’s just attached to show the resizing trick in a real life application. After you draw a shape on the canvas, you can drag a bullet point (handle) to tune the shape.  They are also picture boxes.:)

附带的还有一个示例应用程序TCPaint.exe(在.net framework 3.5上运行)。 它的开发目的是演示已发布和将要发布的一些技巧。 我最初只是想创建一些非常简单的东西,例如Windows mspaint.exe。 经过几周的工作,它已经发展成为一种应用程序,它提供了无限的撤消和重做步骤,以及笔触和屏幕截图的编辑。 开发工作仍在进行中。 虫子和蝴蝶是不可避免的。 附带它是为了显示现实应用程序中的调整大小技巧。 在画布上绘制形状后,可以拖动项目符号点(手柄)以调整形状。 它们也是图片框。:)

Any comments on where this TCPaint program should lead to will be more than welcomed.

对于此TCPaint程序应指向何处的任何评论都将受到欢迎。

Code Attachments:

代码附件:

ResizableControl.zip ResizableControl.zip TCPaint.zip TCPaint.zip

翻译自: https://www.experts-exchange.com/articles/4274/A-simple-trick-to-resize-a-control-at-runtime.html

mfc 控件大小 调整大小

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值