tags:

views:

176

answers:

3

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....

A: 

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.

SLaks
partially the problem is solved, but whenever i click on the picture and move it the mouse keeps in the upper left corner of the picture..is there a solution for this problem...???
Waleedoo
+1  A: 

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.

BillW
Thanx BillW it works.but i have one more think, if i wanna dock the card to some other panel on realeas i used the following but i miss the condition...?private void pictureBox1_MouseUp(object sender, MouseEventArgs e){ if() // what is the condition should be 'contain'!! { pictureBox1.Left = panel1.Left; pictureBox1.Top = panel1.Top; }}
Waleedoo
@Waleedoo, When the mouse goes up, after a "card's" moved, only the "card" gets a mouse-up : you need to write code to test which "card slot" to "fit in." Use the 'InterSectsWith method of 'Rectangle class to check for overlap : if (pictureBox1.Bounds.IntersectsWith(panel1.Bounds)) { // match found } A typical strategy : make a List<Rectangle> : do a 'foreach over that list to find which card container it should "snap to fit" to : if no match : snap the card back to its original position. "Snap" to a match is simple : just set the bounds of the card to the bounds of the matching "slot."
BillW
A: 

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.

Filip Kunc