WPF C# get the top left point of the current screen you are on

Brandon Boone 31 Reputation points
2022-08-06T20:27:35.92+00:00

So I am trying to use this getmonitorinfo-returns-wrong-window-size-for-secondary-display-when-main-display because Apparently it works, but I am not setting.

anyhow, I copied the full FormMaximize class from the link and renamed it WPFWindowSizes because I am using WPF and not Forms. Other then that is it the same class word for word.

Then I tried to use it like this:

 public void Maximize()  
    {  
        _oldSize = new Size(_currentWindow.ActualWidth, _currentWindow.ActualHeight);  
        _oldLocation = new Point(_currentWindow.Top, _currentWindow.Left);  
  
  
        IntPtr handle = new WindowInteropHelper(_currentWindow).Handle;  
  
        HwndSource.FromHwnd(handle).AddHook(new HwndSourceHook(WPFWindowSizes.WindowProc));  
  
      
  
       // double x = SystemParameters.WorkArea.Width;  
       // double y = SystemParameters.WorkArea.Height;  
          
       // _currentWindow.WindowState = WindowState.Normal;  
       //_currentWindow.Top = 0;  
      //  _currentWindow.Left = 0;  
       // _currentWindow.Width = x;  
      //  _currentWindow.Height = y;  
        WindowRestoreTwoBoxVisibility = Visibility.Visible;  
        WindowRestoreOneBoxVisibility = Visibility.Collapsed;  
        MaxSizeFlag = true;  
    }  


 

I click the button that calls the function but it do not change the window at all. It does call the function because I ran the code in debug mode. Also, when I click the button again, it tries to do call SetNormal() but it thinks the window is in fullsceen. Anyhow, the Window Disappears if it is not on the Primary window.

Here is that function:

  public void SetNormal()  
    {  
        _currentWindow.WindowState = WindowState.Normal;  
        _currentWindow.Top = Math.Abs(_oldLocation.Y);  
        _currentWindow.Left= Math.Abs(_oldLocation.X);  
        _currentWindow.Width = _oldSize.Width;  
        _currentWindow.Height = _oldSize.Height;  
        WindowRestoreTwoBoxVisibility = Visibility.Collapsed;  
        WindowRestoreOneBoxVisibility = Visibility.Visible;  
        MaxSizeFlag = false;  
    }  

Also I am not sure what this class is doing , but it calls WindowProc a lot, and then at some point it get in side of here: WmGetMinMaxInfo(handle, lParam); but it does not do anything because I get monitor is set to zero.

So then I tried this:

 public void Maximize()  
    {  
        _oldSize = new Size(_currentWindow.ActualWidth, _currentWindow.ActualHeight);  
        _oldLocation = new Point(_currentWindow.Top, _currentWindow.Left);  
  
  
        IntPtr handle = new WindowInteropHelper(_currentWindow).Handle;  
        IntPtr lParam = new IntPtr();  
        MinMaxInfo  windowInfo =  WPFWindowSizes.GetMinMaxInfo(handle, lParam);  
  
       //  HwndSource.FromHwnd(handle).AddHook(new HwndSourceHook(WPFWindowSizes.WindowProc));  
  
  
  
        // double x = SystemParameters.WorkArea.Width;  
        // double y = SystemParameters.WorkArea.Height;  
  
         _currentWindow.WindowState = WindowState.Normal;  
         _currentWindow.Top = windowInfo.MaxPosition.Y;  
          _currentWindow.Left = windowInfo.MaxPosition.X;  
         _currentWindow.Width = windowInfo.MaxSize.X;  
        _currentWindow.Height = windowInfo.MaxSize.Y;  
        WindowRestoreTwoBoxVisibility = Visibility.Visible;  
        WindowRestoreOneBoxVisibility = Visibility.Collapsed;  
        MaxSizeFlag = true;  
    }  
  
  
  
  
    public static MinMaxInfo GetMinMaxInfo(IntPtr handle, IntPtr lParam)  
    {  
        MinMaxInfo minmaxInfo = (MinMaxInfo)Marshal.PtrToStructure(lParam, typeof(MinMaxInfo));  
  
        // Get current monitor information  
        int MONITOR_DEFAULT_TO_NEAREST = 0x00000002;  
        IntPtr monitor = MonitorFromWindow(handle, MONITOR_DEFAULT_TO_NEAREST);  
  
        if (monitor != System.IntPtr.Zero) // Not null pointer  
        {  
            MonitorInfo monitorInfo = new MonitorInfo();  
            GetMonitorInfo(monitor, monitorInfo);  
            Rectangle workArea = monitorInfo.WorkArea;  
            Rectangle monitorArea = monitorInfo.MonitorArea;  
            minmaxInfo.MaxPosition.X = Math.Abs(workArea.Left - monitorArea.Left);  
            minmaxInfo.MaxPosition.Y = Math.Abs(workArea.Top - monitorArea.Top);  
            minmaxInfo.MaxSize.X = Math.Abs(workArea.Right - workArea.Left);  
            minmaxInfo.MaxSize.Y = Math.Abs(workArea.Bottom - workArea.Top);  
        }  
        Marshal.StructureToPtr(minmaxInfo, lParam, true);  
        return minmaxInfo;  
    }  

