Retrieve WP system tray (notification area) height - silverlight

For some control layout calculations, I need to know the height of the notification area. Sure, I know that it equals 32 pixels in portrait mode in WP 7/8/8.1, but it's not a good idea to hard code this value for the future releases of the OS. How can I retrieve this value on-the-fly in a Silverlight app?

You cant get Height of notification area by Code.
its Standards are Pre-Defined.
System Tray is the small tiny bar across the top of the Phone screen. It displays in Portrait mode. When your application is set in Portrait mode, the height of the System Tray becomes 32 pixel and when the application is set in Landscape mode, the width of the System Tray becomes 72 pixel. This is as per the UI Design Guidelines and Interaction Guideline of Windows Phone 7.
You can get more information here about what is accessible

double contentScaleFactor = (double)Application.Current.Host.Content.ScaleFactor / 100;
double systemTrayHeight = 32 / contentScaleFactor;
Or 72 for landscape orientation. Phones like the Lumia 1520 scale the app content up, so you have to adjust for it.

Found a workaround. We can determine the vertical offset for the main layout root control (generally a Grid), and it will be the height of the system tray:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
GeneralTransform gt = LayoutRoot.TransformToVisual(Application.Current.RootVisual as UIElement);
Point offset = gt.Transform(new Point(0, 0));
double systemTrayHeight = offset.Y;
}

Related

form size to different resolutions

hoping you could help me out with the sizing if my forms. on my school laptop the form fits nicely to the resolution. however, on my personal laptop, the form does not fit the larger res and for some reason is more zoomed. I have sent pictures too
You get you screen resolution with following code:
Screen.PrimaryScreen.Bounds.Width;
Screen.PrimaryScreen.Bounds.Height;
you could adjust the position from every control in the form_load event with Control.Location or the size with Control.Size depending on the width and height of your screen.
For example
label1.Location.X = Screen.PrimaryScreen.Bounds.Width - 100;
label1.Location.Y = Screen.PrimaryScreen.Bounds.Height - 50;
If you want your control to always be fullscreen, you could also add this.WindowState = FormWindowState.Maximized; in the form_load event. then its easier to adjust the positions of the controls depending on screen size.

wpf notifyicon context menu not centered on tray icon at 100% dpi

