Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6002239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:56:58+00:00 2026-05-23T00:56:58+00:00

I have to implement a feature where the last position of the window is

  • 0

I have to implement a feature where the last position of the window is saved. When the application starts up this position needs to be obtained and restored.

Now it could be that a second monitor is dismantled. If the last position is on a now non-visible monitor (in other words the saved coordinates are outside the visible coordinates), this case should be caught and the coordinates shall be set to the default rather than last position.

In order to retrieve the information about monitors I need to use Win32. It is not easy for me to make this work.

I have created a Helper CLass:

public static class DisplayHelper
    {
        private const int MONITOR_DEFAULTTONEAREST = 2;

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern int GetSystemMetrics(int nIndex);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern UInt32 MonitorFromPoint(Point pt, UInt32 dwFlags);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern bool GetMonitorInfo(UInt32 monitorHandle, ref MonitorInfo mInfo);


        public static void GetMonitorInfoNow(MonitorInfo mi, Point pt)
        {
            UInt32 mh = MonitorFromPoint(pt, 0);
            mi.cbSize = (UInt32)System.Runtime.InteropServices.Marshal.SizeOf(typeof(MonitorInfo));
            mi.dwFlags = 0;
            bool result = GetMonitorInfo(mh, ref mi);

        }
    }

And these are my attempts to create the MonitorInfo and Rect classes:

[StructLayout(LayoutKind.Sequential)]
    public class MonitorInfo
    {
        public UInt32 cbSize;
        public Rectangle2 rcMonitor;
        public Rectangle2 rcWork;
        public UInt32 dwFlags;

        public MonitorInfo()
        {
            rcMonitor = new Rectangle2();
            rcWork = new Rectangle2();

            cbSize = (UInt32)System.Runtime.InteropServices.Marshal.SizeOf(typeof(MonitorInfo));
            dwFlags = 0;
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public class Rectangle2
    {
        public UInt64 left;
        public UInt64 top;
        public UInt64 right;
        public UInt64 bottom;

        public Rectangle2()
        {
            left = 0;
            top = 0;
            right = 0;
            bottom = 0;
        }
    }

I am using this code like this to obtain the visible monitors:

//80 means it counts only visible display monitors.
int lcdNr = DisplayHelper.GetSystemMetrics(80);
var point = new System.Drawing.Point((int) workSpaceWindow.Left, (int) workSpaceWindow.Top);
MonitorInfo monitorInfo = new MonitorInfo();
DisplayHelper.GetMonitorInfoNow(monitorInfo, point);

The last method throws an exception when trying to execute

bool result = GetMonitorInfo(mh, ref mi);

Any suggestions what I need to do to fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:56:59+00:00Added an answer on May 23, 2026 at 12:56 am

    Rather than calling a native API, you should use System.Windows.Forms.Screen. It should have everything you need, and be much easier to use.

    Screen.FromPoint is the managed equivalent of your GetMonitorInfoNow function with the MONITOR_DEFAULTTONEAREST option. I just noticed you aren’t using that option, so you may have to write your own or use the correct P/Invoke signatures.

    Writing your own should be fairly simple, if you just reference System.Drawing and System.Windows.Forms. Both of these should work:

    static Screen ScreenFromPoint1(Point p)
    {
        System.Drawing.Point pt = new System.Drawing.Point((int)p.X, (int)p.Y);
        return Screen.AllScreens
                        .Where(scr => scr.Bounds.Contains(pt))
                        .FirstOrDefault();
    }
    
    static Screen ScreenFromPoint2(Point p)
    {
        System.Drawing.Point pt = new System.Drawing.Point((int)p.X, (int)p.Y);
        var scr = Screen.FromPoint(pt);
        return scr.Bounds.Contains(pt) ? scr : null;
    }
    

    If you prefer to make the Win32 calls yourself, the proper P/Invoke signatures (i.e. what you’d get from decompiling the .Net DLL) for the functions you need to call are:

        [DllImport("User32.dll", CharSet=CharSet.Auto)] 
        public static extern bool GetMonitorInfo(HandleRef hmonitor, [In, Out]MONITORINFOEX info);
        [DllImport("User32.dll", ExactSpelling=true)]
        public static extern IntPtr MonitorFromPoint(POINTSTRUCT pt, int flags);
    
        [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto, Pack=4)]
        public class MONITORINFOEX { 
            public int     cbSize = Marshal.SizeOf(typeof(MONITORINFOEX));
            public RECT    rcMonitor = new RECT(); 
            public RECT    rcWork = new RECT(); 
            public int     dwFlags = 0;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)] 
            public char[]  szDevice = new char[32];
        }
    
        [StructLayout(LayoutKind.Sequential)]
        public struct POINTSTRUCT { 
            public int x;
            public int y;
            public POINTSTRUCT(int x, int y) {
              this.x = x; 
              this.y = y;
            } 
        } 
    
        [StructLayout(LayoutKind.Sequential)] 
        public struct RECT {
            public int left; 
            public int top; 
            public int right;
            public int bottom; 
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an application. I have to implement a bookmark feature, and adding
I have implement GridView Row Editing feature in my .net application using <asp:CommandField .
I have to implement a new feature in my existing JSF application. As it
I need help and tips on how to implement this feature. I have a
I have an app that includes a search feature. This feature is implemented by
We have this huge application that has 18 projects in our source control (
Ive been working on a feature of my application to implement a leaderboard -
I have a drawing app in which I want to implement UNDO feature so
For my pages which implement feature X I have them implement a base class
I have to implement a application which displays a grid view of thubmnails each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.