🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #11272

Re: How to make a window modal

#11272
From: Remy Lebeau (TeamB)
Date: Sun, 22 Feb 2004 04:17
60 lines
3136 bytes
"Martin Golm" <golm@gmx.de> wrote in message
news:403746C4.E97F73E6@gmx.de...

> Could you please explain this?
>
> At the moment my ShowModal() function looks like this:

The way you have it set up, your loop will never abort until the message
queue receives a WM_QUIT message during program shutdown, regardless of what
happens with the window that you are displaying.  A user could close the
window and your ShowModal() function would still not ever exit correctly.
When the user closes the window, you need to detect that so that you can
break out of your loop and exit ShowModal(), allowing the rest of your
application code to progress normally again.

If you look at the VCL's TForm class, it has a ModalResult property for
exactly the purpose of aborting the modal message loop, ie:

    function TCustomForm.ShowModal: Integer;
    //...
    begin
    //...
        ModalResult := 0;
        repeat
            Application.HandleMessage;
            if Application.FTerminate then ModalResult := mrCancel else
                if ModalResult <> 0 then CloseModal;
        until ModalResult <> 0;
    //...
    end;

In your own code, you need to either 1) set up a similar type of flag in
your code that your window closing sets when appropriate to abort the modal
window and return control back to the rest of the application, or 2) have
your message loop catch WM_CLOSE or equivilent notification and then exit
the message loop.

>   while(::GetMessage(&msg, 0, 0, 0))

That kind of looping is very dangerous.  If you read the documentation for
GetMessage(), it even says as much:

    Note that the function return value can be TRUE, FALSE, or -1. Thus, you
should avoid code like this:

        while (GetMessage( lpMsg, hWnd, 0, 0)) ...

    The possibility of a -1 return value means that such code can lead to
fatal application errors.

> From what I've tested it seem's to work this way.

What you fail to realize, though, is that your loop never actually exits
even though the window is closed.  GetMessage() does not abort unless it
receives a WM_QUIT message, which is not actually sent when closing a
window.


Gambit
Technical Details
Message-ID: <40382b87$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4036276C.5E202C7F@gmx.de> <40366739@newsgroups.borland.com> <403746C4.E97F73E6@gmx.de>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,214f35d58464ad5
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-02-21 20:40:04 PST
From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4036276C.5E202C7F@gmx.de> <40366739@newsgroups.borland.com> <403746C4.E97F73E6@gmx.de>
Subject: Re: How to make a window modal
Date: Sat, 21 Feb 2004 20:17:46 -0800
Lines: 62
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 67.22.138.253
Message-ID: <40382b87$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1077422983 67.22.138.253 (21 Feb 2004 20:09:43 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail