retMonitorTools Unit 1.0

          
 Version 1.0

 

 Download 

 

  

A unit with the following functions:

 

unit retmonitortools;

//  ============================================================================
//  retMonitorTools Unit for Delphi
//  version 2.0 : rewritten everything
//  ============================================================================
//  Author : retnyg @ http://krazz.net/retnyg
//
//  thanks to muetze1 who supported me with the development of this unit.
//
//  demo code:
//

(*
procedure TForm1.Button1Click(Sender: TObject);
begin
  caption := inttostr(GetNumberMonitors);
  if GetMonitorFromWindow(handle) = 0 then
    Movewindowtomonitor(handle,1,false)
  else
    Movewindowtomonitor(handle,0,false)
end;
*)

//  ============================================================================

interface
uses windows;
const
 DLL = 'User32.dll';
 MONITOR_DEFAULTTONULL = $0;    //If the monitor is not found, return 0
 MONITOR_DEFAULTTOPRIMARY = $1; //If the monitor is not found, return the primary monitor
 MONITOR_DEFAULTTONEAREST = $2; //If the monitor is not found, return the nearest monitor

type

  HMONITOR = integer;

  PMonitor = ^TMonitor;
  TMonitor = record
    handle: HMONITOR;
    rect: TRect;
  end;

  PMonitors = ^TMonitors;
  TMonitors = array of TMonitor;

  PMONITORINFO = ^TMONITORINFO;
  TMONITORINFO = record
    cbSize:DWORD;
    rcMonitor:TRECT;
    rcWork:TRECT;
    dwFlags:DWORD;
  end;

// own functions
 function getDesktopDimensionsXY: tpoint;
 function GetNumberMonitors:byte;
 function moveWindowToMonitor(hnd: HWND; monNum:byte; fullscreen: boolean = false):boolean;
 function GetMonitorFromWindow(hnd: HWND):byte;
// function GetDesktopDimensions:Trect;


implementation

var Screens: TMonitors = nil;

// imported API functions
 function GetMonitorInfo(AMonitorHandle: pointer; Var ADataRecord: TMonitorInfo): Boolean;
          stdcall; External DLL Name 'GetMonitorInfoA'; overload;
 function MonitorFromPoint(APoint: TPoint; AFlags: DWORD): pointer;
          stdcall; External DLL;
 function MonitorFromWindow(AWindowHandle: HWND; AFlags: DWORD): pointer;
          stdcall; External DLL;
 function EnumDisplayMonitors(dc: HDC; lprcClip:Prect; lpfnEnum: Pointer; dwData: LPARAM):boolean;
          stdcall; External DLL;


procedure GetMonitors;

  function EnumMonitorsProc(hm: HMONITOR; dc: HDC; r: PRect; Data: Pointer): Boolean; stdcall;
  var
    l:integer;
  begin
    l := length(Screens);
    setlength(Screens, l + 1);
    Screens[l].handle := HM;
    Screens[l].rect := r^;
    Result := True;
  end;

begin
   if Screens = nil then
     EnumDisplayMonitors(0,nil,@EnumMonitorsProc,0);
end;

function Point(X, Y: Integer): TPoint;
begin
  Result.X := X;
  Result.Y := Y;
end;

function getDesktopDimensionsXY: tpoint;
begin
   result := point(getsystemmetrics(SM_CXVIRTUALSCREEN),getsystemmetrics(SM_CYVIRTUALSCREEN));
end;

function GetNumberMonitors:byte;
Const
  SM_CMONITORS = 80;
begin
  Result := GetSystemMetrics(SM_CMONITORS);
end;

function moveWindowToMonitor(hnd: HWND; monNum:byte; fullscreen: boolean = false):boolean;

  function difF(a,b:integer):integer;
  begin
     if a > b then result := a - b
     else result := b -a ;
  end;

var dr: trect;

    X,Y,W,H:integer;
    l : integer;
    CurrentScreen, NewScreen: PMonitor;
    cSn: integer;

begin
   result:=false;
   if screens = nil then Getmonitors;
   l := length(screens);
   cSn := GetMonitorFromWindow(hnd);
   if  (cSn < L) and (monNum < L) and (cSn <> monNum) then
     if GetwindowRect(hnd,dr) then begin
        CurrentScreen := @Screens[cSn];
        NewScreen := @Screens[monNum];
        if fullscreen then begin
          X:=NewScreen^.rect.Left;
          Y:=NewScreen^.rect.Top;
          W:=abs(diff(NewScreen^.rect.Left,NewScreen^.rect.Right));
          H:=abs(diff(NewScreen^.rect.Top,NewScreen^.rect.Bottom));
        end else begin
          x := NewScreen^.rect.Left + abs(CurrentScreen^.rect.left - dr.Left);
          y := NewScreen^.rect.top  + abs(CurrentScreen^.rect.top - dr.top);
          w := diff(dr.Left, dr.Right);
          h := diff(dr.Top, dr.Bottom);
        end;
        if movewindow(hnd, X, Y, W, H,true) then
           result := true;
     end;
end;

function GetMonitorFromWindow(hnd: HWND):byte;
var HM : HMONITOR;
    i : integer;
begin
   result:=0;
   HM := integer(MonitorFromWindow(hnd, MONITOR_DEFAULTTONEAREST ));
   if Screens = nil then GetMonitors;
   for i := 0 to length(screens)-1 do
     if screens[i].handle = HM then begin
       result := i;
       break;
     end;
end;


end.

{function GetDesktopDimensions:Trect;
var DC: HDC;
    Cn: TCanvas;
begin
   DC:=GetDC(0);
   Cn:=TCanvas.Create;
   cn.Handle:=DC;
   Result:=cn.ClipRect;
   cn.Free;
   ReleaseDc(0,DC);
end; }