unit Win32;interfaceuses
Windows,Messages,SysUtils;const
AppName = 'HelloWorld';
type
//定义一个自己的类
TMyClass = class
public
//有一个窗体注册函数
function WinRegister:Boolean;
//创建窗体句柄函数
function WinCreate:HWND;
//执行窗体函数
function ExecWin(hWnd:HWND):LongInt;
//消息循环函数
function WindowProc(hWnd:HWND;
aMessage:UINT;
wParam,lParam:LongInt):LongInt;stdcall;export;
//消息销毁函数
procedure On_Destroy(hWnd: HWnd; var Meg: TWMDestroy);
end;implementationfunction TMyClass.WinRegister:Boolean;
var 
WCL: TWndClass; 
begin 
WCL.hInstance := hInstance; 
with WCL do 
begin 
Style := CS_HREDRAW or CS_VREDRAW; 
lpfnWndProc := @WindowProc;
hIcon := LoadIcon( hInstance, IDI_APPLICATION ); 
hCursor := LoadCursor( 0, IDC_ARROW ); 
hbrBackGround := hBrush( COLOR_WINDOW ); 
lpszMenuName := 'FontMenu'; 
lpszClassName := AppName; 
cbClsExtra := 0; 
cbWndExtra := 0; 
end; 
Result := RegisterClass( WCL ) <> 0; // 登记窗口类 API 
end;function TMyClass.WinCreate:HWND;
begin 
Result := CreateWindow( AppName, 'Win32 SDK for Delphi', 
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 
625, 275, HWND_DESKTOP, 0, hInstance, nil ); 
end;function TMyClass.ExecWin(hWnd:HWND):LongInt;
var
aMessage: TMsg;
begin
ShowWindow( hWnd, CmdShow ); // 显示窗口 API
UpdateWindow( hWnd ); // 重新显示窗口 API
while GetMessage( aMessage, 0, 0, 0 ) do // 取消息 API
begin
TranslateMessage( aMessage ); // 转换字符消息 API 
DispatchMessage( aMessage ); // 发送消息到指定窗口 API
end;
Result := aMessage.wParam; 
end;function TMyClass.WindowProc(hWnd:HWND;
aMessage:UINT;
wParam,lParam:LongInt):LongInt;stdcall;export;
var
aMeg: TMessage;
begin
aMeg.Msg := aMessage;
aMeg.WParam := wParam;
aMeg.LParam := lParam;
aMeg.Result := 0;
case aMessage of
//WM_CREATE: On_Create( hWindow, TWMCreate( aMeg ) );
WM_DESTROY: On_Destroy( hWnd, TWMDestroy( aMeg ) );
//WM_COMMAND: On_Command( hWindow, TWMCommand( aMeg ) );
//WM_NEWFONT: NewFont( lParam );
//WM_STARTFONTS: StartFont( hWindow );
// 调用缺省的窗口函数 API
else aMeg.Result := DefWindowProc( hWnd, aMessage, wParam, lParam );
end;
Result := aMeg.Result;
end;procedure TMyClass.On_Destroy(hWnd: HWnd; var Meg: TWMDestroy);
begin
PostQuitMessage(0);
end;end.
我只想显示一个窗口,不要显示什么Font文字什么的,所以我注释了一些,一下是错误:
[Error] Win32.pas(38): Variable required
[Warning] Win32.pas(53): Constant expression violates subrange bounds
[Warning] Win32.pas(53): Constant expression violates subrange bounds
[Warning] Win32.pas(61): Symbol 'CmdShow' is specific to a platform

