I need to draw lines to demonstrate transportation of goods on Bing maps. To clarify start- and end-point, I draw a little arrowhead on the destination side. The problem is that when I view the global map, certain lines are drawn along the shortest path 'around the back'. For example going from New York City to Tokyo, it would be drawn across the Pacific Ocean. Since the arrowhead is drawn separately it is reversed.
>-------
instead of
<-------
This problem gets worse when the user scrolls east/west on the map, so that Europe is no longer centered.
This is the code I have so far. I didn't write this, but still have to fix the bug presented in this question. If you have suggestions for optimization, feel free to mention it.
public class MapArrow
{
private readonly MapPolyline line;
private readonly MapLayer arrowLayer;
private readonly Polyline arrowLine;
#region constructor
public MapArrow(Location start, Location end)
: this(start, end, Colors.Red)
{
}
public MapArrow(Location start, Location end)
{
color.A = 200;
Color = Colors.Red;
drawingColor = Colors.Red;
HeadWidth = 8;
HeadHeight = 8;
StrokeThikness = 5;
Start = start;
End = end;
line = new MapPolyline();
arrowLayer = new MapLayer();
arrowLine = new Polyline();
arrowLayer.AddChild(arrowLine, end);
UpdateMapLine();
UpdateArrowPolyline();
}
#endregion
#region properties
public double HeadWidth { get; set; }
public double HeadHeight { get; set; }
public Color Color { get; set; }
public int StrokeThikness { get; set; }
public Location Start { get; private set; }
public Location End { get; private set; }
public MapPolyline Line
{
get
{
return line;
}
}
public MapLayer Arrow
{
get
{
return arrowLayer;
}
}
#endregion
private void UpdateMapLine()
{
line.Stroke = new SolidColorBrush(drawingColor);
line.StrokeThickness = StrokeThikness;
line.Opacity = 1;
line.Locations = new LocationCollection()
{
Start,
End
};
}
private void UpdateArrowPolyline()
{
double theta = Math.Atan2(Start.Latitude - End.Latitude, Start.Longitude - End.Longitude);
double sint = Math.Sin(theta);
double cost = Math.Cos(theta);
Point corner1;
Point corner2;
if (!Start.Equals(End))
{
corner1 = new Point(
(HeadWidth*cost - HeadHeight*sint),
0 - (HeadWidth*sint + HeadHeight*cost));
corner2 = new Point(
(HeadWidth*cost + HeadHeight*sint),
(HeadHeight*cost - HeadWidth*sint));
}
else
{
corner1 = new Point(0,StrokeThikness/2);
corner2 = new Point(0,-StrokeThikness/2);
}
Point endPoint = new Point(0, 0);
arrowLine.Stroke = new SolidColorBrush(drawingColor);
arrowLine.StrokeThickness = StrokeThikness;
arrowLine.Opacity = 1;
arrowLine.Points = new PointCollection()
{
corner1,
endPoint,
corner2
};
}
}
The shortest (Great Circle) is the correct way to draw it. Is it changing when you zoom in to a 2d projection?
Could you detect which view is being displayed and change the orientation based on this?
Related
I want to have two controls in the same datagridview column.
I want to customize the DataGridViewComboBoxCell so that it will show the values of the selected value and on it a floating label with some text. in the past i was able to do it with a checkbox and a label but the problem with the DataGridViewComboBoxCell is that it comes out with an empty datasource when I override the paint event.
I tried to assign the datasource again after I used the Paint event but then although I see values in the DataGridViewComboBoxCell and the label showing the right value, I get into an infinite loop so I see the GUI blinking constantly.
10x for the help.
the code is the following:
*when the form loads
MyDGVCheckBoxColumn col = new MyDGVCheckBoxColumn();
col.DataPropertyName = "value";
col.DataSource = list;
col.DisplayMember = "Yes";
col.ValueMember = "value";
col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dataGridView1.Columns.Add(col);
this.dataGridView1.RowCount = 50;
the class for the generic list:
public class CheckThis
{
public string Yes { get; set; }
public string value { get; set; }
public CheckThis()
{
Yes = "gggg";
value = "1";
}
}
the code for the custom DataGridViewComboBoxCell (I used a similar example in the past from some site)
public class MyDGVCheckBoxColumn : DataGridViewComboBoxColumn
{
private string label;
public string Label
{
get
{
return label;
}
set
{
label = value;
}
}
public override DataGridViewCell CellTemplate
{
get
{
return new MyDGVCheckBoxCell();
}
}
}
public class MyDGVCheckBoxCell : DataGridViewComboBoxCell
{
private string label;
public string Label
{
get
{
return label;
}
set
{
label = value;
}
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
// the base Paint implementation paints the check box
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
// Get the check box bounds: they are the content bounds
Rectangle contentBounds = this.GetContentBounds(rowIndex);
// Compute the location where we want to paint the string.
Point stringLocation = new Point();
stringLocation.Y = cellBounds.Y + 30;
stringLocation.X = cellBounds.X + contentBounds.Bottom;
// Paint the string.
var res = false;
MyDGVCheckBoxColumn col = (MyDGVCheckBoxColumn)this.OwningColumn;
col.DataSource = list;
col.DisplayMember = "Yes";
col.ValueMember = "value";
this.label = "Customer Does Not Appear";
graphics.DrawString(
this.Label, new Font("Arial", 6, FontStyle.Bold), System.Drawing.Brushes.Red, stringLocation);
}
public object list { get; set; }
}
I have a DataGrid in WPF and fill it with data like this:
public enum Sharing
{
Equal,
SurfaceBased,
}
public class Data
{
public bool Active { get; set; }
public string Name { get; set; }
public int Floor { get; set; }
public Sharing Sharing { get; set; }
}
public ObservableCollection<Data> _col = new ObservableCollection<Data>()
{
new Data(){Active = true, Name = "KRL", Floor = 0 },
new Data(){Name = "DAT", Floor = 1},
new Data(){Name = "TRE", Floor = 1},
new Data(){Name = "DUO", Floor = 2},
};
public MainWindow()
{
InitializeComponent();
grid.AutoGenerateColumns = true;
grid.DataContext = _col;
grid.ItemsSource = _col;
}
I was wondering if I could use some attributes on the enumerations and the POCO class so that the DataGrid displays them (instead of the variable names) on the headers and ComboCoxes.
Something like this:
public enum Sharing
{
[Name("This is a test")]
Equal,
[Name("This is a test 2")]
SurfaceBased,
}
Is this possible?
OK. Here is the way to do it for the Headers:
You add attributes, like Description attributes to your Properties.
public class MyPOCO
{
[Description("The amount you must pay")]
public float Amount { get; set; }
}
Then, in a class derived from DataGrid you do this:
protected override void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs e)
{
try
{
base.OnAutoGeneratingColumn(e);
var propDescr = e.PropertyDescriptor as System.ComponentModel.PropertyDescriptor;
e.Column.Header = propDescr.Description;
}
catch (Exception ex)
{
Utils.ReportException(ex);
}
}
For adding custom names to the members of the enumerations, you need to make a custom column. You can see a simple example here : https://stackoverflow.com/a/17510660/964053.
I have a winform powerpacks datareapter control having a picture box. This is the code snippet from the classes.
DisplaySystemUsersControl.Designer.cs
this.picBoxUserImage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.picBoxUserImage.DataBindings.Add(new System.Windows.Forms.Binding("Image", this.UserBindingSource, "User_Image", true));
this.picBoxUserImage.Location = new System.Drawing.Point(3, 3);
this.picBoxUserImage.Name = "picBoxUserImage";
this.picBoxUserImage.Size = new System.Drawing.Size(100, 93);
this.picBoxUserImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.picBoxUserImage.TabIndex = 0;
this.picBoxUserImage.TabStop = false;
this.picBoxUserImage.Click += new System.EventHandler(this.picBoxUserImage_Click);
DisplaySystemUsersControl.cs
public DisplaySystemUsersControl()
{
InitializeComponent();
this.dataRepeaterAccounts.DataSource = this.UserBindingSource;
LoadAccountData();
}
private void LoadAccountData()
{
SystemUserBusinessClass oSystemUserBusinessClass = new SystemUserBusinessClass();
List<SystemUserEntity_Only_For_UI_Binding> obj = oSystemUserBusinessClass.GetSystemUsersForUI();
BindingSource tempUserBindingSource = (BindingSource)dataRepeaterAccounts.DataSource;
obj.ForEach(oSystemUserEntity_Only_For_UI_Binding => tempUserBindingSource.Add(oSystemUserEntity_Only_For_UI_Binding));
}
SystemUserEntity_Only_For_UI_Binding.cs
public class SystemUserEntity_Only_For_UI_Binding
{
public string User_Id { get; set; }
public string User_Name { get; set; }
public byte[] User_Image { get; set; }
}
User ID and User name is getting loaded. But Image is not getting loaded. SystemUserEntity_Only_For_UI_Binding.User_Image() is holding the image byte array.
Can anybody please tell me what is going wrong?
Your class should look something like this:
public class SystemUserEntity_Only_For_UI_Binding
{
public string User_Id { get; set; }
public string User_Name { get; set; }
public Image User_Image { get; set; }
}
The byte array needs to be translated into an image somewhere in your code:
using (MemoryStream ms = new MemoryStream(imgBytes)) {
this.User_Image = Image.FromStream(ms);
}
public void BindRepeater (DataSet dsObj)
{
pictureBox1.DataBindings.Clear();
pictureBox1.DataBindings.Add("ImageLocation", dt, "Photo");
dataRepeater1.DataSource = dsObj;
}
I've got an XNA + Silverlight game in Mango: Mostly XNA with some Silverlight UI on top. The problem I'm having is that when you hit a button or interact with a Silverlight control, the touch information is still passed along to the XNA game loop. How do you suppress this?
Wrote up a class to do the tracking for me. After your page loads (in the Loaded handler), create this and give it the root element (so it can attach to the LayoutUpdated event). Register any controls that might overlay the game surface during play. Then just call TouchesControl and pass in the touch position to find out if you should ignore that point or not. It caches the regions of the controls and updates them when there's a layout update.
Should work for rectangular elements moving, changing size or collapsing/expanding.
public class ControlTouchTracker
{
private List<FrameworkElement> controls = new List<FrameworkElement>();
private Dictionary<FrameworkElement, ControlRegion> controlBounds = new Dictionary<FrameworkElement, ControlRegion>();
public ControlTouchTracker(FrameworkElement rootElement)
{
rootElement.LayoutUpdated += this.OnLayoutUpdated;
}
public void RegisterControl(FrameworkElement control)
{
controls.Add(control);
}
public void RemoveControl(FrameworkElement control)
{
controls.Remove(control);
controlBounds.Remove(control);
}
private void OnLayoutUpdated(object sender, EventArgs e)
{
foreach (Control control in this.controls)
{
this.RefreshControlBounds(control);
}
}
private void RefreshControlBounds(FrameworkElement control)
{
if (this.ControlIsVisible(control))
{
try
{
GeneralTransform controlTransform = control.TransformToVisual(Application.Current.RootVisual);
Point offset = controlTransform.Transform(new Point(0, 0));
this.controlBounds[control] = new ControlRegion
{
Left = (float)offset.X,
Right = (float)(offset.X + control.ActualWidth),
Top = (float)offset.Y,
Bottom = (float)(offset.Y + control.ActualHeight)
};
}
catch (ArgumentException)
{
}
}
else
{
if (this.controlBounds.ContainsKey(control))
{
this.controlBounds.Remove(control);
}
}
}
private bool ControlIsVisible(FrameworkElement control)
{
// End case
if (control == null)
{
return true;
}
if (control.Visibility == Visibility.Collapsed)
{
return false;
}
return this.ControlIsVisible(control.Parent as FrameworkElement);
}
public bool TouchesControl(Vector2 touchPosition)
{
foreach (ControlRegion region in this.controlBounds.Values)
{
if (touchPosition.X >= region.Left && touchPosition.X <= region.Right &&
touchPosition.Y >= region.Top && touchPosition.Y <= region.Bottom)
{
return true;
}
}
return false;
}
public class ControlRegion
{
public float Left { get; set; }
public float Right { get; set; }
public float Top { get; set; }
public float Bottom { get; set; }
}
}
(edit) Updated example to work with parent elements changing Visibility.
Due to the way interop with XNA works, you will always get the touch input processed both by XNA and Silverlight - to some extent, XNA gets the priority, so the Silverlight acts on top of that. What you could do, if you need to ignore specific gesture locations (e.g. where Silverlight buttons are located), you could check the gesture position:
if (TouchPanel.IsGestureAvailable)
{
if (TouchPanel.ReadGesture().GestureType == GestureType.Tap)
{
if (TouchPanel.ReadGesture().Position == new Vector2(120, 120))
{
}
}
}
I have a FlowDocument that varies in height due to an ItemsControl in a BlockUIContainer. In some cases, the ItemsControl extends beyond the page height. Is there a way to scale the FlowDocument to fit a page (8.5" X 11") right before printing if needed?
As of right now, the FlowDocument is named 'doc' and the method for printing I am using is:
private void Print_Click(object sender, RoutedEventArgs e)
{
PrintDialog pd = new PrintDialog();
doc.PageHeight = pd.PrintableAreaHeight;
doc.PageWidth = pd.PrintableAreaWidth;
doc.ColumnGap = 0;
doc.ColumnWidth = pd.PrintableAreaWidth;
IDocumentPaginatorSource dps = doc;
pd.PrintDocument(dps.DocumentPaginator, "Sheet");
}
I know it's a bit late, but here is the solution I came up with.
First, we create a wrapper that will generate the document pages for us. Each page will have a scale transformation applied to it before returning it.
public class FittedDocumentPaginator : DocumentPaginator
{
public DocumentPaginator Base { get; private set; }
public double Scale { get; private set; }
private readonly ScaleTransform _sTransform;
public FittedDocumentPaginator( DocumentPaginator baseDp, double scale )
{
if ( baseDp == null )
throw new ArgumentNullException( "baseDp" );
Base = baseDp;
Scale = scale;
_sTransform = new ScaleTransform( scale, scale );
}
public override DocumentPage GetPage( int pageNumber )
{
var page = Base.GetPage( pageNumber );
( (ContainerVisual)page.Visual ).Transform = _sTransform;
return page;
}
public override bool IsPageCountValid
{
get { return Base.IsPageCountValid; }
}
public override int PageCount
{
get { return Base.PageCount; }
}
public override Size PageSize
{
get { return Base.PageSize; }
set { Base.PageSize = value; }
}
public override IDocumentPaginatorSource Source
{
get { return Base.Source; }
}
}
Using it is fairly simple:
private void PrintDocument( PrintDialog pd, FlowDocument document, double scale, string title )
{
DocumentPaginator dp = ( (IDocumentPaginatorSource)document ).DocumentPaginator;
FittedDocumentPaginator fdp = new FittedDocumentPaginator( dp, scale );
pd.PrintDocument( fdp, title );
}
If you're interested, here is how we determined the scale. In our case, the document was extended past the page width, but it can easily be modified to accommodate the page height.
private void Print( FlowDocument document, string title, double documentWidth )
{
var pd = new PrintDialog();
if ( pd.ShowDialog() == true )
{
// Set the printing margins.
Thickness pageMargins = document.PagePadding;
document.PagePadding = new Thickness( 15.0 );
// In our case, the document's width is NaN so we have to navigate the visual tree to get the ActualWidth, which is represented by 'documentWidth'.
double scale = documentWidth / pd.PrintableAreaWidth;
if ( scale < 1.0 )
{
// The report fits on the page just fine. Don't scale.
scale = 1.0;
}
double invScale = 1 / scale;
document.PageHeight = pd.PrintableAreaHeight * scale;
document.PageWidth = pd.PrintableAreaWidth * scale;
DocumentPaginator dp = ( (IDocumentPaginatorSource)document ).DocumentPaginator;
FittedDocumentPaginator fdp = new FittedDocumentPaginator( dp, invScale );
pd.PrintDocument( fdp, title );
// Restore the original values so the GUI isn't altered.
document.PageHeight = Double.NaN;
document.PageWidth = Double.NaN;
document.PagePadding = pageMargins;
}
}