2017-05-08 76 views
-2

Delphi 5沒有內置功能,可以在雙顯示器的情況下在顯示器上設置先前窗體的窗體。爲此,我已導入windows dll。我搜索了這個,發現MonitorFromWindow()MonitorFromPoint()在顯示器上設置Delphi 5 Form,其中以前的窗體出現在雙顯示器系統中

我實施了MonitorFromWindow()但我無法執行MonitorFromPoint()

如何獲取顯示器並在其上設置我的表單?

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 } ; 
+0

具體什麼你的代碼有問題,你已經張貼?它毫不費力地翻譯MonitorFromPoint,所以你怎麼知道你不能這樣做? –

+0

'Function MonitorFromPoint(ptScreenCoords:TPoint; dwFlags:DWORD):HMONITOR; STDCALL;外部用戶32「,它真的不會更容易。你有沒有嘗試過任何東西?我已經多次告訴過你,如果標題翻譯對你來說太難了,那麼你可以使用JEDI庫中的翻譯。忽視錯誤處理也是一個糟糕的政策。不要忽略錯誤。 –

+0

我無法使用JEDI庫,因爲它不允許在我們的應用程序中使用3方庫。 – user7424581

回答

0
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); 
1

MonitorFromWindow()返回HMONITOR,不是HWND。你不能通過HMONITORSetWindowPos(),就像你正在做的那樣。它只接受HWND s。要將HWND定位在特定的監視器上,只需將HWND移動到監視器的virtual screen的矩形內的座標。如果您有以前的HWNDHMONITOR,請使用GetMonitorInfo()檢索顯示器的屏幕矩形,然後您可以使用SetWindowPos()將您的目標HWND定位在該矩形內。

閱讀MSDN瞭解更多詳情:

About Multiple Display Monitors

Positioning Objects on Multiple Display Monitors

+0

請提供Monitorfrompoint的示例代碼。我嘗試過但沒有成功。 @Remy – user7424581

+0

@ user7424581如果您在做某事時遇到麻煩,請[編輯]您的問題以顯示您所嘗試的內容,並解釋您對它的期望以及爲什麼它不適合您。 –

+0

實際上,我不知道如何在正確的顯示器上將窗體打開之前做什麼。我知道在哪裏調用這些函數,但不知道如何編寫它們。請幫助@Remy – user7424581

相關問題