Drag & Drop a pictureBox onto Form - c#

I am writing a game. Player can choose items (such as weapon) and drag them to the form. Items are on the side, in PictureBox controls. I have set Form.AllowDrop to True. When I drag one of the items pictureBoxes, the pictureBox doesn't drop, neither even drag.
I want to drag a pictureBox on the form, or at least know the position in the form which the player want to drag it in.
EDIT: look at the logo above. When you click it and drag (without release) it drags.

In Winforms you need to change the cursor. Here's a complete example, start a new forms project and drop a picturebox on the form. Set its Image property to a small bitmap. Click and drag to drop copies of the image on the form.
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.AllowDrop = true;
this.pictureBox1.MouseDown += pictureBox1_MouseDown;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
var dragImage = (Bitmap)pictureBox1.Image;
IntPtr icon = dragImage.GetHicon();
Cursor.Current = new Cursor(icon);
DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
DestroyIcon(icon);
}
}
protected override void OnGiveFeedback(GiveFeedbackEventArgs e) {
e.UseDefaultCursors = false;
}
protected override void OnDragEnter(DragEventArgs e) {
if (e.Data.GetDataPresent(typeof(Bitmap))) e.Effect = DragDropEffects.Copy;
}
protected override void OnDragDrop(DragEventArgs e) {
var bmp = (Bitmap)e.Data.GetData(typeof(Bitmap));
var pb = new PictureBox();
pb.Image = (Bitmap)e.Data.GetData(typeof(Bitmap));
pb.Size = pb.Image.Size;
pb.Location = this.PointToClient(new Point(e.X - pb.Width/2, e.Y - pb.Height/2));
this.Controls.Add(pb);
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static bool DestroyIcon(IntPtr handle);
}

For the draggable items, you need to call the DoDragDrop method in the MouseDown event. Make sure your form (or target) has the AllowDrop property set to true.
For your target, you need to wire the dragging events:
private void Form1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
// Examine e.Data.GetData stuff
}

Related

Keep the cursor at default shape during drag and drop operation in windows forms c#

I am doing a simple drag and drop operation in windows forms. Whenever I start the operation the cursor changes shape. I know this is directly related to the DragDropEffects and I can't find an option that results in the default cursor. Can anybody help? Here is the code:
a.MouseDown += new MouseEventHandler(ButtonDown);
a.DragEnter += new DragEventHandler(ButtonDragEnter);
a.AllowDrop = true;
and here are the functions:
private void ButtonDown(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
ButtonClick(sender, e);
p.DoDragDrop(p.BackColor, DragDropEffects.All);
}
private void ButtonDragEnter(object sender, DragEventArgs e)
{
e.Data.GetFormats();
e.Effect = DragDropEffects.None;
Color c = new Color();
c = (Color) e.Data.GetData(c.GetType());
ButtonClick(sender, e,c);
}
Ok answered my own question here:
You first need to add an event to giveFeedBack:
a.GiveFeedback += new GiveFeedbackEventHandler(DragSource_GiveFeedback);
and here is the feedback function:
private void DragSource_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
e.UseDefaultCursors = false;
}

Move Item from one cell to another

