ControlPaint.DrawReversibleLine issue - c#

In a System.Windows.Forms.Panel (in C# and .NET 4.5.1) I have to follow the cursor position with two crossing perpendicular lines.
Using ControlPaint.DrawReversibleLine somtimes parts or whole old lines persist.
This happen in a random manner and applying or not the style:
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true)
as suggested in MSDN.
Temporarily solved using a cross cursor but I have to do it.
If anyone can give me hint...
Thank you
D.R.

Forget DrawReversibleLine!
This will do what you told us you want to do:
Point mouse = Point.Empty;
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
mouse = e.Location;
panel1.Invalidate();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (mouse != Point.Empty)
{
e.Graphics.DrawLine(Pens.Blue, 0, mouse.Y, panel1.ClientSize.Width, mouse.Y );
e.Graphics.DrawLine(Pens.Blue, mouse.X, 0, mouse.X, panel1.ClientSize.Height );
}
}
private void panel1_MouseLeave(object sender, EventArgs e)
{
mouse = Point.Empty;
panel1.Invalidate();
}
But it will not do what you didn't tell us.
Note: You will need to update anthing else on drawn that Panel - I hope there is nothing, otherwise it needs to be redrawn from the Paint event..

Related

C# winforms. Drag events

I need to run code when I dismiss(end) dragging click. How this event is called?
Here is main example, please find the below screenshot for more information:
I made that I can drag the car on other picture boxes like below this:
Repeat again - I need to know what EVENT is then you dismiss to drag on picture box?
There is no event for when the drag is released on a control, but you don't really need one. This is what I did to simulate what (I think) you're looking for. I used code courtesy of this stackoverflow answer
private Point? _mouseLocation;
private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.MouseDown += this.pictureBox1_MouseDown;
this.pictureBox1.MouseUp += this.pictureBox1_MouseUp;
this.pictureBox1.MouseMove += this.pictureBox1_MouseMove;
}
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if ( this._mouseLocation.HasValue)
{
this.pictureBox1.Left = e.X + this.pictureBox1.Left - this._mouseLocation.Value.X;
this.pictureBox1.Top = e.Y + this.pictureBox1.Top - this._mouseLocation.Value.Y;
}
}
void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
this._mouseLocation = null;
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//Check if you've left-clicked if you want
this._mouseLocation = e.Location;
}
Setting the mouse location to null with this._mouseLocation = null; is your "drag released" code.
I guess you talking about DragDrop. You can find example here: How to: Enable Drag-and-Drop Operations with the Windows Forms RichTextBox Control

How to prevent panel content from being erased upon scroll

I have a question regarding how I can prevent some content drawn on a panel control from being erased when a scroll action brings it out of view.
What I am trying to do is create a 2D tile-map editor. Whenever a mouse click event happens on the panel, a tile should get drawn onto the panel. I have this working fine. But if I place the object on the panel and scroll to one side, and scroll back, the object I had placed is gone.
I have done some research and I have seen suggestions on implementing the paint event. The problem is that I do not understand what to implement here. I think most of my struggles is coming from not fully understanding the Graphics object.
Here is some of my code:
private void canvas_MouseClick(object sender, MouseEventArgs e)
{
Graphics g = canvas.CreateGraphics();
float x1 = CommonUtils.GetClosestXTile(e.X);
float y1 = CommonUtils.GetClosestYTile(e.Y);
if (currentTile != null)
{
g.DrawImage(currentTile, x1, y1);
me.AddTile((int)currX, (int)currY, (int)x1, (int)y1, "C:\\DemoAssets\\tileb.png");
}
else
{
// dont do anything
}
g.Dispose();
}
private void canvas_Paint(object sender, PaintEventArgs e)
{
// update here?
}
To hold multiple Tiles, you'd need a List to hold each clicked location along with its associated tile:
List<Tuple<Image, PointF>> Tiles = new List<Tuple<Image, PointF>>();
private void canvas_MouseClick(object sender, MouseEventArgs e)
{
if (currentTile != null)
{
float x1 = CommonUtils.GetClosestXTile(e.X);
float y1 = CommonUtils.GetClosestYTile(e.Y);
Tiles.Add(new Tuple<Image, PointF>(currentTile, new PointF(x1, y1)));
canvas.Refresh();
me.AddTile((int)currX, (int)currY, (int)x1, (int)y1, "C:\\DemoAssets\\tileb.png");
}
}
private void canvas_Paint(object sender, PaintEventArgs e)
{
foreach (Tuple<Image, PointF> tile in Tiles)
{
e.Graphics.DrawImage(tile.Item1, tile.Item2);
}
}

Unable to set the panel location to the current mouse position inside splitContainer?

I have panel1 in my form, i set the visible property to panel1.Visible=false; I want to show this panel wherever i click on the screen.
I need to grab the current mouse location and then display the panel1 where the top-left corner must be in the same point as the mouse cursor !
Sorry for being so beginner but i really stuck on how to do it.
Code that i tried :
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
panel1.Location = e.Location;
panel1.Show();
}
It might this will be your guide to your task, just use the .PointToScreen and .GetCellDisplayRectangle Method
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == -1) return;
var cellRectangle = dataGridView1.PointToScreen(
dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location);
panel1.Location = new Point(cellRectangle.X + 50, cellRectangle.Y - 175);
panel1.Show();
}
As far I can recognize your problem you should use PointToScreen function - more on this here

Find the mouse location over a bitmap control and display a rectangle in c#

I want to find the exact mouse location of the panel's display rectangle. I have a panel and assigned a bitmap image on it. I searched long time and found control over mouse location option but it won't fit. Anyone help me in this.
You can use the MouseMove event and exploit the MouseEventArgs:
//panel.MouseMove += new MouseEventHandler(panel_MouseMove);
void panel_MouseMove(object sender, MouseEventArgs e)
{
Point myLocation = e.Location;
//Or e.X and e.Y
}
You can use MouseMove event:
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
Text = String.Format("({0};{1})", e.X, e.Y);
}
You can use e.Location or splitted values e.X and e.Y.

Drawing and deleting old drawings

I'm new in this forum, and my english is not perfect, so i want to excuse if my question isn't writen good.
I'm making a painting program with C# and all is perfect expect one problem.
When i'm drawing a line, or rectangle or elipse, when the mouseMove event is called the old shapes is drawed too.
How to draw a shape with mouseMove event and the old shapes to not been drawed.
Here is part of my code for more clarification.
//---Variables declared by the Prgrammer---//
//'parent' is variable that take the MdiParent
//'mouseIsDown' is boolean variable
//'startPoint' and 'endPoint' are Point Varables
//'pen' is Pen variable that is configured eralier
//'graphic' is pictureBox in the same form.
private void pbx_MouseDown(object sender, MouseEventArgs e)
{
if (parent.btnLine.Checked)
{
mouseIsDown = true;
startPoint = new Point(e.X, e.Y);
}
}
private void pbx_MouseMove(object sender, MouseEventArgs e)
{
if (mouseIsDown == true && parent.btnLine.Checked)
{
pen = new Pen(parent.btnPreview.BackColor, 12);
endPoint = new Point(e.X, e.Y);
graphic.DrawLine(pen, startPoint, endPoint);
}
}
private void pbx_MouseUp(object sender, MouseEventArgs e)
{
mouseIsDown = false;
}
Please help me. This is a big problem for me.
Thanks.
Please help me, i'm waiting 2 days.
When i'm making a new drawing to the graphic object, the old graphic is deleted.
I try the graphic.Save() method, but has not give me right result.
How to make the drawings to saty when i'm making another graphic?
As you're using picturebox you can use Invalidate() method.

Categories

Resources