获取 Delphi (DirectX) 中所有显示器的当前/本机屏幕分辨率
我们的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅 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.
首先使用
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 theDeviceName
rather than theDeviceString
.Then for each monitor use
EnumDisplaySettings
(lpDisplayDevice.DeviceName, ENUM_CURRENT_SETTINGS, lpDevMode)
to obtain the current settings. Here you can also useNULL
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.