runtime Movable rectangle windows store app - c#

I am trying to create runt time rectangle which will move on dragging by mouse. For some reason code is not working.
private void Rectangle_Click(object sender, RoutedEventArgs e)
{
var rec = new Rectangle();
rec.Height = 100;
rec.Width = 100;
rec.Fill = new SolidColorBrush(Colors.Violet);
rec.ManipulationDelta += rec_ManipulationDelta;
board.Children.Add(rec);
}
void rec_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
dragTranslation.X += e.Delta.Translation.X;
dragTranslation.Y += e.Delta.Translation.Y;
dragTranslation = new TranslateTransform();
this.RenderTransform = this.dragTranslation;
}

The problem's when assigning the transforms. Try
private void Rectangle_Click(object sender, RoutedEventArgs e)
{
var rec = new Rectangle();
rec.Height = 100;
rec.Width = 100;
rec.Fill = new SolidColorBrush(Colors.Violet);
rec.ManipulationMode=ManipulationModes.All;
rec.ManipulationDelta += rec_ManipulationDelta;
rec.RenderTransform=new TranslateTransform(); // Create new TranslateTransform and assign to the rectangle
board.Children.Add(rec);
}
void rec_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
Rectangle recSender = (Rectangle) sender; // Get the Rectangle
TranslateTransform ttSender = recSender.RenderTransform as TranslateTransform; // Get the Rectangle's RenderTransform (which is a TranslateTransform)
ttSender.X += e.Delta.Translation.X;
ttSender.Y += e.Delta.Translation.Y;
}

Related

How to replace TouchPoint on mouse click

Please to say me, how can I replace command TouchPoint similarly on mouse click? I add my code which I must change:
private Dictionary<int, UIElement> myEllipses =
new Dictionary<int, UIElement>();
private void cnv_MouseDown(object sender, MouseButtonEventArgs e)
{
Ellipse ellipse = new Ellipse();
ellipse.Width = 30;
ellipse.Height = 30;
ellipse.Stroke = Brushes.White;
ellipse.Fill = Brushes.Gold;
TouchPoint tp = e.GetPosition(cnv);
Canvas.SetTop(ellipse, tp.Bounds.Top);
Canvas.SetLeft(ellipse, tp.Bounds.Left);
myEllipses[e.TouchDevice.Id] = ellipse;
cnv.Children.Add(ellipse);
}
Try the GetPosition() method to get the point where mouse was clicked. Try this :
private void cnv_MouseDown(object sender, MouseButtonEventArgs e)
{
Point clickedPt = e.GetPosition(this);
var xPos = clickedPt.X;
var yPos = clickedPt.Y;
}

Referencing multiple instantiated pictureboxes

I'm very new to C# but trying to learn so bear with me if my syntax isn't accurate. I am able to create a picturebox with a button and it appears on screen. I can then move it around the screen just fine with a mouse down / mouse move function. I then hit the button to instantiate another picturebox to be created and can move that one around as well, but when I try to move the first picturebox the second one moves instead and goes insane. Is there a way to reference or tag the boxes on instantiation so that when I click on any of them I can move them around the screen?
public partial class Form1 : Form
{
Point MP;
private static Control PB;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int picSizeX = Properties.Resources.police.Width / 3;
int picSizeY = Properties.Resources.police.Height / 3;
PictureBox pb = new PictureBox();
pb.Location = new Point(100, 100);
pb.Size = new Size(picSizeX, picSizeY);
pb.Image = new Bitmap(Properties.Resources.police);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pb);
pb.Tag = "veh";
PB = pb;
pb.MouseDown += Pb_MouseDown;
pb.MouseMove += Pb_MouseMove;
pb.MouseHover += Pb_MouseHover;
}
private void Pb_MouseHover(object sender, EventArgs e)
{
PB.MouseHover += PB_MouseHover;
}
private void PB_MouseHover(object sender, EventArgs e)
{
}
private void Pb_MouseDown(object sender, MouseEventArgs e)
{
MP = e.Location;
}
private void Pb_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
PB.Left = e.X + PB.Left - MP.X;
PB.Top = e.Y + PB.Top - MP.Y;
}
}
}
Actually there is no need to have Control at class level.
In the event method there is a parameter called object sender that contains a reference to the control/object that raised the event.
Point MP;
//private Control PB; //commented out as it is not required
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int picSizeX = Properties.Resources.police.Width / 3;
int picSizeY = Properties.Resources.police.Height / 3;
PictureBox pb = new PictureBox();
pb.Location = new Point(100, 100);
pb.Size = new Size(picSizeX, picSizeY);
pb.Image = new Bitmap(Properties.Resources.police);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pb);
pb.Tag = "veh";
//PB = pb;
pb.MouseDown += Pb_MouseDown;
pb.MouseMove += Pb_MouseMove;
pb.MouseHover += Pb_MouseHover;
}
private void Pb_MouseHover(object sender, EventArgs e)
{
Control pbObj = sender as PictureBox; //sender refers to control that raised the event
pbObj.MouseHover += PB_MouseHover;
}
private void PB_MouseHover(object sender, EventArgs e)
{
}
private void Pb_MouseDown(object sender, MouseEventArgs e)
{
MP = e.Location;
}
private void Pb_MouseMove(object sender, MouseEventArgs e)
{
Control pbObj = sender as PictureBox; //sender refers to control that raised the event
if (e.Button == MouseButtons.Left)
{
pbObj.Left = e.X + pbObj.Left - MP.X;
pbObj.Top = e.Y + pbObj.Top - MP.Y;
}
}

