SIRIGINEEDI Ravi
SIRIGINEEDI Ravi

Reputation: 13

what is the equivalent in wpf for System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width

I'm trying to convert the windows application to wpf application, and everything is fine but i was Struck at this point converting the bellow declaration not working out in code of wpf.

these are windows declarations,

Dim screenwidth As String = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width

Dim screenheight As String = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height

i googled to find the equivalent class properties for these declarations and i got some thing, but those are existing but not working at the point of time i'm trying to use them at

"PointToScreen(New Point(0,0))" they are:

If i use these in my code:

Dim screenwidth As String = System.Windows.SystemParameters.VirtualScreenWidth Dim screenheight As String = System.Windows.SystemParameters.VirtualScreenHeight '(OR)

Dim screenwidth As String = System.Windows.SystemParameters.PrimaryScreenWidth Dim screenheight As String = System.Windows.SystemParameters.PrimaryScreenHeight

       With MyPanel
            .PointToScreen(New Point(SystemParameters.VirtualScreenWidth, 0)) ' Getting Exception in this line               
            .Height = (80 / 1080) * screenheight
            .Width = screenwidth                
            .Background = New SolidColorBrush(Colors.Transparent)
        End With

Am getting the exception as Invalid Operation Exception saying that "The visual is not connected to PresentationSource."

i alreadt tried this post http://social.msdn.microsoft.com/Forums/en/wpf/thread/f3c982a1-ca16-4821-bf08-f6dd8ff8d829 , but i want to try it out using PointToScreen only.

How can i resolve this ???? Please help me

Upvotes: 1

Views: 4287

Answers (2)

JFTxJ
JFTxJ

Reputation: 552

How about this:

Screen.PrimaryScreen.WorkingArea.Height

and

Screen.PrimaryScreen.WorkingArea.Width

If you want to consider DPI settings, I use this (_windowToControl is a window in your application):

    public double ScreenPixelHeight
    {
        get
        {
            PresentationSource source = PresentationSource.FromVisual(_windowToControl);
            if (source != null)
                return Screen.PrimaryScreen.WorkingArea.Height * source.CompositionTarget.TransformFromDevice.M11;
            else
                return 0;
        }
    }
    public double ScreenPixelWidth
    {
        get
        {
            PresentationSource source = PresentationSource.FromVisual(_windowToControl);
            if (source != null)
                return Screen.PrimaryScreen.WorkingArea.Width * source.CompositionTarget.TransformFromDevice.M11;
            else
                return 0;
        }
    }

Upvotes: 0

yo chauhan
yo chauhan

Reputation: 12295

Hi try this to get the Width and height of screen.

double width=System.Windows.SystemParameters.PrimaryScreenWidth;
double height = System.Windows.SystemParameters.PrimaryScreenHeight;

The exception is because you are pointing Mypanel before it is rendered. Do it like this

if (MyPanel.IsVisible)
        { 
            MyPanel
            .PointToScreen(New Point(SystemParameters.PrimaryScreenWidth, 0)) ' Getting Exception in this line               
            .Height = (80 / 1080) * screenheight
            .Width = screenwidth                
            .Background = New SolidColorBrush(Colors.Transparent)
        }

I hope this will help.

Upvotes: 1

Related Questions