Delphi 5 does not have a built-in function to set a Form on a monitor where a previous Form is opened in the case of dual monitors. For this, I have imported windows dll
. I searched for this and found MonitorFromWindow()
and MonitorFromPoint()
.
I implemented MonitorFromWindow()
but am not able to implement MonitorFromPoint()
.
How do I get the monitor and set my Form on it?
function MonitorFromWindow(hwnd: HWND; dwFlags: DWORD): HWND; stdcall; external 'User32.dll';
procedure TSmForm.AfterCreateForm(Session: ISmSession; SmHelpContext: TDM_Int32; IsDLL: Boolean);
type
HMONITOR = type THandle;
var
MBMonitor: HMONITOR;
const
MONITOR_DEFAULTTONEAREST = $00000002;
begin
//If you decide to remove the next two lines, make sure no one use this function and assume init of SmSession is here (like ScriptMaintanance etc.).
if SmSession<>Session then
SmSession := Session;
if SmHelpContext > 0 then
HelpContext := SmHelpContext;
//Following lines ensure that if the form resides in a dll, its icon is the same as the host application's
if (IsDLL) then
begin
if (Icon.Empty) and (ParentHWND <> 0) then
SendMessage(Handle, WM_SETICON, 1, SendMessage(ParentHWND, WM_GETICON, 1, 0));
end;
MBMonitor := MonitorFromWindow(Self.Handle, MONITOR_DEFAULTTONEAREST);
//HWND_NOTOPMOST
SetWindowPos(Self.Handle, MBMonitor, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);
end { of TSmForm.AfterCreateForm } ;
Monitor:= screen.Forms[Screen.FormCount-1].Monitor;
Self.Left := Monitor.Left + ((Monitor.Width - Self.Width) div 2);
Self.Top := Monitor.Top + ((Monitor.Height - Self.Height) div 2);