But the program dies at :

MinMaxInfo minmaxInfo = (MinMaxInfo)Marshal.PtrToStructure(lParam, typeof(MinMaxInfo));
I think is it is because I am not setting up lParam right but I could not find anything online about how to find a window's lParam.

I looked at this link but it was not helpful at all
and I could not find anything that show how to get the LPARAM from the current window or the current screen.

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. ShuaiHua Du 636 Reputation points
    2022-08-09T14:38:01.507+00:00

    Hi @Brandon Boone ,

    You can get current use below code:

            WindowInteropHelper windowInteropHelper = new WindowInteropHelper(this);  
            Screen currentScreen = Screen.FromHandle(windowInteropHelper.Handle);  
    

    I made a sample:

    CS Code:

    using System;  
    using System.Windows;  
    using System.Windows.Forms;  
    using System.Windows.Interop;  
      
    namespace WpfApp.Netframework  
    {  
        /// <summary>  
        /// Interaction logic for MainWindow.xaml  
        /// </summary>  
        public partial class MainWindow : Window  
        {  
            public MainWindow()  
            {  
                InitializeComponent();  
            }  
      
            private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                var doc = txtScreenInfo.Document;  
                doc.Blocks.Clear();  
      
                WindowInteropHelper windowInteropHelper = new WindowInteropHelper(this);  
                Screen currentScreen = Screen.FromHandle(windowInteropHelper.Handle);  
                txtScreenInfo.AppendText("Current Scrren Name: " + currentScreen.DeviceName);  
      
                txtScreenInfo.AppendText(Environment.NewLine);  
                foreach (var item in Screen.AllScreens)  
                {  
                    txtScreenInfo.AppendText("Device Name: " + item.DeviceName);  
                    txtScreenInfo.AppendText(Environment.NewLine);  
                    txtScreenInfo.AppendText("Bounds: " + item.Bounds.ToString());  
                    txtScreenInfo.AppendText(Environment.NewLine);  
                    txtScreenInfo.AppendText("Type: " + item.GetType().ToString());  
                    txtScreenInfo.AppendText(Environment.NewLine);  
                    txtScreenInfo.AppendText("Working Area: " + item.WorkingArea.ToString());  
                    txtScreenInfo.AppendText(Environment.NewLine);  
                    txtScreenInfo.AppendText("Primary Screen: " + item.Primary.ToString());  
                    txtScreenInfo.AppendText(Environment.NewLine);  
                    txtScreenInfo.AppendText("=====================================================");  
                    txtScreenInfo.AppendText(Environment.NewLine);  
                }  
                txtScreenInfo.ScrollToEnd();  
            }  
        }  
    }  
    

    XAML Code:

        <Window x:Class="WpfApp.Netframework.MainWindow"  
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
                xmlns:local="clr-namespace:WpfApp.Netframework"  
                mc:Ignorable="d"  
                Title="MainWindow" Height="800" Width="800">  
            <Grid Height="800">  
                <Grid.RowDefinitions>  
                    <RowDefinition Height="700"></RowDefinition>  
                    <RowDefinition Height="100"></RowDefinition>  
                </Grid.RowDefinitions>  
                <Grid.ColumnDefinitions>  
                    <ColumnDefinition Width="*"/>  
                </Grid.ColumnDefinitions>  
                <Button Content="Get" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="650,0,10,10" Click="Button_Click" Height="30" Width="60"/>  
                <RichTextBox Grid.Column="0" Grid.Row="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="txtScreenInfo" Height="700"  Width="780">  
                    <FlowDocument>  
                        <Paragraph>  
                            <Run/>  
                        </Paragraph>  
                    </FlowDocument>  
                </RichTextBox>  
            </Grid>  
        </Window>  
    

    Run result:

    DISPLAY 1:
    229609-display-1.png

    DISPLAY 2:
    229598-display-2.png

    If right, please Accept.
    Enjoy programming!!!

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.