2017-05-04 55 views
-2

我知道我會爲此投票。但我仍然發佈這個問題如何轉換delphi代碼從c + +代碼

任何一個請在delphi中轉換下面的兩個函數代碼。

#include <windows.h> 
#include "multimon.h"  

#define MONITOR_CENTER  0x0001  // center rect to monitor 
#define MONITOR_CLIP  0x0000  // clip rect to monitor 
#define MONITOR_WORKAREA 0x0002  // use monitor work area 
#define MONITOR_AREA  0x0000  // use monitor entire area 

// 
// ClipOrCenterRectToMonitor 
// 
// The most common problem apps have when running on a 
// multimonitor system is that they "clip" or "pin" windows 
// based on the SM_CXSCREEN and SM_CYSCREEN system metrics. 
// Because of app compatibility reasons these system metrics 
// return the size of the primary monitor. 
// 
// This shows how you use the multi-monitor functions 
// to do the same thing.  

    void ClipOrCenterRectToMonitor(LPRECT prc, UINT flags) 
    { 
     HMONITOR hMonitor; 
     MONITORINFO mi; 
     RECT  rc; 
     int   w = prc->right - prc->left; 
     int   h = prc->bottom - prc->top; 

     // 
     // get the nearest monitor to the passed rect. 
     // 
     hMonitor = MonitorFromRect(prc, MONITOR_DEFAULTTONEAREST); 

     // 
     // get the work area or entire monitor rect. 
     // 
     mi.cbSize = sizeof(mi); 
     GetMonitorInfo(hMonitor, &mi); 

     if (flags & MONITOR_WORKAREA) 
      rc = mi.rcWork; 
     else 
      rc = mi.rcMonitor; 

     // 
     // center or clip the passed rect to the monitor rect 
     // 
     if (flags & MONITOR_CENTER) 
     { 
      prc->left = rc.left + (rc.right - rc.left - w)/2; 
      prc->top = rc.top + (rc.bottom - rc.top - h)/2; 
      prc->right = prc->left + w; 
      prc->bottom = prc->top + h; 
     } 
     else 
     { 
      prc->left = max(rc.left, min(rc.right-w, prc->left)); 
      prc->top = max(rc.top, min(rc.bottom-h, prc->top)); 
      prc->right = prc->left + w; 
      prc->bottom = prc->top + h; 
     } 
    } 

    void ClipOrCenterWindowToMonitor(HWND hwnd, UINT flags) 
    { 
     RECT rc; 
     GetWindowRect(hwnd, &rc); 
     ClipOrCenterRectToMonitor(&rc, flags); 
     SetWindowPos(hwnd, NULL, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); 
    } 

流動我想:

unit Unit1; 

interface 

uses 
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; 

type 
    TForm1 = class(TForm) 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.DFM} 

procedure ClipOrCenterRectToMonitor(prc:Integer ; flags:UINT); 
    type 
    HMONITOR = type THandle; 
var 

    // hMonitor : HMONITOR ; 
    mi : MONITORINFO ; 
    rc : RECT  ; 
    w : Integer; 
    h : Integer; 

begin 
    w := prc->right - prc->left; 
    h := prc->bottom - prc->top; 


    // 
    // get the nearest monitor to the passed rect. 
    // 
    hMonitor := MonitorFromRect(prc, MONITOR_DEFAULTTONEAREST); 

    // 
    // get the work area or entire monitor rect. 
    // 
    mi.cbSize := sizeof(mi); 
    GetMonitorInfo(hMonitor, &mi); 

    if (flags & MONITOR_WORKAREA) then 
     rc := mi.rcWork; 
    else 
     rc := mi.rcMonitor; 

    // 
    // center or clip the passed rect to the monitor rect 
    // 
    if (flags & MONITOR_CENTER) then 
    begin 
     prc->left := rc.left + (rc.right - rc.left - w)/2; 
     prc->top := rc.top + (rc.bottom - rc.top - h)/2; 
     prc->right := prc->left + w; 
     prc->bottom := prc->top + h; 
    end; 
    else 
    begin 
     prc->left := max(rc.left, min(rc.right-w, prc->left)); 
     prc->top := max(rc.top, min(rc.bottom-h, prc->top)); 
     prc->right := prc->left + w; 
     prc->bottom := prc->top + h; 
    end; 
    end; 

procedure ClipOrCenterWindowToMonitor(hwand:HWND; flags:UINT); 
var 
    rd:RECT ; 
begin 

    GetWindowRect(hwnd, &rc); 
    ClipOrCenterRectToMonitor(&rc, flags); 
    SetWindowPos(hwnd, NULL, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); 
    end; 




end. 

,但這是不完整的。請任何人這樣做。

+5

這不是SO的工作原理。這不是代碼翻譯服務。 –

回答

5

以下是您用作基準的MSDN文章Positioning Objects on a Multiple Display Setup (Windows)的C代碼的直接翻譯。它在D2007和德爾福柏林都編譯時沒有錯誤或警告,儘管我沒有測試過它是否能夠實際運行MSDN代碼。

uses 
    Windows, MultiMon, Math; 

procedure ClipOrCenterRectToMonitor(var Prc: TRect; var Flags: UINT); 
var 
    hMon: HMONITOR; 
    mi: MONITORINFO; 
    Rect: TRect; 
    w, h: Integer; 
const 
    MONITOR_CENTER = $0001;  // center rect to monitor 
    MONITOR_CLIP = $0000;  // clip rect to monitor 
    MONITOR_WORKAREA = $0002;  // use monitor work area 
    MONITOR_AREA = $0000;  // use monitor entire area 

begin 
    w := Prc.Right - Prc.Left; 
    h := Prc.Bottom - Prc.Top; 

    hMon := MonitorFromRect(@Prc, Flags); 

    mi.cbSize := SizeOf(mi); 
    GetMonitorInfo(hMon, @mi); 
    if (flags and MONITOR_WORKAREA) <> 0 then 
    Rect := mi.rcWork 
    else 
    Rect := mi.rcMonitor; 
    if (flags and MONITOR_CENTER) <> 0 then 
    begin 
    Prc.Left := Rect.Left + (Rect.Right - Rect.Left - w) div 2; 
    Prc.Top := Rect.Top + (Rect.Bottom - Rect.Top - h) div 2; 
    Prc.Right := Prc.Left + w; 
    Prc.Bottom := Prc.Top + h; 
    end 
    else 
    begin 
    Prc.Left := Max(Rect.left, Min(Rect.Right - w, Rect.Left)); 
    Prc.Top := Max(Rect.Top, Min(Rect.Bottom - h, Rect.Top)); 
    Prc.Right := Prc.Left + w; 
    Prc.Bottom := Prc.Top + h; 
    end; 
end; 

procedure ClipOrCenterWindowToMonitor(Wnd: HWND; Flags: UINT); 
var 
    Rect: TRect; 
begin 
    GetWindowRect(Wnd, Rect); 
    ClipOrCenterRectToMonitor(Rect, Flags); 
    SetWindowPos(Wnd, 0, Rect.Left, Rect.Top, 0, 0, SWP_NOSIZE or SWP_NOZORDER or SWP_NOACTIVATE); 
end;