I have a tableLayoutPanel with 16 cells. 15 of the cells have controls. I want to be able to move the controls from one cell to another at runtime.
I have used
private void button15_Click(object sender, EventArgs e)
{
tableLayoutPanel1.Controls.Remove(button15);
tableLayoutPanel1.Controls.Add(button15, 3, 3);
}
This works well but i want to know if there is any better way to do this???
In Winforms, you can only move a control inside its parent (of course there are some exceptions to some controls which in fact don't have any Parent). So the idea here is if you want to move a control of your TableLayoutPanel, you have to set its Parent to your Form of another container when mouse is held down, when moving, the position of the control is in the new parent, after mouse is released, we have to set the Parent of the control to the TableLayoutPanel back, of course we have to find the drop-down cell position and use SetCellPosition method to position the control on the TableLayoutPanel, here is the demo code for you (works great), I use 2 Buttons in this demo, you can replace them with any control you want:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
//This will prevent flicker
typeof(TableLayoutPanel).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(tableLayoutPanel1, true, null);
}
Point downPoint;
bool moved;
//This is used to store the CellBounds together with the Cell position
//so that we can find the Cell position later (after releasing mouse).
Dictionary<TableLayoutPanelCellPosition, Rectangle> dict = new Dictionary<TableLayoutPanelCellPosition, Rectangle>();
//MouseDown event handler for all your controls (on the tableLayoutPanel1)
private void Buttons_MouseDown(object sender, MouseEventArgs e) {
Control button = sender as Control;
button.Parent = this;
button.BringToFront();
downPoint = e.Location;
}
//MouseMove event handler for all your controls (on the tableLayoutPanel1)
private void Buttons_MouseMove(object sender, MouseEventArgs e) {
Control button = sender as Control;
if (e.Button == MouseButtons.Left) {
button.Left += e.X - downPoint.X;
button.Top += e.Y - downPoint.Y;
moved = true;
tableLayoutPanel1.Invalidate();
}
}
//MouseUp event handler for all your controls (on the tableLayoutPanel1)
private void Buttons_MouseUp(object sender, MouseEventArgs e) {
Control button = sender as Control;
if (moved) {
SetControl(button, e.Location);
button.Parent = tableLayoutPanel1;
moved = false;
}
}
//This is used to set the control on the tableLayoutPanel after releasing mouse
private void SetControl(Control c, Point position) {
Point localPoint = tableLayoutPanel1.PointToClient(c.PointToScreen(position));
var keyValue = dict.FirstOrDefault(e => e.Value.Contains(localPoint));
if (!keyValue.Equals(default(KeyValuePair<TableLayoutPanelCellPosition, Rectangle>))) {
tableLayoutPanel1.SetCellPosition(c, keyValue.Key);
}
}
//CellPaint event handler for your tableLayoutPanel1
private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) {
dict[new TableLayoutPanelCellPosition(e.Column, e.Row)] = e.CellBounds;
if (moved) {
if (e.CellBounds.Contains(tableLayoutPanel1.PointToClient(MousePosition))) {
e.Graphics.FillRectangle(Brushes.Yellow, e.CellBounds);
}
}
}
}
Remove Lock & set dock to none and move!

Moving a control within another control's visible area

I have a PictureBox that is inside a TabPage, and of course this TabPage is part of a TabView and this TabView is inside a Form. I want users be able to move this picture box within the tab page. For this I am using the MouseDown, MouseMove and MouseUp events of the picture box:
private void pictureBoxPackageView_MouseDown(object sender, MouseEventArgs e)
{
if (!_mapPackageIsMoving)
{
_mapPackageIsMoving = true;
}
}
private void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e)
{
if(_mapPackageIsMoving)
{
pictureBoxPackageView.Location = MousePosition; //This is not exact at all!
return;
}
//Some other code for some other stuff when picturebox is not moving...
}
private void pictureBoxPackageView_MouseUp(object sender, MouseEventArgs e)
{
if (_mapPackageIsMoving)
{
_mapPackageIsMoving = false; //Mouse button is up, end moving!
return;
}
}
But my problem lies in the MouseMove event. As soon as I move mouse after button down, the picture box jumps out of tab page's visible area.
I need to know how to handle the move only within the rectangle of the tab page, and if picture box is being dragged out of tab view's visible area, it shouldn't move anymore unless user brings the mouse inside the tab view's visible rectangle.
Any helps/tips will be appriciated!
You need a variable to hold the original position of the PictureBox:
Modified from a HansPassant answer:
private Point start = Point.Empty;
void pictureBoxPackageView_MouseUp(object sender, MouseEventArgs e) {
_mapPackageIsMoving = false;
}
void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e) {
if (_mapPackageIsMoving) {
pictureBoxPackageView.Location = new Point(
pictureBoxPackageView.Left + (e.X - start.X),
pictureBoxPackageView.Top + (e.Y - start.Y));
}
}
void pictureBoxPackageView_MouseDown(object sender, MouseEventArgs e) {
start = e.Location;
_mapPackageIsMoving = true;
}

