I have been working on WPF desktop application and have graphics according to a particular screen resolution. I need to scale the all the margins based on screen resolution. How can I achieve this?
Asked
Active
Viewed 1,839 times
0
-
1check this http://stackoverflow.com/questions/1927540/how-to-get-the-size-of-the-current-screen-in-wpf or this – Akrem Nov 26 '13 at 10:21
-
The link answers for multi-screen problem and I dont care abut multi screen as of now and what does "this" refers to in your comment, its not a link – androider Nov 26 '13 at 10:28
3 Answers
1
you can use :
System.Windows.SystemParameters.PrimaryScreenWidth;
System.Windows.SystemParameters.PrimaryScreenHeight;
make a little search :)

Akrem
- 5,033
- 8
- 37
- 64
-
1Yeah I did that, but how would I adjust the margins.For eg:
is designed for 1366x768. But this margin would be set for all screens independent of the resolution. I ned answer for this, if its a valid question and it makes sense – androider Nov 26 '13 at 10:25 -
@yshooooo in this case you better look for Grid.RowDefinitions and ColumnDefinitions – Akrem Nov 26 '13 at 11:24
-
So you mean to say, if the textblock is of width 30, then i should create a grid and have its ColumnDefinitions defined as 1* and 2*. That would be a lot of work I guess, and also I cant achieve accuracy for the all the definitions. Isn't there some other way? – androider Nov 26 '13 at 11:53
-
@yshooooo you want to adjust margin between textbox or change the with when resolution changed ? – Akrem Nov 26 '13 at 13:25
-
want to adjust the margins.But it would be a plus point if you could give answer for both of them – androider Nov 26 '13 at 14:03
0
To get the Screen Resolutions
we have to use the dpi factor value of system, use the below code as per this article (please read it in full to understand):
using System.Windows.Media;
Window mainWindow = Application.Current.MainWindow;
PresentationSource mainWindowPresentationSource = PresentationSource.FromVisual(mainWindow);
Matrix m = mainWindowPresentationSource.CompositionTarget.TransformToDevice;
double dpiWidthFactor = m.M11;
double dpiHeightFactor = m.M22;
double ScreenHeight = SystemParameters.PrimaryScreenHeight * dpiHeightFactor; //calculated correct value
double ScreenWidth = SystemParameters.PrimaryScreenWidth * dpiWidthFactor; //calculated correct value

S2S2
- 8,322
- 5
- 37
- 65
-
-
@MarkRichman Your MainWindow object should be loaded before calling this code, I think that is the reason you are getting null. Try putting your code in Window.Loded event or some other event when Load is completed.. – S2S2 May 21 '14 at 06:30
0
Add reference to :- System.Windows.Forms
var width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;

Subhash Saini
- 254
- 4
- 5