I am trying to move a rectangle when user drags it. My code is:
private void Grid_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
{
Rectangle r = (sender) as Rectangle;
r.RenderTransform = new CompositeTransform { TranslateX = e.DeltaManipulation.Translation.X };
}
Above code doesn't work because it should be like this:
r.RenderTransform = new CompositeTransform { TranslateX += e.DeltaManipulation.Translation.X };
"+" sign makes it work but above syntax give an error. It works fine when I add Render transform using xaml but I want to do it in c#. Any suggestion?
Just putting the solution proposed in the comments into an answer:
They way you did it was to assign a new CompositeTransform on every call of the event handler. This is not what you want. You want to manipulate an existing CompositeTransform instead.
In order to achieve this, you have to manually assign the CompositeTransform once outside your eventhandler:
r.RenderTransform = new CompositeTransform();
After this you can use the following in your event handler:
((CompositeTransform)r.RenderTransform).TranslateX += e.DeltaManipulation.Translation.X;
Related
I'm generating a list of image controls in WPF .
Image img = new Image();
img.Source = GetPic(value);
img.Tag = cst.GetIdDriverde();
img.ToolTip = dtID.Rows[j][0];
img.Width = 130;
img.Height = 130;
lst.Add(img);
I want to add an event to each control.
How can I do that?
I agree with Mighty and Omid but if you run stubborn, here's a lambda
img.MouseLeftButtonUp += (se, a) => {/*YOUR CODE*/};
You could attach an event like you do it for any other object.
For example a MouseLeftButtonDown event.
Image img = new Image();
img.MouseLeftButtonDown += Img_MouseLeftButtonDown;
private void Img_MouseLeftButtonDown(object sender, MouseButtonEventArgs eventArgs)
{
//your logic
}
Have a look at the Image Class for the available events.
Hint
This would work but you should not add images like this at all. The better way would be to use a ItemsControl and using a DataTemplate. If you want to change your code let me know if you need some help.
I am trying to draw images on a panel with a click event.I managed to do that,but I want to keep the generated images.After each click,the previously generated image disapears.How can I keep all the drawed images?
This is my code untill now:
private void drawdot(object sender,PaintEventArgs e)
{
Image dot = Image.FromFile("dot.png");
var points = this.PointToClient(new Point(Cursor.Position.X-20, Cursor.Position.Y-30));
e.Graphics.DrawImage(dot, points);
}
private void grid2_Paint(object sender, EventArgs e)
{
if(started==true)
{
var points = this.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));
coord2.Add(points.ToString());
clickuri2++;
test2_puncte.Text = "Testul 2 | Puncte: " + clickuri2;
//draw
grid2.Paint -= drawdot;
grid2.Paint += drawdot;
grid2.Invalidate();
}
}
Since you are invalidating the whole grid is redrawn, you should invalidate only the parts that you just redrawn. This is create a region and pass it to grid2.Invalidate as a parameter. Roughly it would look like something like this:
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddLines(points);
lvResults.Invalidate(new Region(path));
UPDATE:
You could also keep a list of all the clicked points and call the imagedraw method once for each new points, just add a new item to the list when clicking on the object and you should be good to go. I don't know if you could get into some race condition scenario where someone clicks while it's trying to draw on all points but it would be easily handled if that was the case
I have created 4 buttons dynamically and placed them horizontally using c# win forms.Now i want show a custom tooltip(actually its a borderless form) under each of the 4 buttons on mouse hover event.But how do i position my tooltip form under the buttons??
I have tried the code below but it does not work the desired way.
tooltip.Location = new System.Drawing.Point(b.Left, b.Top);
Where 'tooltip' is tooltip form object & 'b' is the dynamic button.Please advise with some code snippet.
private void B_MouseHover(object sender, EventArgs e)
{
var b = sender as Button;
//MessageBox.Show(Cursor.Position.ToString());
if(b!= null)
{
if (tooltip == null)
{
tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = new System.Drawing.Point(b.Left, b.Bottom);
tooltip.data(b.Tag.ToString());
tooltip.Show();
}
}
The way you named it is a bit misleading. As I understand, what you call a tooltip is just a Form. You need to consider 2 things
(1) Form.StartPosition must be set to FormStartPosition.Manual
(2) Form.Location must be in screen coordinates. Note that the Button.Location you are trying to use is in button's parent client coordinates. Control.PointToScreen has to be used for conversion.
In your case, it should be something like this
tooltip.StartPosition = FormStartPosition.Manual;
var topLeft = b.PointToScreen(new Point(0, 0));
tooltip.Location = new Point(topLeft.X, topLeft.Y + b.Height);
When you show the tooltip you can control its location, check show method overloads: https://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.show.aspx
I am trying to syncronize the scrolling of two splitcontainers within a splitpanel control. I have the code below:
Point mPrevPan1Pos = new Point();
Point mPrevPan2Pos = new Point();
void PanelPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (splitContainer1.Panel1.AutoScrollPosition != mPrevPan1Pos)
{
splitContainer1.Panel2.AutoScrollPosition = new System.Drawing.Point(-splitContainer1.Panel1.AutoScrollPosition.X, -splitContainer1.Panel1.AutoScrollPosition.Y);
mPrevPan1Pos = splitContainer1.Panel1.AutoScrollPosition;
}
else if (splitContainer1.Panel2.AutoScrollPosition != mPrevPan2Pos)
{
splitContainer1.Panel1.AutoScrollPosition = new System.Drawing.Point(-splitContainer1.Panel2.AutoScrollPosition.X, -splitContainer1.Panel2.AutoScrollPosition.Y);
mPrevPan2Pos = splitContainer1.Panel2.AutoScrollPosition;
}
}
However the AutoScrollPosition is always (0,0). I have AutoScroll enabled for both split containers. Why is this? What can I do to get the scroll position?
It looks like you copied the code from this answer: Scroll 2 panels at the same time
Did you wire up the events:
this.splitContainer1.Panel1.Paint += new PaintEventHandler(PanelPaint);
this.splitContainer1.Panel2.Paint += new PaintEventHandler(PanelPaint);
The picture below shows a chart in my project. As you can see there are two dotted crossing lines. I’m asked to make it to follow the mouse, but now only if I click on the chart it moves. I tried to use CursorPositionChanging but it didn’t work.
CursorEventHandler also is not shown in the command below:
this.chart1.CursorPositionChanging += new System.Windows.Forms.DataVisualization.Charting.Chart.CursorEventHandler(this.chart1_CursorPositionChanging);
do we need to add extra lib for that?
So I have two problems now:
1. Make the lines to follow the mouse
2. Missing CursorEventHandler
the project is window form application with C#
private void chData_MouseMove(object sender, MouseEventArgs e)
{
Point mousePoint = new Point(e.X, e.Y);
Chart.ChartAreas[0].CursorX.SetCursorPixelPosition(mousePoint, true);
Chart.ChartAreas[0].CursorY.SetCursorPixelPosition(mousePoint, true);
// ...
}
The chart supports a 'MouseMove' event which is fired each time the mouse is moved inside the chart. The MouseEventArgs contain the position of the mouse so u can move the dotted lines based on that data each time the event fires.
A more generalized form to synchronized all areas without any additional logic is as follows:
var mousePoint = new Point(e.X, e.Y);
var chart = (Chart)sender;
//foreach child
foreach (var ca in chart.ChartAreas)
{
ca.CursorX.SetCursorPixelPosition(mousePoint, true);
ca.CursorY.SetCursorPixelPosition(mousePoint, true);
}