What is wrong with my Rectangle drawing coordinates?

I'm trying to use the following code to allow the user to draw a rectangle around something on the screen, to select that area.
public partial class MainWindow : Window
{
public enum DrawMode
{
Move,
Draw
}
private DrawMode _drawmode;
private Point _startPoint;
private Rectangle _rectangle;
public MainWindow()
{
_rectangle = new Rectangle();
_rectangle.Stroke = new SolidColorBrush(Colors.Black);
_rectangle.StrokeThickness = 1;
InitializeComponent();
}
private void MapImageOnMouseDown(object sender, MouseButtonEventArgs e)
{
_drawmode = DrawMode.Draw;
_startPoint = e.GetPosition(DrawCanvas);
}
private void MapImageOnMouseUp(object sender, MouseButtonEventArgs e)
{
_drawmode = DrawMode.Move;
}
private void MapImageOnMouseMove(object sender, MouseEventArgs e)
{
if (_drawmode == DrawMode.Draw)
{
DrawCanvas.Children.Remove(_rectangle);
var endPoint = e.GetPosition(DrawCanvas);
var width = Math.Abs(endPoint.X - _startPoint.X);
var height = Math.Abs(endPoint.Y - _startPoint.Y);
_rectangle.Width = width;
_rectangle.Height = height;
DrawCanvas.Children.Add(_rectangle);
Canvas.SetTop(_rectangle, _startPoint.X);
Canvas.SetLeft(_rectangle, _startPoint.Y);
}
}
}
However, although when I mousedown, the top left of the rectangle is nowhere near the point that I mousedown at. Are there different coordinate systems or something?
You have confused X and Y (or Left and Top). Change
Canvas.SetTop(_rectangle, _startPoint.X);
Canvas.SetLeft(_rectangle, _startPoint.Y);
to
Canvas.SetLeft(_rectangle, _startPoint.X);
Canvas.SetTop(_rectangle, _startPoint.Y);
It is also not necessary to remove and add the Rectangle each time its position changes:
public MainWindow()
{
InitializeComponent();
_rectangle = new Rectangle
{
Stroke = Brushes.Black,
StrokeThickness = 1
};
DrawCanvas.Children.Add(_rectangle); // add it once
}
private void MapImageOnMouseMove(object sender, MouseEventArgs e)
{
if (_drawmode == DrawMode.Draw)
{
var endPoint = e.GetPosition(DrawCanvas);
_rectangle.Width = Math.Abs(endPoint.X - _startPoint.X);
_rectangle.Height = Math.Abs(endPoint.Y - _startPoint.Y);
Canvas.SetLeft(_rectangle, Math.Min(_startPoint.X, endPoint.X));
Canvas.SetTop(_rectangle, Math.Min(_startPoint.Y, endPoint.Y));
}
}
Point startPoint = new Point(0, 0);
Point endPoint = new Point(1, 1);
bool mousePress = false;
control.MouseDown += (s, e) =>
{
startPoint = new Point(e.X,e.Y);
mousePress = true;
};
control.MouseUp += (s, e) =>
{
mousePress = false;
};
control.MouseMove += (s, e) =>
{
if (mousePress)
{
endPoint = new Point(e.X, e.Y);
control.Invalidate();
}
};
control.Paint += (s, e) =>
{
Graphics g = e.Graphics;
g.DrawRectangle(Pens.Black, new Rectangle(startPoint.X, startPoint.Y,endPoint.X-startPoint.X, endPoint.Y-startPoint.Y));
};
I have been doing drawing in the past and this worked for me for rectangles, dont know how it translates to wpf tho

