如何在C#中调整大小并移动任何控件 [英] How can I resize and move any control in c#

查看:78
本文介绍了如何在C#中调整大小并移动任何控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我是C#编程的新手

我想让我的程序用户能够调整大小和移动控件(如Button或PictureBox).

我的程序将使用户能够单击按钮(ADD)将新的pictureBox添加到窗体中,我成功地做到了.然后我尝试通过设置 bool 变量来应用方法,并在PictureBox的 Mousedown 中将其设置为true,在 Mouseup 中将其设置为false并设置位置如果bool为true,则控制 MouseMove 中光标的位置.
问题是,当我插入新的pictureBox时,我无法移动旧的pictureBox,所有动作只会发生在新的PictureBox上.

希望我能解释我的问题,否则,您可能会告诉我有关调整PictureBox大小或移动它的想法.

GoodBye

Hello

I am new to c# programming

I want to enable user of my program to resize and move control (like Button or PictureBox).

my program will enable user to click on button (ADD) to add a new pictureBox to the form, I successfully do that. Then I tried to apply a method by set bool variable , and set it to true in Mousedown of PictureBox ,to false in Mouseup and set location of control to location of cursor in MouseMove If bool is true .
the problem that when I insert new pictureBox, I can not move the old one, all actions happen to new one only.

I hope I can explain my problem, and if not, you may tell me your idea about making a PictureBox is re-sizable or movable.

GoodBye

推荐答案

要移动控件,您只需要MouseDownMouseMove事件处理程序即可;
To move a control you only need the MouseDown and MouseMove event handlers if you do it like;
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        PictureBox picBox = sender as PictureBox;
        picBox.Tag = e.Location;
    }
}

void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    PictureBox picBox = sender as PictureBox;

    if (e.Button == MouseButtons.Left && picBox.Tag != null)
    {
        Point lastLocation = (Point)picBox.Tag;

        picBox.Top = picBox.Top + (e.Location.Y - lastLocation.Y);
        picBox.Left = picBox.Left + (e.Location.X - lastLocation.X);
    }
}


如果我了解您在追求什么...

MouseDown事件处理程序中,sender参数表示发送事件的控件(使用前必须将其强制转换为PictureBox(或Control)类型.一旦完成,就可以保存它到一个全局类变量以在表单的其他地方使用.
If I understand what you''re after...

In your MouseDown event handler, the sender parameter represents the control sending the event (you have to cast it to a PictureBox (or Control) type before using it. Once you do that, you can save it to a global class variable to use elsewhere in the form.


您可以看到此链接

点击

点击

点击
You can see this links

Click

Click

Click


这篇关于如何在C#中调整大小并移动任何控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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