Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在winforms中,用户如何在运行时调整控件的大小_C#_.net_Winforms - Fatal编程技术网

C# 在winforms中,用户如何在运行时调整控件的大小

C# 在winforms中,用户如何在运行时调整控件的大小,c#,.net,winforms,C#,.net,Winforms,假设我有一个图片盒 现在我想要的是,用户应该能够随意调整pictureBox的大小。然而,我甚至不知道如何开始这件事。我在网上搜索过,但信息很少 至少有人能告诉我从哪里开始吗?这里有一篇文章 这应该会对你有所帮助,因为它在vb中是C#翻译的 using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; public cl

假设我有一个图片盒

现在我想要的是,用户应该能够随意调整pictureBox的大小。然而,我甚至不知道如何开始这件事。我在网上搜索过,但信息很少

至少有人能告诉我从哪里开始吗?

这里有一篇文章

这应该会对你有所帮助,因为它在vb中是C#翻译的

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{


    ResizeableControl rc;

    private void Form1_Load(System.Object sender, System.EventArgs e)
    {
        rc = new ResizeableControl(pbDemo);

    }
    public Form1()
    {
        Load += Form1_Load;
    }

}
和重新大小函数

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class ResizeableControl
{

    private Control withEventsField_mControl;
    private Control mControl {
        get { return withEventsField_mControl; }
        set {
            if (withEventsField_mControl != null) {
                withEventsField_mControl.MouseDown -= mControl_MouseDown;
                withEventsField_mControl.MouseUp -= mControl_MouseUp;
                withEventsField_mControl.MouseMove -= mControl_MouseMove;
                withEventsField_mControl.MouseLeave -= mControl_MouseLeave;
            }
            withEventsField_mControl = value;
            if (withEventsField_mControl != null) {
                withEventsField_mControl.MouseDown += mControl_MouseDown;
                withEventsField_mControl.MouseUp += mControl_MouseUp;
                withEventsField_mControl.MouseMove += mControl_MouseMove;
                withEventsField_mControl.MouseLeave += mControl_MouseLeave;
            }
        }
    }
    private bool mMouseDown = false;
    private EdgeEnum mEdge = EdgeEnum.None;
    private int mWidth = 4;

    private bool mOutlineDrawn = false;
    private enum EdgeEnum
    {
        None,
        Right,
        Left,
        Top,
        Bottom,
        TopLeft
    }

    public ResizeableControl(Control Control)
    {
        mControl = Control;
    }


    private void mControl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left) {
            mMouseDown = true;
        }
    }


    private void mControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        mMouseDown = false;
    }


    private void mControl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        Control c = (Control)sender;
        Graphics g = c.CreateGraphics;
        switch (mEdge) {
            case EdgeEnum.TopLeft:
                g.FillRectangle(Brushes.Fuchsia, 0, 0, mWidth * 4, mWidth * 4);
                mOutlineDrawn = true;
                break;
            case EdgeEnum.Left:
                g.FillRectangle(Brushes.Fuchsia, 0, 0, mWidth, c.Height);
                mOutlineDrawn = true;
                break;
            case EdgeEnum.Right:
                g.FillRectangle(Brushes.Fuchsia, c.Width - mWidth, 0, c.Width, c.Height);
                mOutlineDrawn = true;
                break;
            case EdgeEnum.Top:
                g.FillRectangle(Brushes.Fuchsia, 0, 0, c.Width, mWidth);
                mOutlineDrawn = true;
                break;
            case EdgeEnum.Bottom:
                g.FillRectangle(Brushes.Fuchsia, 0, c.Height - mWidth, c.Width, mWidth);
                mOutlineDrawn = true;
                break;
            case EdgeEnum.None:
                if (mOutlineDrawn) {
                    c.Refresh();
                    mOutlineDrawn = false;
                }
                break;
        }

        if (mMouseDown & mEdge != EdgeEnum.None) {
            c.SuspendLayout();
            switch (mEdge) {
                case EdgeEnum.TopLeft:
                    c.SetBounds(c.Left + e.X, c.Top + e.Y, c.Width, c.Height);
                    break;
                case EdgeEnum.Left:
                    c.SetBounds(c.Left + e.X, c.Top, c.Width - e.X, c.Height);
                    break;
                case EdgeEnum.Right:
                    c.SetBounds(c.Left, c.Top, c.Width - (c.Width - e.X), c.Height);
                    break;
                case EdgeEnum.Top:
                    c.SetBounds(c.Left, c.Top + e.Y, c.Width, c.Height - e.Y);
                    break;
                case EdgeEnum.Bottom:
                    c.SetBounds(c.Left, c.Top, c.Width, c.Height - (c.Height - e.Y));
                    break;
            }
            c.ResumeLayout();
        } else {
            switch (true) {
                case e.X <= (mWidth * 4) & e.Y <= (mWidth * 4):
                    //top left corner
                    c.Cursor = Cursors.SizeAll;
                    mEdge = EdgeEnum.TopLeft;
                    break;
                case e.X <= mWidth:
                    //left edge
                    c.Cursor = Cursors.VSplit;
                    mEdge = EdgeEnum.Left;
                    break;
                case e.X > c.Width - (mWidth + 1):
                    //right edge
                    c.Cursor = Cursors.VSplit;
                    mEdge = EdgeEnum.Right;
                    break;
                case e.Y <= mWidth:
                    //top edge
                    c.Cursor = Cursors.HSplit;
                    mEdge = EdgeEnum.Top;
                    break;
                case e.Y > c.Height - (mWidth + 1):
                    //bottom edge
                    c.Cursor = Cursors.HSplit;
                    mEdge = EdgeEnum.Bottom;
                    break;
                default:
                    //no edge
                    c.Cursor = Cursors.Default;
                    mEdge = EdgeEnum.None;
                    break;
            }
        }
    }


    private void mControl_MouseLeave(object sender, System.EventArgs e)
    {
        Control c = (Control)sender;
        mEdge = EdgeEnum.None;
        c.Refresh();
    }

}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用系统数据;
使用系统诊断;
公共类可调整控件
{
事件现场控制的私人控制;
专用控制{
获取{returnwitheventsfield\u mControl;}
设置{
如果(带事件字段控制!=null){
withEventsField\u mControl.MouseDown-=mControl\u MouseDown;
withEventsField\u mControl.MouseUp-=mControl\u MouseUp;
withEventsField\u mControl.MouseMove-=mControl\u MouseMove;
带事件字段\u mControl.MouseLeave-=mControl\u MouseLeave;
}
withEventsField\u mControl=值;
如果(带事件字段控制!=null){
withEventsField\u mControl.MouseDown+=mControl\u MouseDown;
withEventsField\u mControl.MouseUp+=mControl\u MouseUp;
withEventsField\u mControl.MouseMove+=mControl\u MouseMove;
带事件字段\u mControl.MouseLeave+=mControl\u MouseLeave;
}
}
}
private bool mMouseDown=false;
private EdgeEnum mEdge=EdgeEnum.None;
私有整数mWidth=4;
private bool MoutlineDrawed=假;
私有枚举边缘
{
没有一个
正确的,
左边
顶部
底部,
左上角
}
公共可调整控件(控件)
{
mControl=控制;
}
私有void mControl\u MouseDown(对象发送方,System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left){
mMouseDown=true;
}
}
私有void mControl\u MouseUp(对象发送方,System.Windows.Forms.MouseEventArgs e)
{
mMouseDown=false;
}
私有void mControl\u MouseMove(对象发送方,System.Windows.Forms.MouseEventArgs e)
{
控制c=(控制)发送方;
图形g=c.CreateGraphics;
开关(mEdge){
案例边缘。左上角:
g、 圆角矩形(画笔。紫红色,0,0,mWidth*4,mWidth*4);
MoutlineDrawed=真;
打破
案件边缘。左:
g、 圆角矩形(画笔。紫红色,0,0,宽度,c高度);
MoutlineDrawed=真;
打破
案例EdgeEnum。右侧:
g、 圆角矩形(画笔。紫红色,c.宽度-宽度,0,c.宽度,c.高度);
MoutlineDrawed=真;
打破
案例边缘。顶部:
g、 圆角矩形(画笔。紫红色,0,0,c。宽度,mWidth);
MoutlineDrawed=真;
打破
箱子边缘。底部:
g、 圆角矩形(画笔。紫红色,0,c.高度-米宽,c.宽度,米宽);
MoutlineDrawed=真;
打破
案例EdgeEnum。无:
如果(MoutlineDrawed){
c、 刷新();
MoutlineDrawed=假;
}
打破
}
如果(mMouseDown&mEdge!=EdgeEnum.None){
c、 SuspendLayout();
开关(mEdge){
案例边缘。左上角:
c、 立根(c.左+东X,c.顶+东Y,c.宽,c.高);
打破
案件边缘。左:
c、 立根(c.左+东X,c.顶,c.宽-东X,c.高);
打破
案例EdgeEnum。右侧:
c、 立根(c.左侧,c.顶部,c.宽度-(c.宽度-e.X),c.高度);
打破
案例边缘。顶部:
c、 立根(c.左侧,c.顶部+e.Y,c.宽度,c.高度-e.Y);
打破
箱子边缘。底部:
c、 立根(c.左侧,c.顶部,c.宽度,c.高度-(c.高度-e.Y));
打破
}
c、 恢复布局();
}否则{
开关(真){

case e.X这很容易做到,Windows中的每个窗口都具有天生的可调整大小的能力。对于PictureBox,它只是关闭了,您可以通过收听来重新打开它。您只需告诉Windows光标位于窗口的一角,您就可以免费获得其他所有内容。您还需要绘制一个抓取手柄,以使其清晰可见对于用户,拖动角点将调整框的大小

向项目中添加一个新类并粘贴如下所示的代码。Build+Build。您将在工具箱顶部获得一个新控件,将其放到窗体上。设置Image属性,然后您就可以尝试了

using System;
using System.Drawing;
using System.Windows.Forms;

class SizeablePictureBox : PictureBox {
    public SizeablePictureBox() {
        this.ResizeRedraw = true;
    }
    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);
        var rc = new Rectangle(this.ClientSize.Width - grab, this.ClientSize.Height - grab, grab, grab);
        ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc); 
    }
    protected override void WndProc(ref Message m) {
        base.WndProc(ref m);
        if (m.Msg == 0x84) {  // Trap WM_NCHITTEST
            var pos = this.PointToClient(new Point(m.LParam.ToInt32()));
            if (pos.X >= this.ClientSize.Width - grab && pos.Y >= this.ClientSize.Height - grab)
                m.Result = new IntPtr(17);  // HT_BOTTOMRIGHT
        }
    }
    private const int grab = 16;
}
另一种免费调整大小的非常便宜的方法是为控件提供一个可调整大小的边框。该边框适用于所有角和边。将此代码粘贴到类中(您不再需要WndProc):


