Einzelnen Beitrag anzeigen

EWeiss
(Gast)

n/a Beiträge
 
#29

AW: 1400 Error beim Erstellen eines Fensters via API

  Alt 8. Aug 2011, 14:04
Vorallem darfst du niemals einen Breakpoint bei WinHandle := CreateWindow(
setzen auch dann wird das erstellen des Windows jedesmal fehlschlagen.
Dem kann ich so nicht zustimmen, denn ich konnte (also ich sowas das letzte Mal gemacht hatte) überall Halptepunkte hinsetzen oder mit F7 durchsteppen, ohne daß es da nennenswerte Probleme gab.
(damals in Delphi 7 und TDE)
Ungläubiger Thomas
Es ist aber so!

Habe es gerade nach deinem Post nochmalig getestet.
Das setzen eines Breakpoint auf WinHandle := CreateWindow( schlägt immer ohne ausnahme fehl.

Ist auch unabhängig von der Delphi Version Das ist Winapi muss überall laufen egal welche Delphi Version das ist.
Schlägt es in D7 fehl dann auch in allen anderen Versionen wie bei mir d2009

Hier deine Lösung!

Delphi-Quellcode:
program API;

uses
  Windows, Messages, SHFolder, inifiles, SysUtils;

type

  TSForm = class
  private
    run: Boolean;
    ClassNamen: PAnsiChar;
    windowClass: TWndClass;
    Winhandle: HWND;
    AppName: PAnsiChar;
    w, h: Integer;
    msg: TMsg;
    procedure finale;
    procedure init;
    procedure ShowError(ErrorText: PChar);
    function getAppData() : String;
    procedure keypress(c : Char);
  protected
    //
  public
    constructor Create();
    procedure Show();
    destructor Close();
  end;

var
  win : TSForm;

procedure TSForm.finale;
begin
  //
end;

function WindowProcedure(Winhandle: HWND; uMsg: UINT; wParam: wParam; lParam: LParam):
  lresult; stdcall;
var
  x, y : integer;
begin
  Result := 0;

  case uMsg of
    WM_CREATE:
      begin
        x := GetSystemMetrics(SM_CXSCREEN);
        y := GetSystemMetrics(SM_CYSCREEN);

        MoveWindow(Winhandle, (x div 2) - (win.w div 2),
          (y div 2) - (win.h div 2),
          win.w, win.h, true);
      end;

    WM_DESTROY:
      begin
         win.run := false;
        win.finale();
        PostQuitMessage(0);
      end;
    WM_KEYDOWN:
      begin
        win.KeyPress(Char(Word(wParam)));
      end;

  else
    Result := DefWindowProc(Winhandle, uMsg, wParam, lParam);
  end;
end;

destructor TSForm.Close;
begin

  UnregisterClass(windowClass.lpszClassName, hInstance);
end;

constructor TSForm.Create;
begin

  w := 640;
  h := 400;
end;

function TSForm.getAppData: String;
var
  path : array[0..MAX_PATH] of char;

begin

  if SUCCEEDED(SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, 0, PChar(path[0]))) then
    result := path
  else
    result := '';
end;

procedure TSForm.init;
begin

  run := true;
end;

procedure TSForm.keypress(c: Char);
begin
  //
end;

procedure TSForm.Show;
begin
  //
end;

procedure TSForm.ShowError(ErrorText: PChar);
begin

  MessageBox(Winhandle, ErrorText, 'Error', MB_ICONERROR);
  Halt;
end;


begin
  win := TSForm.Create();

  win.appName := 'WinAPITestApp';
  win.ClassNamen := 'WinAPITestWindow';
  win.windowClass.hInstance := hInstance;

  with win.windowClass do
  begin
    Style := CS_HREDRAW or CS_VREDRAW;
    lpfnWndProc := @WindowProcedure;
    cbClsExtra := 0;
    cbWndExtra := 0;
    hbrBackground := COLOR_APPWORKSPACE;
    lpszMenuName := nil;
    lpszClassName := win.ClassNamen;
    hIcon := LoadIcon(0, IDI_WINLOGO);
    hCursor := LoadCursor(0, IDC_ARROW);
  end;

  if RegisterClass(win.windowClass) = 0 then
    win.ShowError(PChar('#' + IntToStr(GetLastError) + ' : ' + SysErrorMessage(GetLastError)));

  win.WinHandle := CreateWindow(win.windowClass.lpszClassName, win.AppName, WS_CAPTION or WS_VISIBLE or WS_SYSMENU
    or WS_MINIMIZEBOX or WS_MAXIMIZEBOX or WS_SIZEBOX, 400,
    400, win.w, win.h, 0, 0, hInstance, nil);

  if win.WinHandle <> 0 then
  begin
    win.init();

    while win.run do
    begin
      if PeekMessage(win.msg, win.WinHandle, 0, 0, PM_REMOVE) then
      begin
        TranslateMessage(win.msg);
        DispatchMessage(win.msg);
      end;
    end;
  end else
  win.ShowError(PChar('#' + IntToStr(GetLastError) + ' : ' + SysErrorMessage(GetLastError)));

  ExitCode := win.msg.wParam;

  win.Show();
  win.Close();

end.

gruss

Geändert von EWeiss ( 8. Aug 2011 um 14:30 Uhr)
  Mit Zitat antworten Zitat