2

Im trying to code a simple slideshow program. I have a class named SlideShowText. I want it to be scalable by mouse like scaling the control in Visual Studio. I want it to have dotted borders. Any help? (I didn't try anything since im such a noob)

Talha Talip Açıkgöz
  • 1,821
  • 4
  • 15
  • 28
  • 1
    Did you try searching at least? - [See here](http://stackoverflow.com/questions/20870285/how-to-make-an-object-scalable-while-rendered-in-a-form?rq=1) - You will only need the lower left corner, I'd say.. – TaW Sep 06 '16 at 17:23
  • WinForms is not the best choice for that... Does your form has only text and borders. If so, then you might draw yourself exactly like you want... – Phil1970 Sep 06 '16 at 21:55
  • That kind of control and behavior is easier to do in WPF if some want to zoom other controls like button, scroll bars... One can easily embed WPF controls in a WinForms application. – Phil1970 Sep 06 '16 at 21:57
  • Additional to options proposed by Taw and Alexander Petrov, you also can hanlde `WM_NCHITTEST` for your control and perform resizing. For example take a look at [this](http://stackoverflow.com/a/19670447/3110834) example which is for `Form` but can simply apply to controls even without creating an inherited control. – Reza Aghaei Sep 07 '16 at 05:54
  • Have you resolved your problems? - If you are happy with an answer, please consider [accepting](http://stackoverflow.com/help/accepted-answer) it..! – TaW Sep 13 '16 at 09:25

2 Answers2

3

Here is a Resizer control:

public partial class Resizer : Control
{
    public Resizer()
    {
        InitializeComponent();
        Size = new System.Drawing.Size(12, 12);
    }

    public Control control { get; set; }
    Point mDown = Point.Empty;

    public Resizer(Control ctl) 
    {
        InitializeComponent();
        control = ctl;
        Size = new System.Drawing.Size(12, 12);
    }

    protected override void OnLayout(LayoutEventArgs levent)
    {
        base.OnLayout(levent);
        if (control != null)
        {
            BackColor = Color.DarkOrange;
            Location = new Point(control.Width - Size.Width, 
                                 control.Height - Size.Height);
            Parent = control;
            BringToFront();
        }
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        mDown = e.Location;
        base.OnMouseDown(e);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        Hide();
        base.OnMouseUp(e);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (e.Button.HasFlag(MouseButtons.Left))
        {
            Left += -(mDown.X - e.X);
            Top += -(mDown.Y - e.Y);
            control.Size = new Size(Right, Bottom);
        }
        base.OnMouseMove(e);
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
    }
}

After adding it to the project you can use it maybe like this:

Resizer rz = null;

private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
    if (rz == null) rz = new Resizer(pictureBox1);  else rz.Show();
}

Result:

enter image description here

TaW
  • 53,122
  • 8
  • 69
  • 111
3

You can make any control sizeable with the mouse, overriding its CreateParams property.

For example

class SizeableRichTextBox : RichTextBox
{
    protected override CreateParams CreateParams
    {
        get
        {
            const int WS_SIZEBOX = 0x40000;
            var cp = base.CreateParams;
            cp.Style |= WS_SIZEBOX;
            return cp;
        }
    }
}

class SizeablePictureBox : PictureBox
{
    protected override CreateParams CreateParams
    {
        get
        {
            const int WS_SIZEBOX = 0x40000;    
            var cp = base.CreateParams;
            cp.Style |= WS_SIZEBOX;    
            return cp;
        }
    }
}

Now they have a thick border and can be resized with the mouse.

The image scale in the SizeablePictureBox will change automatically, if you set the ImageLayout.Zoom parameter.

var pictureBox = new SizeablePictureBox { Parent = this, Width = 500, Height = 500 };
pictureBox.BackgroundImageLayout = ImageLayout.Zoom;
pictureBox.BackgroundImage = Image.FromFile("pic.jpg");

To change the text scale in the TextBox you have to manually calculate the aspect ratio and change the font size. Or you can try to change the value of ZoomFactor property.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49