使用中的use ControlMoverOrResizer类,只需一行代码即可在运行时执行可移动和可调整大小的控件!:)示例:

ControlMoverOrResizer.Init(按钮1)

现在button1是一个可移动且可调整大小的控件!

创建一个新的c#winform应用程序并粘贴以下内容:

当它帮助你的时候,别忘了说谢谢

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类MyForm:Form
{
//公开声明:
双rW=0;
双相对湿度=0;
int fH=0;
protected override CreateParams CreateParams {
    get {
        var cp = base.CreateParams;
        cp.Style |= 0x840000;  // Turn on WS_BORDER + WS_THICKFRAME
        return cp;
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
  {
 public partial class MyForm : Form
  {
    //Public Declaration:
    double rW = 0;
    double rH = 0;

    int fH = 0;
    int fW = 0;


    // @ Form Initialization
    public MyForm()
    {
        InitializeComponent();
        this.Resize += MyForm_Resize; // handles resize routine
        this.tabControl1.Dock = DockStyle.None;

    }


    private void MyForm_Resize(object sender, EventArgs e)
    {
        rResize(this,true); //Call the routine

    }

    private void rResize(Control t, bool hasTabs) // Routine to Auto resize the control
    {

        // this will return to normal default size when 1 of the conditions is met

        string[] s = null;

        if (this.Width < fW || this.Height < fH)
        {

            this.Width = (int)fW;
            this.Height = (int)fH;

            return;
        }

        foreach (Control c in t.Controls)
        {
            // Option 1:
            double rRW = (t.Width > rW ? t.Width / (rW) : rW / t.Width);
            double rRH = (t.Height > rH ? t.Height / (rH) : rH / t.Height);

            // Option 2:
            //  double rRW = t.Width / rW;
            //  double rRH = t.Height / rH;

            s = c.Tag.ToString().Split('/');
            if (c.Name == s[0].ToString())
            {
                //Use integer casting
                c.Width = (int)(Convert.ToInt32(s[3]) * rRW);
                c.Height = (int)(Convert.ToInt32(s[4]) * rRH);
                c.Left = (int)(Convert.ToInt32(s[1]) * rRW);
                c.Top = (int)(Convert.ToInt32(s[2]) * rRH);
            }
            if (hasTabs)
            {
                if (c.GetType() == typeof(TabControl))
                {

                    foreach (Control f in c.Controls)
                    {
                        foreach (Control j in f.Controls) //tabpage
                        {
                            s = j.Tag.ToString().Split('/');

                            if (j.Name == s[0].ToString())
                            {

                                j.Width = (int)(Convert.ToInt32(s[3]) * rRW);
                                j.Height = (int)(Convert.ToInt32(s[4]) * rRH);
                                j.Left = (int)(Convert.ToInt32(s[1]) * rRW);
                                j.Top = (int)(Convert.ToInt32(s[2]) * rRH);
                            }
                        }
                    }
                }
            }

        }
    }

    // @ Form Load Event
    private void MyForm_Load(object sender, EventArgs e)
    {


        // Put values in the variables

        rW = this.Width;
        rH = this.Height;

        fW = this.Width;
        fH = this.Height;


        // Loop through the controls inside the  form i.e. Tabcontrol Container
        foreach (Control c in this.Controls)
        {
            c.Tag = c.Name + "/" + c.Left + "/" + c.Top + "/" + c.Width + "/" + c.Height;

            // c.Anchor = (AnchorStyles.Right |  AnchorStyles.Left ); 

            if (c.GetType() == typeof(TabControl))
            {

                foreach (Control f in c.Controls)
                {

                    foreach (Control j in f.Controls) //tabpage
                    {
                        j.Tag = j.Name + "/" + j.Left + "/" + j.Top + "/" + j.Width + "/" + j.Height;
                    }
                }
            }
        }
    }
}
}