Recent

Author Topic: WinAPI - problem with dialog box based app  (Read 2135 times)

Conrad_404

  • New member
  • *
  • Posts: 8
WinAPI - problem with dialog box based app
« on: January 06, 2025, 08:55:27 pm »
Hi,
I am asking for help with a seemingly simple problem. I'm trying to write a simple GUI window application based solely on the dialog box. Despite correct compilation, the window does not appear. Below is the code. I am using an external application (Resource Builder 4) to compile resource files. Will you help?

====================

{$APPTYPE        GUI}

{$LONGSTRINGS    OFF}
{$WRITEABLECONST ON}
{$DESCRIPTION    'Default DialogBox based App'}

Program DefBox; Uses Windows, Strings, Messages, CommDlg, DOS;
{$ifdef VER90} { Delphi 2.0 }
type
       ptrint  = longint;
       ptruint = dword;
{$endif}

var
      DialogWnd : hWND;
                 Msg : TMsg;

{$R Defbox.res}

const
     MAIN_DIALOG = 200;

        TIMER_ID = 9999;

     EXIT_BUTTON = 1;
       OK_BUTTON = 2;
   CANCEL_BUTTON = 3;

function MainDialogProc( Dialog : hWnd; Msg : UINT; wParam, lParam : ptrint ) : ptrint; stdcall;
begin
  case Msg of
   WM_INITDIALOG : begin
                     Result:=0;
                     SetTimer( Dialog, TIMER_ID, 500, nil );
                   end;
        WM_TIMER : begin



                   end;
      WM_COMMAND : begin
                     case LoWord( wParam ) of
                  OK_BUTTON : begin

                              end;
              CANCEL_BUTTON : begin

                              end;
                EXIT_BUTTON : SendMessage( Dialog, WM_CLOSE, 0, 0 );
                     end
                   end;
        WM_CLOSE : begin                 
                     KillTimer( Dialog, TIMER_ID );
                     DestroyWindow( Dialog );
                   end;
      WM_DESTROY : begin
                     PostQuitMessage( 0 );
                     Result:=0;
                   end;
  else Result:=0;
  end
end;

BEGIN
  DialogWnd:=CreateDialogParam( hInstance, MakeIntResource( MAIN_DIALOG ), 0, @MainDialogProc, 0 );             //-- main window
  ShowWindow( DialogWnd, SW_SHOWNORMAL );

  while GetMessage( @Msg, 0, 0, 0 ) do          { wait for message            }
    begin
      TranslateMessage( Msg );                        { key conversions             }
      DispatchMessage( Msg );                         { send to window procedure    }
    end;

END.

=================
Resource:

100 ICON "ruler_pencil.ico"

200 DIALOG MOVEABLE PURE LOADONCALL DISCARDABLE 0, 0, 403, 237
STYLE DS_FIXEDSYS |DS_SETFONT |WS_POPUP |WS_VISIBLE |WS_SYSMENU |WS_THICKFRAME |WS_MAXIMIZEBOX |WS_MINIMIZEBOX |WS_CAPTION
CAPTION "Dialog"
FONT 9, "Ms Shell Dlg"
LANGUAGE LANG_NEUTRAL, 0
BEGIN
  CONTROL "Exit",1,"BUTTON",BS_PUSHBUTTON |BS_VCENTER |BS_CENTER |WS_CHILD |WS_TABSTOP |WS_VISIBLE ,339,200,50,25
  CONTROL "OK",2,"BUTTON",BS_PUSHBUTTON |BS_VCENTER |BS_CENTER |WS_CHILD |WS_TABSTOP |WS_VISIBLE ,7,200,50,25
  CONTROL "Cancel",3,"BUTTON",BS_PUSHBUTTON |BS_VCENTER |BS_CENTER |WS_CHILD |WS_TABSTOP |WS_VISIBLE ,177,200,50,25
END

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11993
  • FPC developer.
Re: WinAPI - problem with dialog box based app
« Reply #1 on: January 06, 2025, 09:07:50 pm »
I am asking for help with a seemingly simple problem. I'm trying to write a simple GUI window application based solely on the dialog box. Despite correct compilation, the window does not appear. Below is the code. I am using an external application (Resource Builder 4) to compile resource files. Will you help?

