如何将delphi代码从c++;代码

如何将delphi代码从c++;代码,delphi,Delphi,我知道我会为此投赞成票。但我仍然在发布这个问题 请大家在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_WO

我知道我会为此投赞成票。但我仍然在发布这个问题

请大家在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);
    }

但这并不是完整的。请任何人都这样做。

这里是您用作基础的MSDN文章中C代码的直接翻译。它在D2007和Delphi Berlin上编译时都没有错误或警告,尽管我还没有测试它看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;
使用
Windows、多任务、数学;
程序CLIPORCENTERRECTOMONITOR(变量Prc:TRCT;变量标志:UINT);
变量
hMon:HMONITOR;
mi:MONITORINFO;
Rect:TRect;
w、 h:整数;
常数
监控中心=$0001;//要监视的中心rect
监视器剪辑=$0000;//clip rect到监视器
MONITOR_工作区=$0002;//使用监控工作区
监控面积=$0000;//使用监视器监视整个区域
开始
w:=Prc.右-Prc.左;
h:=中华人民共和国底部-中华人民共和国顶部;
hMon:=监视器FROMRECT(@Prc,标志);
mi.cbSize:=SizeOf(mi);
GetMonitorInfo(hMon,@mi);
如果(标记和监视工作区)为0,则
Rect:=mi.rcWork
其他的
Rect:=mi.rcMonitor;
如果(标志和监控中心)为0,则
开始
Prc.Left:=矩形左+(矩形右-矩形左-w)第2部分;
Prc.Top:=Rect.Top+(Rect.Bottom-Rect.Top-h)第2部分;
Prc.右:=Prc.左+w;
Prc.底部:=Prc.顶部+h;
结束
其他的
开始
Prc.Left:=最大值(正左,最小值(正右-w,正左));
Prc.Top:=最大值(矩形顶部,最小值(矩形底部-h,矩形顶部));
Prc.右:=Prc.左+w;
Prc.底部:=Prc.顶部+h;
结束;
结束;
程序ClipEnterWindowToMonitor(Wnd:HWND;标志:UINT);
变量
Rect:TRect;
开始
GetWindowRect(Wnd,Rect);
CLIPORCENTERRECTOMONITOR(Rect,标志);
设置窗口位置(Wnd、0、矩形左、矩形上、0、0、SWP_NOSIZE或SWP_NOZORDER或SWP_NOACTIVATE);
结束;

这不是它的工作原理。这不是代码翻译服务。此例程不应这样使用。它将错误的标志(在MultiMon.pas中以类似于常量的“MONITOR_u2;”前缀混淆命名)传递给MonitorFromRect(),并且不检查GetMonitorInfo()或GetWindowRect()的返回值。微软的示例代码“在多显示器设置中定位对象”中的示例代码也没有
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;