Hi! I had created a picture box and a button. WhenI click on the button one circle and one rectangle will be drawn on the picture box. I would like to implement drag and drop effect on thepicture box so thatboth circle and rectangle can be move freely in picture box area. Does anyone know how to implement it? Because I don't have any idea where to start. Thanks in advance for the help.
Here is my code on drawing that 2 shapes in picture box:-
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace test_drag
{
public partial class Window1
{
private Graphics g;
private System.Drawing.Pen pen1;
Bitmap bitmap;
public Window1()
{
this.InitializeComponent();
bitmap = new Bitmap(pictureBox.Width,pictureBox.Height);
g = Graphics.FromImage(bitmap);
// Insert code required on object creation below this point.
}
private void create_Click(object sender, RoutedEventArgs e)
{
pen1 = new System.Drawing.Pen(System.Drawing.Color.Blue, 1);
g.DrawRectangle(pen1, 10, 10, 50, 50);
g.DrawEllipse(pen1, 10, 80, 30, 30);
pen1.Dispose();
using (Graphics h = pictureBox.CreateGraphics())
{
h.DrawImageUnscaled(bitmap, 0, 0, pictureBox.Width, pictureBox.Height);
h.Dispose();
}
}
}
} | | Yuen Li Friday, March 07, 2008 3:03 AM | You'll need to deal with three mouse events in your picture box: MouseDown, MouseMove, MouseUp.
In MouseDown you'll need to set a flag to indicate to MouseMove that you are now moving the objects (otherwise any movement will run the MouseMove code). You'll also need to store the point at which the mouse was when it was pressed down.
In MouseMove, if the flag is set, you'll have to erase the current ellipse/rectangle, calculate the current offset from the initial mouse location when it was pressed down (the point you stored above), offset the rectangle/ellipse by that amount and redraw them. You will need to remember where you drew the items last, and where you want to draw them now (last for erasing, now for redrawing).
In MouseUp you need to clear the flag that says the mouse is down so MouseMove stops running.
Now go try and see what you can come up with. If you have more specific questions, come back, show us the code and the problem you are having.
| | Ron.Whittle Friday, March 07, 2008 3:27 AM | You'll need to deal with three mouse events in your picture box: MouseDown, MouseMove, MouseUp.
In MouseDown you'll need to set a flag to indicate to MouseMove that you are now moving the objects (otherwise any movement will run the MouseMove code). You'll also need to store the point at which the mouse was when it was pressed down.
In MouseMove, if the flag is set, you'll have to erase the current ellipse/rectangle, calculate the current offset from the initial mouse location when it was pressed down (the point you stored above), offset the rectangle/ellipse by that amount and redraw them. You will need to remember where you drew the items last, and where you want to draw them now (last for erasing, now for redrawing).
In MouseUp you need to clear the flag that says the mouse is down so MouseMove stops running.
Now go try and see what you can come up with. If you have more specific questions, come back, show us the code and the problem you are having.
| | Ron.Whittle Friday, March 07, 2008 3:27 AM | Hi! I understand that to implement these 3 events handler i need to overwrite my paint function. But when i overwrite it error occurs because the picture box that used was embeded in windows host forms in xaml file. Belows are my code, could anyone help me to solve it? Thanks.
XAML code: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wf="clr-namespace ystem.Windows.Forms;assembly=System.Windows.Forms" xmlns="http://schemas.microsoft.com/expression/blend/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="test_drag.Window1" x:Name="Window" Title="Window1" Width="516" Height="694" >
<Grid x:Name="LayoutRoot"> <WindowsFormsHost x:Name="host1" Background="#FFE1E0E0" AllowDrop="True" Margin="0,0,92,0" VerticalAlignment="Top" Height="312" > <wf ictureBox x:Name="pictureBox" Width="300" Height="300" Paint="pictureBox_Paint" /> </WindowsFormsHost> </Grid> </Window>
C# coding: using System; using System.IO; using System.Net; using System.Windows; using System.Windows.Controls; using System.Collections.Generic; using System.Windows.Data; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Navigation; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Documents; using System.Windows.Markup;
namespace test_drag { public partial class Window1 { Font font; public Window1() { this.InitializeComponent(); font = new Font("Arial", 10); // Insert code required on object creation below this point.
} private void pictureBox_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { e.Graphics.DrawString("This text is render", font, System.Drawing.Brushes.Red, 0, 0);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
base.OnPaint(e); e.Graphics.DrawString("this overrride", font, System.Drawing.Brushes.Blue, 5, 5); } } }
| | Yuen Li Saturday, March 08, 2008 3:44 PM |
|