1

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

4

2 回答 2

1

嗨,试试这个来获得屏幕的宽度和高度。

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

例外是因为您在呈现之前指向 Mypanel。像这样做

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)
        }

我希望这将有所帮助。

于 2012-07-30T05:40:36.337 回答
0

这个怎么样:

Screen.PrimaryScreen.WorkingArea.Height

Screen.PrimaryScreen.WorkingArea.Width

如果您想考虑 DPI 设置,我使用它(_windowToControl 是您应用程序中的一个窗口):

    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;
        }
    }
于 2012-07-30T12:30:33.037 回答