How to draw points on a panel with an image

I have a panel named dPanel. I set the the backgroundImage of that panel with an image named dImage.Now I want to draw points on the panel, in other words I want to color the panel by using mouse.I want to be able to save the drawing and the image together later. My codes do this but the picture lights up during drawing and its very slow.Here is my code:
private void drawP_MouseDown(object sender, MouseEventArgs e)
{
if (!drawbool)
{
dStartPoint = e.Location;
drawbool = true;
}
drawP.Invalidate();
}
private void drawP_MouseMove(object sender, MouseEventArgs e)
{
if (drawbool)
{
dStartPoint = e.Location;
drawP.Invalidate();
}
}
private void drawP_MouseUp(object sender, MouseEventArgs e)
{
if (drawbool)
{
drawbool = false;
}
}
private void drawP_Paint_1(object sender, PaintEventArgs e)
{
if (drawbool)
{
int dStartX = dStartPoint.X;
int dStartY = dStartPoint.Y;
e.Graphics.DrawEllipse(dP, dStartX, dStartY, 2, 2);
Bitmap dPPB = new Bitmap(drawP.Width, drawP.Height);
drawP.DrawToBitmap(dPPB, new Rectangle(0, 0, drawP.Width, drawP.Height));
drawP.BackgroundImage = (Image)dPPB;
}
}
Whani's the solution? Thanks in advance.
Check out this great example about drawing on panel: Painting on a Panel.
Later you can save your drawing just by invoking panel's method DrawToBitmap.

How do I add a mouse over tooltip to an Image using .DrawImage()

Hey all, I am not sure if this is possible, but I am trying to dynamically add a tooltip to an image using the Graphics method - DrawImage. I dont see any properties or events for when the image is moused over or anything so I don't know where to begin. I am using WinForms (in C# - .NET 3.5). Any ideas or suggestions would be appreciated. Thanks.
I would guess that you have some sort of UserControl and you call DrawImage() in the OnPaint method.
Given that, your tooltip will have to controlled explicitly. Basically, create a Tooltip on your Form, give that to your control via a property, show the tooltip when your control received a MouseHover event and hide the tooltip when you receive a MouseLeave event.
Something like this:
public partial class UserControl1 : UserControl
{
public UserControl1() {
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
// draw image here
}
public ToolTip ToolTip { get; set; }
protected override void OnMouseLeave(EventArgs e) {
base.OnMouseLeave(e);
if (this.ToolTip != null)
this.ToolTip.Hide(this);
}
protected override void OnMouseHover(EventArgs e) {
base.OnMouseHover(e);
if (this.ToolTip == null)
return;
Point pt = this.PointToClient(Cursor.Position);
String msg = this.CalculateMsgAt(pt);
if (String.IsNullOrEmpty(msg))
return;
pt.Y += 20;
this.ToolTip.Show(msg, this, pt);
}
private string CalculateMsgAt(Point pt) {
// Calculate the message that should be shown
// when the mouse is at thegiven point
return "This is a tooltip";
}
}
Remember, you have to store bounds of the Image that you are drawing
and in the mouseMove event check if the location of current Mouse cursor at that region, then display ToolTip else hide it.
ToolTip t;
private void Form1_Load(object sender, EventArgs e)
{
t = new ToolTip(); //tooltip to control on which you are drawing your Image
}
Rectangle rect; //to store the bounds of your Image
private void Panel1_Paint(object sender, PaintEventArgs e)
{
rect =new Rectangle(50,50,200,200); // setting bounds to rect to draw image
e.Graphics.DrawImage(yourImage,rect); //draw your Image
}
private void Panel1_MouseMove(object sender, MouseEventArgs e)
{
if (rect.Contains(e.Location)) //checking cursor Location if inside the rect
{
t.SetToolTip(Panel1, "Hello");//setting tooltip to Panel1
}
else
{
t.Hide(Panel1); //hiding tooltip if the cursor outside the rect
}
}

Categories

Resources