获取 Delphi (DirectX) 中所有显示器的当前/本机屏幕分辨率

发布于 2024-11-29 23:24:09 字数 1542 浏览 1 评论 0原文

我们的 Delphi 应用程序可以有多个 DirectX 窗口,通常位于多个屏幕上。到目前为止,用户必须使用具有支持的分辨率的下拉列表来指定全屏分辨率。如果他可以使用像“当前”这样的设置(即窗口所在屏幕的分辨率),那就太好了。

我们使用 delphi 和 clootie directX 标头。有人可以给我一个提示,我如何使用 directX、winAPI 或 delphi 方法编写一个方法来获取窗口所在的当前屏幕的分辨率?

亲切的问候, thalm

最终解决方案:

好的,delphi 2007 MultiMon.pas 返回 GetMonitorInfo 的废话,所以我找到了这个方法,它对我有用,直接使用winAPI:

function GetRectOfMonitorContainingRect(const R: TRect): TRect;
{ Returns bounding rectangle of monitor containing or nearest to R }
type
  HMONITOR = type THandle;
  TMonitorInfo = record
    cbSize: DWORD;
    rcMonitor: TRect;
    rcWork: TRect;
    dwFlags: DWORD;
  end;
const
  MONITOR_DEFAULTTONEAREST = $00000002;
var
  Module: HMODULE;
  MonitorFromRect: function(const lprc: TRect; dwFlags: DWORD): HMONITOR; stdcall;
  GetMonitorInfo: function(hMonitor: HMONITOR; var lpmi: TMonitorInfo): BOOL; stdcall;
  M: HMONITOR;
  Info: TMonitorInfo;
begin
  Module := GetModuleHandle(user32);
  MonitorFromRect := GetProcAddress(Module, 'MonitorFromRect');
  GetMonitorInfo := GetProcAddress(Module, 'GetMonitorInfoA');
  if Assigned(MonitorFromRect) and Assigned(GetMonitorInfo) then begin
    M := MonitorFromRect(R, MONITOR_DEFAULTTONEAREST);
    Info.cbSize := SizeOf(Info);
    if GetMonitorInfo(M, Info) then begin
      Result := Info.rcMonitor;
      Exit;
    end;
  end;
  Result := GetRectOfPrimaryMonitor(True);
end;

our delphi application can have multiple DirectX windows, often on multiple screens. up to now the user had to specify the fullscreen resolution using a dropdown with the supported resolutions. it would be very nice, if he could use a setting like 'current' which would be the resolution of the screen on which the window is.

we are using delphi with clootie directX headers. can someone give me a hint, how i would write a method using directX, winAPI or delphi methods to get the resolution of the current screen on which the window is?

kind regards,
thalm

Final Solution:

ok, delphi 2007 MultiMon.pas returns crap for GetMonitorInfo, so i found this method, which works for me, using the winAPI directly:

function GetRectOfMonitorContainingRect(const R: TRect): TRect;
{ Returns bounding rectangle of monitor containing or nearest to R }
type
  HMONITOR = type THandle;
  TMonitorInfo = record
    cbSize: DWORD;
    rcMonitor: TRect;
    rcWork: TRect;
    dwFlags: DWORD;
  end;
const
  MONITOR_DEFAULTTONEAREST = $00000002;
var
  Module: HMODULE;
  MonitorFromRect: function(const lprc: TRect; dwFlags: DWORD): HMONITOR; stdcall;
  GetMonitorInfo: function(hMonitor: HMONITOR; var lpmi: TMonitorInfo): BOOL; stdcall;
  M: HMONITOR;
  Info: TMonitorInfo;
begin
  Module := GetModuleHandle(user32);
  MonitorFromRect := GetProcAddress(Module, 'MonitorFromRect');
  GetMonitorInfo := GetProcAddress(Module, 'GetMonitorInfoA');
  if Assigned(MonitorFromRect) and Assigned(GetMonitorInfo) then begin
    M := MonitorFromRect(R, MONITOR_DEFAULTTONEAREST);
    Info.cbSize := SizeOf(Info);
    if GetMonitorInfo(M, Info) then begin
      Result := Info.rcMonitor;
      Exit;
    end;
  end;
  Result := GetRectOfPrimaryMonitor(True);
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我是男神闪亮亮 2024-12-06 23:24:09
var
  MonInfo: TMonitorInfo;
begin
  MonInfo.cbSize := SizeOf(MonInfo);
  GetMonitorInfo(MonitorFromWindow(Handle, MONITOR_DEFAULTTONEAREST), @MonInfo);
  ShowMessage(Format('Current resolution: %dx%d',
              [MonInfo.rcMonitor.Right - MonInfo.rcMonitor.Left,
               MonInfo.rcMonitor.Bottom - MonInfo.rcMonitor.Top]));
var
  MonInfo: TMonitorInfo;
begin
  MonInfo.cbSize := SizeOf(MonInfo);
  GetMonitorInfo(MonitorFromWindow(Handle, MONITOR_DEFAULTTONEAREST), @MonInfo);
  ShowMessage(Format('Current resolution: %dx%d',
              [MonInfo.rcMonitor.Right - MonInfo.rcMonitor.Left,
               MonInfo.rcMonitor.Bottom - MonInfo.rcMonitor.Top]));
爱要勇敢去追 2024-12-06 23:24:09

请参阅 GetDeviceCaps API 以获取屏幕分辨率。

使用 TCustomForm.Monitor 属性获取 监视表单出现的位置。

See the GetDeviceCaps API to get the screen resolution.

Use the TCustomForm.Monitor property to get the monitor on which the form appears.

塔塔猫 2024-12-06 23:24:09

首先使用 EnumDisplayDevices< /a> 要获取所有监视器名称的列表,请参阅 usenet 帖子了解如何在 Delphi 中执行此操作。请注意,您需要 DeviceName 而不是 DeviceString

然后对于每个显示器使用 EnumDisplaySettings(lpDisplayDevice.DeviceName, ENUM_CURRENT_SETTINGS, lpDevMode) 获取当前设置。这里还可以使用NULL作为设备名称,这意味着:“NULL值指定调用线程正在运行的计算机上的当前显示设备。”。这通常应与用户当前使用的显示器相对应。

First use EnumDisplayDevices to obtain a list of all monitor names, see this usenet post for how to do that in Delphi. Note that you want the DeviceName rather than the DeviceString.

Then for each monitor use EnumDisplaySettings(lpDisplayDevice.DeviceName, ENUM_CURRENT_SETTINGS, lpDevMode) to obtain the current settings. Here you can also use NULL as the device name, which means: "A NULL value specifies the current display device on the computer on which the calling thread is running.". This should usually correspond to the monitor the user is currently on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文