解决方案 »

  1.   

    路过,Variable required
    就一个错误,可能你调用哪个函数 传递的需要变量,你传了常量
      

  2.   


    lpfnWndProc := @WindowProc;
    为:
    lpfnWndProc := @TMyClass.WindowProc;
    就可以编译了
      

  3.   


    刚好我实现了一个 给你吧  直接在工程文件里 复制 粘贴program Project1;uses
      Forms,
      Windows,
      SysUtils,
      Messages,
      Dialogs;type
      TMyForm = class(TObject)
      private
        winclass:WNDCLASSA;
        hwindow:HWND;
        Amsg:TMsg;
        FAppName:string;
        FWndproc:TFNAPCProc;
        function winregister():Boolean;
        procedure CreateMyWindow;
      public
        constructor Create;
        destructor Destroy;override;
        procedure WinCreate;
        procedure MyRun;
        procedure CreateButton(x,y:Integer;className,windowName:string);
        property ApplicationName : string  read FAppName write FAppName;
        property MyProcedure : TFNAPCProc read FWndproc write FWndproc;
      end;const
      AppName = 'Myclass';var
      Mywindow:TMyForm;
    { TMyForm }constructor TMyForm.Create;
    beginend;procedure TMyForm.CreateButton(x,y:Integer;className,windowName:string);
    var
      hMyButton:HWND;
    begin
      hMyButton:=CreateWindow(PAnsiChar(className),PAnsiChar(windowName),WS_VISIBLE or WS_Child or BS_Bitmap,
        x,y,65,24,hwindow,0,system.MainInstance,nil);
      ShowWindow(hMyButton,CmdShow);
      ShowWindow(hMyButton,SW_SHOWNORMAL);
      UpdateWindow(hMyButton);
    end;procedure TMyForm.CreateMyWindow;
    begin
      hwindow := CreateWindowEx(WS_EX_CLIENTEDGE,PAnsiChar(FAppName),AppName,WS_OVERLAPPEDWINDOW,100,
      100,600,400,0,0,SYSTEM.MainInstance,nil); // hwindow := CreateWindow(AppName,'创建窗口',WS_OVERLAPPED,CW_USEDEFAULT,
     //   CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,SYSTEM.MainInstance,nil);
      if hwindow <> 0 then
      begin
        ShowWindow(hwindow,CmdShow);
        ShowWindow(hwindow,SW_SHOWNORMAL);
        UpdateWindow(hwindow);
        CreateButton(75,175,'BUTTON','Mybutton1');
        CreateButton(75,275,'BUTTON','Mybutton2');
      end;
    end;destructor TMyForm.Destroy;
    begin  inherited;
    end;procedure TMyForm.MyRun;
    begin
      while GetMessage(Amsg,0,0,0) do
      begin
        TranslateMessage(Amsg);
        DispatchMessage(Amsg);
      end;
      Halt(Amsg.wParam);
    end;procedure TMyForm.WinCreate;
    begin
      if winregister then
      begin
        CreateMyWindow;
      end;
    end;function TMyForm.winregister: Boolean;
    begin
      winclass.style := CS_HREDRAW or CS_VREDRAW;
      winclass.lpfnWndProc := FWndproc;
      winclass.cbClsExtra := 0;
      winclass.cbWndExtra := 0;
      winclass.hInstance := System.MainInstance;
      winclass.hIcon := LoadIcon(0,IDI_APPLICATION);
      winclass.hCursor := LoadCursor(0,IDC_ARROW);
      winclass.hbrBackground := GetStockObject(WHITE_BRUSH);
      winclass.lpszMenuName := nil;
      winclass.lpszClassName := PChar(FAppName);
      Result := RegisterClass(winclass)<> 0;
    end;function  WindowProc(hwnd:HWND;umsg:UINT;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;export;
    var
      dc:HDC;
      ps:TPaintStruct;
      r:TRect;
      Curxy:TPoint;
    begin
      WindowProc := 0 ;
      case umsg of
      WM_PAINT:
      begin
        dc := BeginPaint(hwnd,ps);
        GetClientRect(hwnd,r);
        DrawText(dc,'哈哈哈',-1,r,DT_SINGLELINE or DT_CENTER or DT_VCENTER);
        EndPaint(hwnd,ps);
        Exit;
      end;
      WM_DESTROY:
      begin
        PostQuitMessage(0);
        Exit;
      end;
      WM_COMMAND:
      begin
        if GetCursorPos(Curxy) then// 获取鼠标坐标
          if Curxy.Y < 350 then
          begin
            ShowMessage('按钮1按下了');
          end
          else
          begin
            ShowMessage('按钮2按下了');
          end;
      end;
     end;
     WindowProc := DefWindowProc(hwnd,umsg,wParam,lParam); 
    end;begin
      Mywindow := TMyForm.Create;
      Mywindow.ApplicationName := AppName;
      Mywindow.FWndproc := TFNAPCProc(@WindowProc);
      Mywindow.WinCreate;
      try
        Mywindow.MyRun;
       // Mywindow.CreateButton;
      finally
        FreeAndNil(Mywindow);
      end;  
    end.
      

  4.   

    各位大哥,我把我的程序按照规定改动了一下:
    unit Win32;interfaceuses
      Windows,Messages,SysUtils;const
      AppName = 'HelloWorld';
    type
      //定义一个自己的类
      TMyClass = class
      public
        //有一个窗体注册函数
        function WinRegister:Boolean;
        //创建窗体句柄函数
        function WinCreate:HWND;
        //执行窗体函数
        function ExecWin(hWnd:HWND):LongInt;
      end;  //消息销毁函数
        procedure On_Destroy(hWnd: HWnd; var Meg: TWMDestroy);
      //消息循环函数
      function WindowProc(hWnd:HWND;
                          aMessage:UINT;
                          wParam,lParam:LongInt):LongInt;stdcall;export;implementationfunction TMyClass.WinRegister:Boolean;
    var 
      WCL: TWndClass; 
    begin 
      WCL.hInstance := hInstance; 
      with WCL do 
      begin 
        Style := CS_HREDRAW or CS_VREDRAW; 
        lpfnWndProc := @WindowProc;
        hIcon := LoadIcon( hInstance, IDI_APPLICATION ); 
        hCursor := LoadCursor( 0, IDC_ARROW ); 
        hbrBackGround := hBrush( COLOR_WINDOW ); 
        lpszMenuName := nil; 
        lpszClassName := AppName; 
        cbClsExtra := 0; 
        cbWndExtra := 0; 
      end; 
      Result := RegisterClass( WCL ) <> 0;  // 登记窗口类 API 
    end;function TMyClass.WinCreate:HWND;
    begin 
      Result := CreateWindow( AppName,
                              'Win32 SDK for Delphi',
                              WS_OVERLAPPEDWINDOW,
                              100,
                              100,
                              635,
                              432,
                              0,
                              0,
                              hInstance,
                              nil );
    end;function TMyClass.ExecWin(hWnd:HWND):LongInt;
    var
      aMessage: TMsg;
    begin
      ShowWindow( hWnd, CmdShow );  // 显示窗口 API
      UpdateWindow( hWnd );        // 重新显示窗口 API
      while GetMessage( aMessage, 0, 0, 0 ) do // 取消息 API
      begin
        TranslateMessage( aMessage );  // 转换字符消息 API 
        DispatchMessage( aMessage );    // 发送消息到指定窗口 API
      end;
      Result := aMessage.wParam; 
    end;function WindowProc(hWnd:HWND;
                            aMessage:UINT;
                            wParam,lParam:LongInt):LongInt;stdcall;export;
                            var
                              aMeg: TMessage;
                            begin
                              aMeg.Msg := aMessage;
                              aMeg.WParam := wParam;
                              aMeg.LParam := lParam;
                              aMeg.Result := 0;
                                case aMessage of
                                    //WM_CREATE: On_Create( hWindow, TWMCreate( aMeg ) );
                                    WM_DESTROY: On_Destroy( hWnd, TWMDestroy( aMeg ) );
                                    //WM_COMMAND: On_Command( hWindow, TWMCommand( aMeg ) );
                                    //WM_NEWFONT: NewFont( lParam );
                                    //WM_STARTFONTS: StartFont( hWindow );
                                    // 调用缺省的窗口函数 API
                                else aMeg.Result := DefWindowProc( hWnd, aMessage, wParam, lParam );
                                end;
                            Result := aMeg.Result;
                            end;procedure On_Destroy(hWnd: HWnd; var Meg: TWMDestroy);
    begin
      PostQuitMessage(0);
    end;end.我这个改过后运行没有错误,但是,也没有弹出窗口,这是为什么,我也没找到哪里出现的逻辑错误,各位高手大哥帮帮忙,小弟谢谢了
      

  5.   

    我测试过了 你的程序没问题。我是这样测试的,你可以参考
    1 定义一个类对象在私有部分。如  haha:TMyClass;在定义一个hand:THandle;
    2 在一个按钮的OnClick 键入如下代码(我没有检查错误)
    procedure TForm1.btn1Click(Sender: TObject);
    begin
       haha.WinRegister;
       hand := haha.WinCreate;
       haha.ExecWin(hand);
    end;
    就能出现你要的窗口。建议你把CmdShow 改为SW_SHOW。要不会出一个警告。我的是D6