项目 Project1.exe 引发异常 class ERangeError 消息 'Range check error'

Project Project1.exe raised exception class ERangeError with message 'Range check error'

我正在读一本关于 Windows API 的 Delphi 旧书。这是其中的一个例子。

unit Unit1;

interface

uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
    System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
    TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
    private
        { Private declarations }
    public
        { Public declarations }
    end;

var
    Form1: TForm1;

implementation

{$R *.dfm}

{ Register the Window Class }
function RegisterClass: Boolean;
var
    WindowClass: TWndClass;
begin
    { setup our new window class }
    WindowClass.Style := CS_HREDRAW or CS_VREDRAW; { set the class styles }
    WindowClass.lpfnWndProc := @DefWindowProc;     { point to the default window procedure }
    WindowClass.cbClsExtra := 0;                   { no extra class memory }
    WindowClass.cbWndExtra := 0;                   { no extra window memory }
    WindowClass.hInstance := hInstance;            { the application instance }
    WindowClass.hIcon := 0;                        { no icon specified }
    WindowClass.hCursor := 0;                      { no cursor specified }
    WindowClass.hbrBackground := COLOR_WINDOW;     { use a predefined color }
    WindowClass.lpszMenuName := nil;               { no menu }
    WindowClass.lpszClassName := 'TestClass';      { the registered class name }

    { now that we have our class set up, register it with the system }
    Result := Winapi.Windows.RegisterClass(WindowClass) <> 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
    hWindow: HWND;
begin
    { Step 1: Register our new window class }
    if not RegisterClass then
    begin
        ShowMessage('RegisterClass failed');
        Exit;
    end;

    { Step 2: Create a window based on our new class }
    hWindow := CreateWindowEx(0, { no extended styles }
        'TestClass',             { the registered class name }
        'New Window',            { the title bar text }
        WS_OVERLAPPEDWINDOW,     { a normal window style }
        CW_USEDEFAULT,           { default horizontal position }
        CW_USEDEFAULT,           { default vertical position }
        CW_USEDEFAULT,           { default width }
        CW_USEDEFAULT,           { default height }
        0,                       { no owner window }
        0,                       { no menu }
        hInstance,               { the application instance }
        nil                      { no additional information }
        );

    { Step 3: If our window was created successfully, display it }
    if hWindow <> 0 then
    begin
        ShowWindow(hWindow, SW_SHOWNORMAL);
        UpdateWindow(hWindow);
    end
    else
    begin
        ShowMessage('CreateWindow failed');
        Exit;
    end;
end;

end.

UI 很简单。我只是拖一个按钮并将其放在窗体上。但是,当我 运行 使用 Embarcadero® Delphi 11.0 Version 28.0.42600.6491 的程序并单击表单上的按钮时,我得到 Project Project1.exe raised exception class ERangeError with message 'Range check error'..之后,我收到了这个错误。

谁能帮忙看看是哪里出了问题?

顺便说一句,我刚刚尝试了 Delphi 7,程序运行正常。当我单击该按钮时,会弹出一个空的 window。

CW_USEDEFAULT 常量的值与 CreateWindowEx 函数的声明不太一致。 将以下代码添加到 Button1Click 的顶部,靠近 var 部分:

const
  CW_USEDEFAULT = -MaxInt - 1;