I want to draw a border outline of matching braces by clicking on one of them. Just like ReSharpers Brace Matching option.
The code itself kinda works but I want the border to be a simple square and I have absolutely no idea how to achieve that.
What it looks like now:
What I want:
Here's the Code. It's based on a Microsofts exmple vor Visual Studio Extentions for mark a specific char.
private void Draw(int from, int to)
{
var fromTo = new List<int> { from, to }.OrderBy(x => x).ToList();
from = fromTo.First();
to = fromTo.Last();
var textViewLines = this.view.TextViewLines;
var span = new SnapshotSpan(this.view.TextSnapshot, Span.FromBounds(from, to + 1));
var geometry = (textViewLines as IWpfTextViewLineCollection).GetMarkerGeometry(span);
if (geometry != null)
{
this.Draw(span, geometry);
}
}
private void Draw(SnapshotSpan span, Geometry geometry)
{
var drawing = new GeometryDrawing(new SolidColorBrush(), this.pen, geometry);
drawing.Freeze();
var drawingImage = new DrawingImage(drawing);
drawingImage.Freeze();
var image = new Image {
Source = drawingImage
};
Canvas.SetLeft(image, geometry.Bounds.Left);
Canvas.SetTop(image, geometry.Bounds.Top);
this.layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, span, null, image, null);
}
Related
I am following this tutorial for adding polygons onto a map in Xamarin. I am currently implementing the iOS section of this tutorial, but because I want to add multiple polygons to my map instead of the single polygon shown in the tutorial, I am using the addOverlays() function which takes in an array of IMKOverlay objects, instead of the addOverlay() function which takes 1 IMKOverlay object.
For some reason all of my polygons are being plotted at the same coordinates as my first polygon in my List, similar to an issue this person had!
Here is my code:
void addPolygonsToMap()
{
overlayList = new List<IMKOverlay>();
for (int i = 0; i < polygons.Count; i++)
{
CLLocationCoordinate2D[] coords = new CLLocationCoordinate2D[polygons[i].Count];
int index=0;
foreach (var position in polygons[i])
{
coords[index] = new CLLocationCoordinate2D(position.Latitude, position.Longitude);
index++;
}
var blockOverlay = MKPolygon.FromCoordinates(coords);
overlayList.Add(blockOverlay);
}
IMKOverlay[] imko = overlayList.ToArray();
nativeMap.AddOverlays(imko);
}
You have to edit the code in MKOverlayRenderer GetOverlayRenderer(MKMapView mapView, IMKOverlay overlayWrapper) which is in the tutorial. Just remove the if statement or remove the polygonRenderer == null && in the if statement, like this:
MKOverlayRenderer GetOverlayRenderer(MKMapView mapView, IMKOverlay overlayWrapper)
{
if (!Equals(overlayWrapper, null))
{
var overlay = Runtime.GetNSObject(overlayWrapper.Handle) as IMKOverlay;
polygonRenderer = new MKPolygonRenderer(overlay as MKPolygon)
{
FillColor = UIColor.Red,
StrokeColor = UIColor.Blue,
Alpha = 0.4f,
LineWidth = 9
};
}
return polygonRenderer;
}
Otherwise, it will always return the first polygonRenderer which is including your first coordinates group.
I am trying to make the points in a C# Chart show up as letters (e.g. 'A' and 'B') to separate sets of points, instead of coloring them with green/red.
The Label property of the point collection does not satisfy this request, since it only places labels beside the point, and I want the label to replace the point.
Here is what I have:
while (reader.Read())
{
if (reader[2].ToString() == "Kupi 002")
chart3.Series["Good_Group"].Points[pointsCounter].Label = "A";
else
chart3.Series["Good_Group"].Points[pointsCounter].Label = "B";
pointsCounter = pointsCounter + 1;
}
What property, instead of Label, should I use to achieve my goal?
Thanks in advance.
One option would be to use the MarkerImage property of either the series or point to apply custom bitmap labels of each letter:
chart.Series[0].MarkerImage = "a.png";
chart.Series[0].Points[2].MarkerImage = "b.png";
chart.Series[0].Points[4].MarkerImage = "b.png";
The bitmap images could be created in a paint program and distributed with your program. They could also be generated dynamically, as in the following (highly simplified) example:
private void CreateLetterBitmap(char letter, string path)
{
using (var bmp = new Bitmap(13, 13))
using (var gfx = Graphics.FromImage(bmp))
using (var font = new Font(FontFamily.GenericSansSerif, 12.0f, FontStyle.Bold, GraphicsUnit.Pixel))
{
gfx.TextRenderingHint = TextRenderingHint.AntiAlias;
gfx.DrawString(letter.ToString(), font, Brushes.Black, new Point(0, 0));
bmp.Save(path, ImageFormat.Png);
}
}
private void PrepareChart()
{
CreateLetterBitmap('A', "a.png");
CreateLetterBitmap('B', "b.png");
chart.Series[0].MarkerImage = "a.png";
chart.Series[0].Points[2].MarkerImage = "b.png";
chart.Series[0].Points[4].MarkerImage = "b.png";
}
I'm working on a mini-game where I need to make images go from their initial position to another using a translation transformation.
The problem is: I don't know how to proceed to apply the translation.
So here's the code used to generate my images.
Image myImage1 = new Image();
myImage1.Source = new BitmapImage(new Uri("/Images/image1.png", UriKind.Relative));
myImage1.Name = "image" + index++.ToString();
myImage1.Tap += myImage_Tap;
Canvas.SetLeft(image, 200);
Canvas.SetTop(image, 600);
gameArea.Children.Add(image);
Thanks for your time.
You have two choices to move your image around. The first is using a Canvas like your code shows, but you have to make sure your element is actually within a Canvas. Is your "gameArea" a Canvas? If it's not, your code won't work. The other option is to use Transforms.
var myImage1 = new Image
{
Source = new BitmapImage(new Uri("/Images/image1.png", UriKind.Relative)),
Name = "image" + index++.ToString(),
Tap += myImage_Tap,
RenderTransform = new TranslateTransform
{
X = 200,
Y = 600
}
};
gameArea.Children.Add(image);
Now gameArea can be any type of Panel and it will work.
Note that when using a Canvas, your Top and Left will be from the upper left corner of the Canvas. When using a Transform, your X and Y will be relative to where the element would have originally be drawn.
UPDATE -- Helper class to do simple animations using a Canvas
public sealed class ElementAnimator
{
private readonly UIElement _element;
public ElementAnimator(UIElement element)
{
if (null == element)
{
throw new ArgumentNullException("element", "Element can't be null.");
}
_element = element;
}
public void AnimateToPoint(Point point, int durationInMilliseconds = 300)
{
var duration = new Duration(TimeSpan.FromMilliseconds(durationInMilliseconds));
var easing = new BackEase
{
Amplitude = .3
};
var sb = new Storyboard
{
Duration = duration
};
var animateLeft = new DoubleAnimation
{
From = Canvas.GetLeft(_element),
To = point.X,
Duration = duration,
EasingFunction = easing,
};
var animateTop = new DoubleAnimation
{
From = Canvas.GetTop(_element),
To = point.Y,
Duration = duration,
EasingFunction = easing,
};
Storyboard.SetTargetProperty(animateLeft, "(Canvas.Left)");
Storyboard.SetTarget(animateLeft, _element);
Storyboard.SetTargetProperty(animateTop, "(Canvas.Top)");
Storyboard.SetTarget(animateTop, _element);
sb.Children.Add(animateLeft);
sb.Children.Add(animateTop);
sb.Begin();
}
}
So here's what I did with your code, I took just this part and made it a function:
public void AnimateToPoint(UIElement image, Point point, int durationInMilliseconds = 300)
{
var duration = new Duration(TimeSpan.FromMilliseconds(durationInMilliseconds));
var sb = new Storyboard
{
Duration = duration
};
var animateTop = new DoubleAnimation
{
From = Canvas.GetTop(image),
To = point.Y,
Duration = duration
};
Storyboard.SetTargetProperty(animateTop, new PropertyPath("Canvas.Top")); // You can see that I made some change here because I had an error with what you gave me
Storyboard.SetTarget(animateTop, image);
sb.Children.Add(animateTop);
sb.Begin();
}
And then I made the call with:
Point myPoint = new Point(leftpos, 300); // leftpos is a variable generated randomly
AnimateToPoint(myImage, myPoint);
So now there is still a single error, and it's in the instruction "sb.Begin;".
And it says: Cannot resolve TargetProperty Canvas.Top on specified object.
I reaylly don't know how to proceed.
Thanks for your previous answer!
I want to color on part of tree node but not through the user (without using "selected node")
so DrawMode is not helping me.
I am using c#
For example I want that all tree nodes with space on the text will color in one side at green and the other side to red.
Thanks!!
DrawMode is the way to go. You have to set it to OwnerDrawText, and subscribe to the DrawNode event. I.e.:
this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawText;
this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView1_DrawNode);
This is just a sample of how the drawing method could look like. It's up to you to modify it in order to have a good graphical result, but it can give you an idea of the way to go.
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
string txt = e.Node.Text;
int idx = txt.IndexOf(' ');
string greenTxt;
string redTxt;
if (idx >= 0)
{
greenTxt = txt.Substring(0, idx);
redTxt = txt.Substring(idx);
}
else
{
greenTxt = txt;
redTxt = string.Empty;
}
Rectangle greenRect = new Rectangle(e.Bounds.Location, new Size((int)Math.Ceiling(e.Graphics.MeasureString(greenTxt, nodeFont).Width), e.Bounds.Height));
Rectangle redRect = new Rectangle(e.Bounds.Location + new Size(greenRect.Width, 0), new Size((int)Math.Ceiling(e.Graphics.MeasureString(redTxt, nodeFont).Width), e.Bounds.Height));
e.Graphics.DrawString(greenTxt, nodeFont, Brushes.Green, greenRect);
if (!string.IsNullOrEmpty(redTxt))
e.Graphics.DrawString(redTxt, nodeFont,
Brushes.Red, redRect);
}
You can find a more complex example here.
I have drag'n'drop support in my application. When I start dragging a certain item, I create the dragging visual from code. The code below works, but I want to set the width and height using the style instead of hardcoding it like below. How?
private void OnDragInitialize(object sender, DragInitializeEventArgs args)
{
args.AllowedEffects = DragDropEffects.All;
var shape = new MyShape();
// TODO: How do I let the 'DragShapeStyle' style apply the dimensions?
var shapeSize = new Size(180, 80);
var dropInfo = new DiagramDropInfo(shapeSize, SerializeItems(
new List<IDiagramItem> { shape }));
args.Data = dropInfo;
args.DragVisualOffset = new Point(
args.RelativeStartPoint.X - (shapeSize.Width / 2),
args.RelativeStartPoint.Y - (shapeSize.Height / 2));
var dragShape = new DragShape()
{
DataContext = new ShapeData()
{
Data = new MyData()
}
};
dragShape.SetResourceReference(DragShape.StyleProperty, "DragShapeStyle");
args.DragVisual = new ContentControl() { Content = dragShape };
}
My DragShapeStyle style paints a grid, with its size set using properties of MyData and some other logic. I want to apply this logic to my dragged Visual.