Get Monitor Information : Native Windows Function « Windows « C# / CSharp Tutorial






/*        
Revised from 

NET Development for Java Programmers

*/

using System;
using System.Runtime.InteropServices;

[ StructLayout( LayoutKind.Explicit ) ]
struct Point
{
  [ FieldOffset( 0 ) ]
  public int x;
  [ FieldOffset( 4 ) ]
  public int y;
}

[ StructLayout( LayoutKind.Sequential ) ]
struct Rect
{
  public int left;
  public int top;
  public int right;
  public int bottom;
}

[ StructLayout( LayoutKind.Sequential ) ]
struct MonitorInfo
{
  public uint size;
  public Rect monitor;
  public Rect work;
  public uint flags;
}

class MainClass
{
  [ DllImport( "user32.dll" ) ]
  static extern IntPtr MonitorFromPoint( Point p, uint flags );

  [ DllImport( "user32.dll" ) ]
  static extern bool GetMonitorInfo( IntPtr hmon, ref MonitorInfo mi );

  [STAThread]
  static void Main(string[] args)
  {
    Point p = new Point();
    p.x = 1;
    p.y = 1;
    IntPtr hmon = MonitorFromPoint( p, 1 );

    MonitorInfo mi = new MonitorInfo();
    mi.size = (uint)Marshal.SizeOf( mi );
        bool success = GetMonitorInfo( hmon, ref mi );

    Console.WriteLine(mi.size);
    Console.WriteLine(mi.monitor);
    
  }
}
40
Rect








29.10.Native Windows Function
29.10.1.Calling Native DLL Functions
29.10.2.Calling a Function with a Structure Parameter
29.10.3.Enumerate Display Monitors
29.10.4.Get Workstation information
29.10.5.Get Computer name (char * parameter)
29.10.6.Get free disk space
29.10.7.Use native windows function to read file
29.10.8.The windows version information
29.10.9.Get current Active Window
29.10.10.Writing INI file: Write Private Profile String
29.10.11.Reading INI file: Get Private Profile String
29.10.12.GetVersionEx by using kernel32.dll
29.10.13.Get computer name (StringBuilder parameter)
29.10.14.Lock work station
29.10.15.Get Monitor Information