【发布时间】:2017-05-08 12:31:12
【问题描述】:
Delphi 5 没有内置功能来在双显示器的情况下在显示器上打开以前的表格时设置表格。为此,我导入了windows dll。我搜索了这个,发现MonitorFromWindow()和MonitorFromPoint()。
我实现了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 } ;
【问题讨论】:
-
您发布的代码有什么具体问题?翻译 MonitorFromPoint 不费力,那你怎么知道你做不到呢?
-
function MonitorFromPoint(ptScreenCoords: TPoint; dwFlags: DWORD): HMONITOR; stdcall; external user32真的再简单不过了。你有没有尝试过任何东西?我已经告诉过你很多次了,如果标题翻译对你来说太难了,那么你可以使用 JEDI 库中的翻译。忽略错误处理也是一个糟糕的策略。不要忽略错误。 -
我不能使用 JEDI 库,因为它不允许在我们的应用程序中使用 3 方库。
-
哦,看在上帝的份上。阅读那里的代码,然后复制您需要的内容。但是好的,现在我看到你已经做到了。不过你做错了。它返回一个 HMONITOR,而不是一个 HWND。您缺乏错误检查正在杀死您。为什么不尝试学习和理解其中的一些代码。
-
public void Concatenate([MarshalAs(UnmanagedType.IUnknown)] Form f1) //LPWStr { var screen1 = Screen.FromPoint(Cursor.Position); f1.StartPosition = FormStartPosition.Manual; f1.Left = screen1.Bounds.Left + screen1.Bounds.Width / 2 - f1.Width / 2; f1.Top = screen1.Bounds.Top + screen1.Bounds.Height / 2 - f1.Height / 2; } 我在 c# 中尝试过,但无法在 delphi 5 中实现。请帮助@Ken White sir