Rectangle can't be drawn on the panel by mouse dragging runtime

I am trying to draw a rectangle on a panel, but nothing is drawn. Below is the code to show how I draw a rectangle on the panel. In my code SetSelectionRect() is used to set the rectangle to be drawn. For these I use following methods.
private void Panel_MouseDown(object sender, MouseEventArgs e)
{
Point point = new Point();
this.mouseDown = true;
this.Panel.SendToBack();
point = this.Panel.PointToClient(Cursor.Position);
point.X = e.X;
point.Y = e.Y;
this.selectionStart.X = point.X;
this.selectionStart.Y = point.Y;
}
private void Panel_MouseMove(object sender, MouseEventArgs e)
{
if (!this.mouseDown)
{
return;
}
else
{
this.mouseMove = true;
Point point = this.Panel.PointToClient(Cursor.Position);
point.X = e.X;
point.Y = e.Y;
this.selectionEnd.X = point.X;
this.selectionEnd.Y = point.Y;
this.SetSelectionRect();
////this.Panel.Invalidate();
////this.Invalidate();
}
}
private void Panel_MouseUp(object sender, MouseEventArgs e)
{
SetSelectionRect();
this.GetSelectedControls();
this.mouseDown = false;
this.mouseMove = false;
////this.Panel.Invalidate();
////this.Invalidate();
this.Panel.Refresh();
}
private void Panel_Paint(object sender, PaintEventArgs e)
{
////base.OnPaint(e); drawRect = true when RectangleToolStripMenuItem is Clicked.
if (this.drawRect)
{
using (Pen pen = new Pen(Color.Black, 1F))
{
this.rectangle = new RectangleShape();
this.Panel.SendToBack();
this.shapeContainer1.Shapes.Add(this.rectangle);
this.rectangle.Location = this.selection.Location;
this.rectangle.Size = this.selection.Size;
this.rectangle.Name = "rectShape";
this.shapeContainer1.Size = this.Panel.Size;
this.shapeContainer1.Location = this.Panel.Location;
this.rectangle.Enabled = false;
this.rectangle.MouseClick += new MouseEventHandler(this.mouseclick);
this.rectangle.MouseMove += new MouseEventHandler(this.mouseMove);
this.rectangle.MouseDown += new MouseEventHandler(this.mouseDown);
this.rectangle.MouseUp += new MouseEventHandler(this.mouseUp);
this.drawRect = false;
}
}
}
protected override void OnPaint (PaintEventArgs e)
{
base.OnPaint(e);
}
What's wrong with the code?
The problem is base.OnPaint(e); is raising an Paint event, where you placed base.OnPaint(e);, so it calling itself again and again.
private void panel_Paint(object sender, PaintEventArgs e)
{
//base.OnPaint(e); // remove it from here
// something to do.
}
base.OnPaint(e); should be called in overriden method:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}

WinForm - draw resizing frame using a single-pixel border

In a Windows Form with a Resizing Frame, the frame border draws with a raised 3-D look. I'd like it to draw with a flat single pixel border in a color of my choosing.
Is this possible without having to owner draw the whole form?
You could try something like this:
Point lastPoint = Point.Empty;
Panel leftResizer = new Panel();
leftResizer.Cursor = System.Windows.Forms.Cursors.SizeWE;
leftResizer.Dock = System.Windows.Forms.DockStyle.Left;
leftResizer.Size = new System.Drawing.Size(1, 100);
leftResizer.MouseDown += delegate(object sender, MouseEventArgs e) {
lastPoint = leftResizer.PointToScreen(e.Location);
leftResizer.Capture = true;
}
leftResizer.MouseMove += delegate(object sender, MouseEventArgs e) {
if (lastPoint != Point.Empty) {
Point newPoint = leftResizer.PointToScreen(e.Location);
Location = new Point(Location.X + (newPoint.X - lastPoint.X), Location.Y);
Width = Math.Max(MinimumSize.Width, Width - (newPoint.X - lastPoint.X));
lastPoint = newPoint;
}
}
leftResizer.MouseUp += delegate (object sender, MouseEventArgs e) {
lastPoint = Point.Empty;
leftResizer.Capture = false;
}
form.BorderStyle = BorderStyle.None;
form.Add(leftResizer);

Categories