移动图片框的问题

发布于 2024-08-16 22:48:07 字数 779 浏览 8 评论 0原文

我正在编写一款纸牌游戏,当我做视觉部分时,我在将面板内的纸牌从一个地方移动到另一个地方时遇到问题,当我尝试移动它时,图像不断闪烁并移动到每个地方。

这是我的代码......

public partial class Form1 : Form
{
    bool clicked = false;
    public Form1()
    {
        InitializeComponent();
        pictureBox1.ImageLocation = @"c:\kingHearts.png";
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        clicked = true;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (clicked)
            pictureBox1.Location = e.Location;
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        clicked = false;

    }
}

那么出了什么问题,任何人都可以帮忙......

I am programming a cards game, when i was doing the visual part i had a problem with moving the card within a panel from one place to another, the image keeps blinks and moves every where when i try to move it.

This is my code.....

public partial class Form1 : Form
{
    bool clicked = false;
    public Form1()
    {
        InitializeComponent();
        pictureBox1.ImageLocation = @"c:\kingHearts.png";
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        clicked = true;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (clicked)
            pictureBox1.Location = e.Location;
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        clicked = false;

    }
}

So what is wrong, anyone can help plz....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情独悲 2024-08-23 22:48:07

这是一种在运行时通过单击并拖动 ui 对象来移动的非常典型的模式,当控件位于窗体上或像面板这样的容器中时,该模式将起作用

private bool pb_mouseIsDown;
private int oX;
private int oY;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    pb_mouseIsDown = true;
    oX = e.X;
    oY = e.Y;
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    pb_mouseIsDown = false;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (pb_mouseIsDown)
    {
        pictureBox1.Left += e.X - oX;
        pictureBox1.Top += e.Y - oY;
    }
}

注意:...在设计时:如果当控件位于窗体“上”(控件的父级是窗体)时定义 MouseUp、MouseDown 和 MouseMove 的事件处理程序,然后将其剪切并粘贴到容器(如面板)中:您需要在 IDE 中重新建立控件与 MouseDown、MouseUp 和 MouseMove 事件之间的绑定/链接,才能使其正常工作。

A very typical pattern for moving by click-and-drag for ui objects at runtime, and which will work when the control is on a form, or in a container like a Panel :

private bool pb_mouseIsDown;
private int oX;
private int oY;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    pb_mouseIsDown = true;
    oX = e.X;
    oY = e.Y;
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    pb_mouseIsDown = false;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (pb_mouseIsDown)
    {
        pictureBox1.Left += e.X - oX;
        pictureBox1.Top += e.Y - oY;
    }
}

Note : ...at Design Time : if you define the event handlers for MouseUp, MouseDown, and MouseMove while the control is "on" a Form (the parent of the control is the Form), and then cut and paste it into a container, like a Panel : you'll have re-establish the binding/linkage between the control and the MouseDown, MouseUp, and MouseMove events in the IDE for it to work.

佞臣 2024-08-23 22:48:07

e.Location 返回鼠标相对于 PictureBox 的位置。
您需要编写 PointToClient(pictureBox1.PointToScreen(e.Location)) 来获取相对于表单的位置。

e.Location returns the location of the mouse relative to the PictureBox.
You need to write PointToClient(pictureBox1.PointToScreen(e.Location)) to get the location relative to the form.

绅士风度i 2024-08-23 22:48:07

对于闪烁问题,可以将this.DoubleBuffered设置为true。

但是移动 PictureBox 或其他控件效率很低,最好将绘图代码写入 pictureBox1.Paint 事件或使用更快的东西,如 WPF、DirectX 或 OpenGL。

我不知道你想要实现哪些效果,如果一切都是静态的并且你没有大的移动部件,那么当前的解决方案就足够好了。

To the blinking problem, you can set this.DoubleBuffered to true.

But moving PictureBox or another Control is inefficient, better would be to write drawing code into pictureBox1.Paint event or use something faster like the WPF, DirectX or OpenGL.

I don't know which effects do you want to achieve, if everything is static and you don't have big moving parts, then current solution is good enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文