I'm using the wpf notifyicon (http://www.hardcodet.net/wpf-notifyicon)
When my laptop is at 100% dpi scaling, the left side of the context menu is centred on the tray icon, as expected.
When the laptop isn't at 100%, the context menu is pushed to the far right.
On high resolution laptop displays, 100% scaling is not the default.
Wherever my tray icon is positioned, that is, however far from the clock, the menu always pops up over the clock, as far to the bottom-right of the screen as is possible while remaining visible.
Note: I'm testing on a default installation of Windows 8.1. Also, the NotifyIcon that I'm using is the one that is generally recommended for anyone attempting tray functionality in WPF.
To reproduce: the problem exists in the windowless sample provided by hardcodet. I'm using wpf NotifyIcon without a window, and can reproduce easily in code or xaml. In fact, I cannot stop reproducing it. It occurs when dpi scaling is turned on, i.e. when a 1080p display is actually showing a lesser resolution, which is what windows does to stop applications having text too tiny to read.
Any ideas about how I can make the context menu appear in the expected place regardless of dpi?
Screen shots as suggested by kennyzx:
good behaviour. the m on red background (MEGAsync) has just been right-clicked
bad behaviour. the green tick, my notifyicon, has just been right-clicked and the menu appears over the clock
!good behaviour. the m on red background (MEGAsync) has just been right-clicked
!bad behaviour. the green tick, my notifyicon, has just been right-clicked and the menu appears over the clock
and some code:
var n = new TaskbarIcon();
n.Icon=new System.Drawing.Icon(#"C:\window - 64 - tick.ico");
n.ContextMenu = new System.Windows.Controls.ContextMenu();
n.ContextMenu.Items.Add(new System.Windows.Controls.MenuItem {Header="E_xit" });
Found the solution here: http://www.codeproject.com/Messages/4929452/Problem-with-context-menu-position.aspx
It is, with thanks to codeproject user Igorious:
Get the code for Wpf Notifyicon (http://www.hardcodet.net/wpf-notifyicon)).
In Hardcodet.Wpf.TaskbarNotification.TaskbarIcon.ShowContextMenu()
replace
ContextMenu.HorizontalOffset = cursorPosition.X;
ContextMenu.VerticalOffset = cursorPosition.Y;
with
var g = Graphics.FromHwnd(IntPtr.Zero);
var scaleX = g.DpiX / 96.0;
var scaleY = g.DpiY / 96.0;
ContextMenu.HorizontalOffset = cursorPosition.X / scaleX;
ContextMenu.VerticalOffset = cursorPosition.Y / scaleY;
Explanation (thanks to codeproject user yachting):
It's needed because WinApi.GetPhysicalCursorPos return the mouse position in pixel,
but WPF's measurement unit is device independent pixel (by definition, it's 1/96 inch)
You need to adjust the return value of GetPhysicalCursorPos by DPI (dots per inch) setting,
otherwise the position of the context menu will be incorrect if users set DPI other than the default 96.

Printing with more than 96 dpi in WPF

Let me explain the problem, I'm getting stuck in it
If I change the dpi settings from the dialog of Printing Preferences of a virtual printer like PDF Creator or any printer that allows to change this setting, and then set a breakpoint like the code below:
PrintDialog printDialog = new PrintDialog();
if ((bool)printDialog.ShowDialog().GetValueOrDefault())
{
System.Printing.PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
...... insert breakpoint here
}
I can see that the properties printDialog.PrintTicket.PageResolution.X; and printDialog.PrintTicket.PageResolution.Y change correctly while printDialog.PrintTicket.PageMediaSize.Width and printDialog.PrintTicket.PageMediaSize.Height don't change despite the printer resolution change... an A4 paper in portrait mode will always have PageMediaSize.Height = 1122.5196850393702 and PageMediaSize.Width = 793.70078740157476 no matter which resolution is set before ..... for WPF the unit size of these dimensions is set to 1/96th inch but when is Ok on screen because default screen resolution is 96 dpi on the other side is wrong on the printer because it has a different resolution, in other words confuting that Height and Width of the paper are read only properties if I cannot find the way to tell WPF that the unit size of the printer is not 1/96th inch but for example 1/300th inch (if on the printer I previously set 300 dpi ) there's absolutely no way to print at higher resolution than 96dpi
A last note, in my specific case I cannot use RenderTargetBitmap and then resize all to match printer's paper height and width settings because I'm printing high definition barcode images and it would cause an image rescaling that would make the barcode unreadable on final paper because i create it with the purpose to be printed with a resolution of 300dpi which without a resizing will result out of bounds because WPF is telling me the printer paper dimensions in the wrong unit size (1/96th inch) despite the real dpis prevoiusly set on printer
Hoping to have clarified enough the problem,
thanks in advance,
Dave

Use a different image depending on the computer's dpi setting

I have used 16x16 px images in my application, so that I get crisp edges and no automatic resizing at the standard dpi setting of 96.
When the user changes their dpi setting, the images get enlarged, and since the source files are only 16x16, they look naturally bad. Is there a way I can provide multiple images for a particular image source, and the best one will be chosen automatically? For example I provide images with the size of 16x16, 20x20 and 24x24 pixels, when the image's size is 16x16 [wpf units], so I have one perfect match for 96, 120 and 144 dpi?
What best i can think is to set the image source dynamically at run-time based on system's DPI settings. In code-behind you can set dynamically like -
ImageViewer1.Source = new BitmapImage(new Uri(#"\\myserver\\folder1\\sample.png"));
Listen to this event in your class to get notified about the dpi settings changed of computer - Microsoft.Win32.SystemEvents.DisplaySettingsChanged. Details of it can be found here - System Events
Also, you can get the system dpi value using the following code -
float dpiX, dpiY;
Graphics graphics = this.CreateGraphics();
dpiX = graphics.DpiX;
dpiY = graphics.DpiY;
Move this logic to a property and based on the property value, dynamically set the image source.

Positioning of Windows Form Application

How do I code the start location of my winform app such that it always starts in the bottom right hand corner of the screen. As using x and y coordinate only affects one particular screen resolution, on a smaller or larger screen the winform would not appear in the desired location.
Thanks!
You have to do this in the OnLoad() method/event, one of the few real reasons to use it. The form's actual size won't be the designed size because the user might have changed preferences like the window caption height or the form might be rescaled due to a different video DPI setting. This is all sorted out when OnLoad() starts running.
Make it look like this:
protected override void OnLoad(EventArgs e) {
var scr = Screen.FromPoint(this.Location);
this.Left = scr.WorkingArea.Right - this.Width;
this.Top = scr.WorkingArea.Bottom - this.Height;
base.OnLoad(e);
}
Check this on MSDN:
Setting the Screen Location of Windows Forms
Regards

Resources