I already working with manipulations over images on canvas. Now, I need to check, if image moved on canvas using fingers. How to do this?
void Window_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
Image photoToMove = e.OriginalSource as Image;
int photoIndex = photos.IndexOf(photoToMove);
System.Windows.Media.Matrix rectsMatrix = ((MatrixTransform)photoToMove.RenderTransform).Matrix;
rectsMatrix.RotateAt(e.DeltaManipulation.Rotation,
e.ManipulationOrigin.X,
e.ManipulationOrigin.Y);
rectsMatrix.ScaleAt(e.DeltaManipulation.Scale.X,
e.DeltaManipulation.Scale.X,
e.ManipulationOrigin.X,
e.ManipulationOrigin.Y);
rectsMatrix.Translate(e.DeltaManipulation.Translation.X,
e.DeltaManipulation.Translation.Y);
MatrixTransform newImageMTransform = new MatrixTransform(rectsMatrix);
photoToMove.RenderTransform = newImageMTransform;
Rect containingRect =
new Rect(((FrameworkElement)e.ManipulationContainer).RenderSize);
Rect shapeBounds =
photoToMove.RenderTransform.TransformBounds(
new Rect(photoToMove.RenderSize));
if (e.IsInertial && !containingRect.Contains(shapeBounds))
{
e.Complete();
}
if (e.DeltaManipulation.Translation.X > 0 || e.DeltaManipulation.Translation.Y > 0)
{
imageMoved = true;
}
else
{
imageMoved = false;
}
e.Handled = true;
}
At now, this code always says me "imageMoved == true".
Related
I have drag and drop control with adorner support. When dragging the list item from right to left the adorer window is not near the mouse pointer. Please anyone help me to place the adorner window near the mouse pointer. I have followed for creating the adorner window and displaying the adorner by using the below question
Please refer the question. I tried like this for placing the adorner window near the mouse pointer. But its not working for higher resolution window, I did not get any correct solution. Please suggestion your idea.
Behavior
private Window _dragdropWindow = null;
ListBoxItem draggedItem = null;
Win32Point w32Mouse = new Win32Point();
this.AssociatedObject.PreviewMouseMove += (sender, e) =>
{
if (e.LeftButton == MouseButtonState.Pressed && dataObject != null && !IsDragging)
{
var currentPoint = e.GetPosition(sender as UIElement);
if (Math.Abs(currentPoint.X - startingPoint.X) > 10 || (Math.Abs(currentPoint.Y - startingPoint.Y) > 10))
{
IsDragging = true;
if (draggedItem != null)
{
CreateDragDropWindow(draggedItem);
}
DragDrop.DoDragDrop(sender as ListBox, dataObject, DragDropEffects.Copy);
if (_dragdropWindow != null)
{
_dragdropWindow.Close();
_dragdropWindow = null;
}
}
}
};
this.AssociatedObject.PreviewMouseLeftButtonDown += (sender, e) =>
{
var listBoxItem = VisualHelper.FindParentOfType(e.OriginalSource as DependencyObject, typeof(ListBoxItem)) as ListBoxItem;
if (listBoxItem != null)
{
startingPoint = e.GetPosition(sender as UIElement);
dataObject = listBoxItem.DataContext as Details;
draggedItem = VisualHelper.FindParentOfType(e.OriginalSource as DependencyObject, typeof(ListBoxItem)) as ListBoxItem;
}
else
{
dataObject = null;
IsDragging = false;
}
};
this.AssociatedObject.PreviewGiveFeedback += (sender, e) =>
{
Win32Point w32Mouse = new Win32Point();
GetCursorPos(ref w32Mouse);
_dragdropWindow.Left = w32Mouse.X;
_dragdropWindow.Top = w32Mouse.Y;
};
private void CreateDragDropWindow(Visual dragElement)
{
this._dragdropWindow = new Window();
_dragdropWindow.WindowStyle = WindowStyle.None;
_dragdropWindow.AllowsTransparency = true;
_dragdropWindow.AllowDrop = false;
_dragdropWindow.Background = null;
_dragdropWindow.IsHitTestVisible = false;
_dragdropWindow.SizeToContent = SizeToContent.WidthAndHeight;
_dragdropWindow.Topmost = true;
_dragdropWindow.ShowInTaskbar = false;
Rectangle rectangle = new Rectangle();
rectangle.Width = ((FrameworkElement)dragElement).ActualWidth;
rectangle.Height = ((FrameworkElement)dragElement).ActualHeight;
rectangle.Fill = new VisualBrush(dragElement);
this._dragdropWindow.Content = rectangle;
this._dragdropWindow.Left = w32Mouse.X;
this._dragdropWindow.Top = w32Mouse.Y;
this._dragdropWindow.Show();
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(ref Win32Point pt);
[StructLayout(LayoutKind.Sequential)]
internal struct Win32Point
{
public Int32 X;
public Int32 Y;
};
When dragging the list item the adorner is not near the mouse pointer
I have been tasked with creating a somewhat hierarchical datagridview for my company. I heavily modified one from Syed Shanu that is posted here https://www.codeproject.com/Articles/848637/Nested-DataGridView-in-windows-forms-csharp. I'm almost done (data loads properly, etc.), however I cannot for the life of me figure out how to get the detail grid to move when I scroll. It's a drawn on rectangle and I'm looking for a way to somehow bind it to the master grid so it scrolls up and down with the regular grid. Any help would be appreciated. Here is the code that draws the rectangle:
private void masterDGVs_CellContentClick_Event(object sender, DataGridViewCellEventArgs e)
{
DataGridViewImageColumn cols = (DataGridViewImageColumn)MasterDGVs.Columns[0];
MasterDGVs.Rows[e.RowIndex].Cells[0].Value = Image.FromFile(#"expand.png");
if (e.ColumnIndex == gridColumnIndex)
{
if (ImageName == #"expand.png")
{
DetailDGVs.Visible = true;
ImageName = #"toggle.png";
MasterDGVs.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Image.FromFile(ImageName);
String FilterExpression = MasterDGVs.Rows[e.RowIndex].Cells[FilterColumnName].Value.ToString();
MasterDGVs.Controls.Add(DetailDGVs);
Rectangle DGVRectangle = MasterDGVs.GetCellDisplayRectangle(1, e.RowIndex, true);
DetailDGVs.Size = new Size(MasterDGVs.Width - 48, DetailDGVs.PreferredSize.Height - 16);
DetailDGVs.Location = new Point(DGVRectangle.X, DGVRectangle.Y + 20);
DataView detailView = new DataView(DetailGridDT);
detailView.RowFilter = FilterColumnName + " = '" + FilterExpression + "'";
foreach (DataGridViewRow row in DetailDGVs.Rows)
{
if (row.Cells[5].Value.ToString() == "Error")
{
row.Cells[5].Style.ForeColor = Color.DarkRed;
}
else if (row.Cells[5].Value.ToString() == "Processed and Complete")
{
row.Cells[5].Style.ForeColor = Color.Green;
}
else
{
row.Cells[5].Style.ForeColor = Color.Yellow;
}
}
}
else
{
ImageName = #"expand.png";
MasterDGVs.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Image.FromFile(ImageName);
DetailDGVs.Visible = false;
}
}
else
{
DetailDGVs.Visible = false;
}
}
I have sort of working by adding:
MasterDGVs.MouseWheel += new MouseEventHandler(DetailDGV_Scroll);
DetailDGVs.MouseWheel += new MouseEventHandler(MasterDGV_Scroll);
and
private void DetailDGV_Scroll(object sender, MouseEventArgs e)
{
int scale = e.Delta * SystemInformation.MouseWheelScrollLines / 5;
DetailDGVs.Top = DetailDGVs.Top + scale;
}
private void MasterDGV_Scroll(object sender, MouseEventArgs e)
{
int scale = e.Delta * SystemInformation.MouseWheelScrollDelta / 5;
MasterDGVs.Top = MasterDGVs.Top - scale;
}
I have a route/polygon named "BBB"(figure1,figure2) in my GMap .Net Windows form Application.I am drawing route/polygon from mouse and storing all latitudes and longitudes into List<PointLatLng>.What i want is set of Latitudes and Longitudes between two intersecting points(latitude and longitude).
Note:
I have all Latitudes and Longitudes of route/polygon "BBB"(figure1,figure2) as well as "CCC"(figure1,figure2).
please let me know if something is not clear.Is there any Library or Api ?
Code: Some Code for Idea
List<PointLatLng> ListOfDragLatLang = new List<PointLatLng>();
PointLatLng StartingLatLng = new PointLatLng();
PointLatLng EndingLatLng = new PointLatLng();
// polygons
GMapPolygon polygon;
readonly GMapOverlay top = new GMapOverlay();
internal readonly GMapOverlay objects = new GMapOverlay("objects");//for storing markers
internal readonly GMapOverlay routes = new GMapOverlay("routes");// for storing routes
internal readonly GMapOverlay polygons = new GMapOverlay("polygons");//for storing polygons
public bool IsErasorCursorVisible { get => _IsErasorCursorVisible; set => _IsErasorCursorVisible = value; }
public bool IsPencilCursorVisible { get => _IsPencilCursorVisible; set => _IsPencilCursorVisible = value; }
public bool IsNormalCursorVisible { get => _IsNormalCursorVisible; set => _IsNormalCursorVisible = value; }
private void MainMap_MouseUp(object sender, MouseEventArgs e)
{
PointLatLng OnMouse = MainMap.FromLocalToLatLng(e.X, e.Y);
lblLatitude.Text = OnMouse.Lat.ToString();
lblLongitude.Text = OnMouse.Lng.ToString();
if (IsPencilCursorVisible && IsDrawing)
{
EndingLatLng = MainMap.FromLocalToLatLng(e.X, e.Y);
ListOfDragLatLang.Add(StartingLatLng);
IsDragging = false;
IsDrawing = false;
MainMap.DragButton = MouseButtons.Right;
//polygon = new GMapPolygon(ListOfDragLatLang, txtZoneName.Text);
//polygon.LocalPoints.AddRange(ListOfPoints);
//polygon.Stroke = new Pen(Color.Black, 3);
//polygon.IsHitTestVisible = true;
//polygon.Stroke.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
//polygons.Polygons.Add(polygon);
//MainMap.UpdatePolygonLocalPosition(polygon);
lblTotalPolygonsAdded.Text = polygons.Polygons.Count.ToString();
for (int i = 0; i < ListOfDragLatLang.Count; i++)
{
AddPinPointToPolygon(ListOfDragLatLang[i], i, txtZoneName.Text);
}
RegenerateRoute(txtZoneName.Text);
GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(ListOfDragLatLang.Sum(c => c.Lat) / ListOfDragLatLang.Count, ListOfDragLatLang.Sum(c => c.Lng) / ListOfDragLatLang.Count), GMarkerGoogleType.orange_dot);
marker.ToolTip = new GMapBaloonToolTip(marker);
marker.ToolTipText = txtZoneName.Text;
marker.ToolTipMode = MarkerTooltipMode.Always;
marker.IsVisible = true;
marker.Tag = txtZoneName.Text;
objects.Markers.Add(marker);
MainMap.UpdateMarkerLocalPosition(marker);
MainMap.UpdatePolygonLocalPosition(polygon);
}
}
private void MainMap_MouseDown(object sender, MouseEventArgs e)
{
PointLatLng OnMouse = MainMap.FromLocalToLatLng(e.X, e.Y);
if (e.Button == MouseButtons.Left)
{
if (IsPencilCursorVisible && IsDrawing)
{
StartingLatLng = OnMouse;
ListOfDragLatLang.Add(StartingLatLng);
ListOfPoints.Add(new GPoint(e.X, e.Y));
IsDragging = true;
MainMap.DragButton = MouseButtons.Middle;
currentRoute = new GMapRoute(txtZoneName.Text);
currentRoute.Stroke = new Pen(Color.Black, 3);
currentRoute.IsHitTestVisible = true;
routes.Routes.Add(currentRoute);
MainMap.UpdateRouteLocalPosition(currentRoute);
//polygon = new GMapPolygon(ListOfDragLatLang,txtZoneName.Text);
//polygon.LocalPoints.AddRange(ListOfPoints);
//polygon.Stroke = new Pen(Color.Black, 3);
//polygon.IsHitTestVisible = true;
//polygon.Stroke.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
//polygons.Polygons.Add(polygon);
//MainMap.UpdatePolygonLocalPosition(polygon);
}
}
}
private void MainMap_MouseMove(object sender, MouseEventArgs e)
{
PointLatLng OnMouse = MainMap.FromLocalToLatLng(e.X, e.Y);
lblLatitude.Text = OnMouse.Lat.ToString();
lblLongitude.Text = OnMouse.Lng.ToString();
if (e.Button == MouseButtons.Left)
{
if (IsPencilCursorVisible && IsDrawing)
{
if (MainMap.IsMouseOverPolygon)
{
MainMap.Cursor = MainCursor;
}
else
{
MainMap.Cursor = PencilCursor;
}
IsDragging = true;
ListOfPoints.Add(new GPoint(e.X, e.Y));
ListOfDragLatLang.Add(OnMouse);
lblTotalLatLng.Text = ListOfDragLatLang.Count.ToString();
currentRoute.Points.Add(OnMouse);
MainMap.UpdateRouteLocalPosition(currentRoute);
//polygon.Points.Add(OnMouse);
//MainMap.UpdatePolygonLocalPosition(polygon);
}
else
{
PointLatLng pnew = MainMap.FromLocalToLatLng(e.X, e.Y);
if (CurentRectMarker == null)
{
return;
}
int? pIndex = (int?)CurentRectMarker.Tag;
if (pIndex.HasValue)
{
if (pIndex < currentRoute.Points.Count)
{
currentRoute.Points[pIndex.Value] = pnew;
MainMap.UpdateRouteLocalPosition(currentRoute);
}
//if (pIndex < polygon.Points.Count)
//{
// polygon.Points[pIndex.Value] = pnew;
// MainMap.UpdatePolygonLocalPosition(polygon);
//}
}
if (currentMarker.IsVisible)
{
currentMarker.Position = pnew;
}
CurentRectMarker.Position = pnew;
if (CurentRectMarker.InnerMarker != null)
{
CurentRectMarker.InnerMarker.Position = pnew;
}
}
MainMap.Refresh();
}
}
Figure 1
Figure 2
Question:How to get the Latitudes and Longitudes of yellow color edge in figure 2 between intersecting point "A" and "B"?
Assuming your List is in the correct order
Find the index of the coordinate at point A and B in your List and then create a new list using all points that are between the two indexes.
The idea here is that I redraw the combol "cell" so that it shows the block of colour and text. This is when form displays and it is about to show the dropdown:
After I have selected a colour it does weird:
Now it is all wrong. I have to hover the mouse over the control to render other bits. Just not working right.
My handler:
private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if(e.ColumnIndex == 0 && e.RowIndex > 0)
{
e.PaintBackground(e.ClipBounds, true);
e.PaintContent(e.ClipBounds);
Graphics g = e.Graphics;
Color c = Color.Empty;
string s = "";
Brush br = SystemBrushes.WindowText;
Brush brBack;
Rectangle rDraw;
rDraw = e.ClipBounds;
rDraw.Inflate(-1, -1);
{
brBack = Brushes.White;
g.FillRectangle(brBack, e.ClipBounds);
}
try
{
ComboboxColorItem oColorItem = (ComboboxColorItem)((ComboBox)sender).SelectedItem;
s = oColorItem.ToString();
c = oColorItem.Value;
}
catch
{
s = "red";
c = Color.Red;
}
SolidBrush b = new SolidBrush(c);
Rectangle r = new Rectangle(e.ClipBounds.Left + 5, e.ClipBounds.Top + 3, 10, 10);
g.FillRectangle(b, r);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, Form.DefaultFont, Brushes.Black, e.ClipBounds.Left + 25, e.ClipBounds.Top + 1);
b.Dispose();
g.Dispose();
e.Handled = true;
}
}
}
Is there something I am missing? Must be.
Update:
I tried this in the CellPainting event:
if(e.ColumnIndex == 0 && e.RowIndex > 0)
{
using (Graphics g = e.Graphics)
{
g.FillRectangle(Brushes.Aqua, e.CellBounds);
}
}
else
{
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
}
e.Handled = true;
That improves things in the sense that it does not go as weird. Ofcourse, it is not actually drawing anything. But then it doe snot take long for the left most cells (with the editing symbols) to only show in white. So the mechanics of it are still not right.
Thank you.
If I try it the way suggested I end up with:
Made progress! Can we adkjust it to still include the grid lines? Like in normal cells?
After
exchanging all ClipBounds by CellBounds
Deleting the g.Dispose();
..things look almost normal.
This is the result :
Of this Paint event:
private void dataGridView2_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 4 && e.RowIndex == 0) // use your own checks here!!
{
e.PaintBackground(e.CellBounds, true);
e.PaintContent(e.CellBounds);
Graphics g = e.Graphics;
Color c = Color.Empty;
string s = "";
Brush br = SystemBrushes.WindowText;
Brush brBack;
Rectangle rDraw;
rDraw = e.CellBounds;
rDraw.Inflate(-1, -1);
{
brBack = Brushes.White;
g.FillRectangle(brBack, rDraw); // **
}
try
{ // use your own code here again!
// ComboboxColorItem oColorItem =
// (ComboboxColorItem)((ComboBox)sender).SelectedItem;
s = "WW";// oColorItem.ToString();
c = Color.LawnGreen;// oColorItem.Value;
} catch
{
s = "red";
c = Color.Red;
}
// asuming a square is right; make it a few pixels smaller!
int butSize = e.CellBounds.Height;
Rectangle rbut = new Rectangle(e.CellBounds.Right - butSize ,
e.CellBounds.Top, butSize , butSize );
ComboBoxRenderer.DrawDropDownButton(e.Graphics, rbut,
System.Windows.Forms.VisualStyles.ComboBoxState.Normal);
SolidBrush b = new SolidBrush(c);
Rectangle r = new Rectangle( e.CellBounds.Left + 5,
e.CellBounds.Top + 3, 10, 10);
g.FillRectangle(b, r);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, Form.DefaultFont, Brushes.Black,
e.CellBounds.Left + 25, e.CellBounds.Top + 1);
b.Dispose();
//g.Dispose(); <-- do not dispose of thing you have not created!
e.Handled = true;
}
}
Note that I only have one CombBoxCell, so I changed the checks. And that I have no ComboboxColorItem, so I substituted a random string & color.
Update from OP: I had some of the syntax wrong and needed:
// use your own code here again!
if(DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
ComboboxColorItem oColorItem = (ComboboxColorItem)DataGridView1
.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
s = oColorItem.ToString();
c = oColorItem.Value;
}
I have a problem with drawing image on form background. I have a form where there are inserted both scrollbars (H and V). Because I need to be able display image in original size I use them for scrolling it but when I scroll to maximum right or bottom on both sides missing 7 pixels which are hidden under scrollbars. There is sample code:
private int PosX, PosY;
this.Map = new Bitmap(TestLines.Properties.Resources.mapa);
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
if (this.Map != null)
{
e.Graphics.DrawImageUnscaled(Map, new Point(this.PosX, this.PosY));
int MapResX = (int)((float)this.Map.Width / this.Map.HorizontalResolution * e.Graphics.DpiX);
int MapResY = (int)((float)this.Map.Height / this.Map.VerticalResolution * e.Graphics.DpiY);
if (MapResX > this.ClientSize.Width && MapResY > this.ClientSize.Height - this.toolStrip1.Height)
{
hScrollBar1.Minimum = 0;
hScrollBar1.Maximum = MapResX - this.ClientSize.Width + vScrollBar1.Width;
hScrollBar1.Visible = true;
vScrollBar1.Minimum = 0;
vScrollBar1.Maximum = MapResY - this.ClientSize.Height + toolStrip1.Height + hScrollBar1.Height;
vScrollBar1.Visible = true;
}
else if (MapResX > this.ClientSize.Width)
{
hScrollBar1.Minimum = 0;
hScrollBar1.Maximum = MapResX - this.ClientSize.Width;
hScrollBar1.Visible = true;
vScrollBar1.Visible = false;
}
else if (MapResY > this.ClientSize.Height - this.toolStrip1.Height)
{
vScrollBar1.Minimum = 0;
vScrollBar1.Maximum = MapResY - this.ClientSize.Height + toolStrip1.Height;
vScrollBar1.Visible = true;
hScrollBar1.Visible = false;
}
else
{
hScrollBar1.Visible = false;
vScrollBar1.Visible = false;
}
}
}
Note that there is also a toolstrip where i do not draw. And then simple scrollbars actions:
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
this.PosX = -e.NewValue;
this.Invalidate(false);
this.Update();
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
this.PosY = toolStrip1.Height -e.NewValue;
this.Invalidate(false);
this.Update();
}
Can you describe me why this happens ?
This is just not the right way to go about it. Create your own control instead, using Panel as the base class so you get the scrolling for free. Add a new class to your project and paste the code shown below. Compile. Drop it from the top of the toolbox onto your form, you probably want to set its Dock property to Fill. Assign the Map property, either with the designer or in your code.
using System;
using System.Drawing;
using System.Windows.Forms;
class MapPanel : Panel {
public MapPanel() {
this.DoubleBuffered = true;
this.ResizeRedraw = true;
}
private Image map;
public Image Map {
get { return map; }
set {
map = value;
this.AutoScrollMinSize = value == null ? Size.Empty : value.Size;
this.Invalidate();
}
}
protected override void OnPaintBackground(PaintEventArgs e) {
base.OnPaintBackground(e);
if (map != null) {
e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
e.Graphics.DrawImage(map, 0, 0);
}
}
}