I compiled with brcc32 (had to find  a suitable icon, as that one afaik doesn't grok vista icons in "icon" resources), but then it simply worked with both FPC 3.2.2 (32-bit) and fpc 3.1.1 (development, also 32-bit)

I couldn't attach my res file on the forum, so I put it here:  https://www.stack.nl/~marcov/files/defbox.res
« Last Edit: January 06, 2025, 09:12:35 pm by marcov »

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: WinAPI - problem with dialog box based app
« Reply #2 on: January 06, 2025, 09:13:44 pm »
I used windres.exe to compile the RC file, and then compiled the FPC code, the window is visible.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11993
  • FPC developer.
Re: WinAPI - problem with dialog box based app
« Reply #3 on: January 06, 2025, 09:50:00 pm »
I tried fpcres, but I got

fpcres -of res defbox.rc -o defbox.res
------------------------------

#MAIN.RC(3:50): at "0": syntax error
200 DIALOG MOVEABLE PURE LOADONCALL DISCARDABLE 0, 0, 403, 237
                                                 ^
Error: EResourceReaderUnexpectedEndOfStreamException

The marker is on the first comma of "0, 0, .."

440bx

  • Hero Member
  • *****
  • Posts: 4901
Re: WinAPI - problem with dialog box based app
« Reply #4 on: January 06, 2025, 10:27:04 pm »
Like marcov, I used BRCC32 and after commenting out the icon line (can't find the .ico file), it compiled without problem.

I also tried Resource Hacker and it didn't like it even after commenting out the icon line.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Conrad_404

  • New member
  • *
  • Posts: 8
Re: WinAPI - problem with dialog box based app
« Reply #5 on: January 06, 2025, 10:28:48 pm »
Thank you for the answer. I compile it under Lazarus 3.6 and I still don't see the dialog nevertheless it exists in the memory.
I am sure that the problem is not with the resource file compiler because other programs where the main window is registered and used
and dialog boxes just support it, are work correctly.

Any clue about Lazarus IDE settings...?

440bx

  • Hero Member
  • *****
  • Posts: 4901
Re: WinAPI - problem with dialog box based app
« Reply #6 on: January 06, 2025, 10:31:57 pm »
Any clue about Lazarus IDE settings...?
Yes, the problem is due to the name you've given to the .res resource file.  Change the name to something like "MyResourceFile.res" and it will work.

ETA:

You need to recompile the .rc file into a "MyResourceFile.res" because the original .res file is not the result of compiling your .rc file, it's something else.

Also, choose any name you want _except_ the one you're currently using.  :D

ETA2:

if you want details about why renaming the .rc file will make the thing work see the thread:
https://forum.lazarus.freepascal.org/index.php/topic,40830.msg291257.html#msg291257
the posts show the problem and the last post explains why it didn't work.

HTH.


« Last Edit: January 06, 2025, 10:38:50 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Conrad_404

  • New member
  • *
  • Posts: 8
Re: WinAPI - problem with dialog box based app
« Reply #7 on: January 06, 2025, 11:04:09 pm »
I restarted everything as a new project and now I got the following information (see attached file).
Surprisingly, the executable file saved in the directory works when run independently, outside the Lazarus IDE.
I think that in this case the problem is not in the name of the resource file.

440bx

  • Hero Member
  • *****
  • Posts: 4901
Re: WinAPI - problem with dialog box based app
« Reply #8 on: January 06, 2025, 11:19:07 pm »
It's difficult to provide effective help without having access to the project files.

Compress the project files and attach them to your post while stating the problem you're having.  That will make it much easier to diagnose the problem.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Conrad_404

  • New member
  • *
  • Posts: 8
Re: WinAPI - problem with dialog box based app
« Reply #9 on: January 07, 2025, 06:47:18 pm »
Thank you all for your help. Finally everything works. Indeed, my problem was caused by the fact that one MUST NOT name your resource file the same way the application is named.
In such a case Lazarus creates its own resource file with this name and overwrites existing resource file. My later problems resulted from the fact that changing the name itself did not help - everything had to be recompiled. I attached zip file with a working version. Maybe someone will need it in the future. Regards!
« Last Edit: January 07, 2025, 09:16:36 pm by Conrad_404 »

440bx

  • Hero Member
  • *****
  • Posts: 4901
Re: WinAPI - problem with dialog box based app
« Reply #10 on: January 07, 2025, 09:26:28 pm »
Indeed, my problem was caused by the fact that one MUST NOT name your resource file the same way the application is named.
That reminds me of what my father used to tell me: you learn fast after explaining it to you a long time.

You're welcome. :)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Conrad_404

  • New member
  • *
  • Posts: 8
Re: WinAPI - problem with dialog box based app
« Reply #11 on: January 08, 2025, 09:06:09 pm »
You father was absolutely right :-)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11993
  • FPC developer.
Re: WinAPI - problem with dialog box based app
« Reply #12 on: January 08, 2025, 09:50:07 pm »
I filed a bug for fpcres not understanding the dialog line btw.

 

TinyPortal © 2005-2018