Article View: borland.public.cppbuilder.nativeapi
Article #3573Re: Handle of MessageBox
From: Remy Lebeau (TeamB)
Date: Wed, 31 May 2006 01:12
Date: Wed, 31 May 2006 01:12
65 lines
3842 bytes
3842 bytes
"Dennis Jones" <nospam@nospam.com> wrote in message news:447ce85d$1@newsgroups.borland.com... > I need the ability to programmatically close a message box. From where exactly? MessageBox() shows a modal dialog, so you can't close it from the context of the thread that is displaying it (without some low-level hooks, that is). > However, I only want to close it if it was opened as a result of calling > MessageBox or Application->MessageBox from a specific form > (which is also open). That is going to be very tricky. Sure, you can detect whether the form is open. But you can't really detect if MessageBox() was called from any particular form. The only identifying feature of a MessageBox dialog in regards to its caller is the Owner HWND that is provided as a parameter to MessageBox(). Most of the time, that is NULL when calling MessageBox() directly. And when not NULL, there is no guarantee that it is the calling form. When calling TApplication::MessageBox(), the Owner is always the TApplication::Handle, never any form. > To do so, it would seem that I need to know a couple of things: > > 1) If a message box is currently open > 2) If the message box was opened in the context of a specific form > 3) The handle of the message box > > Is it possible to obtain these 3 pieces of information? #1 is obtained by simply using FindWindow() and similar functions to check for the existance of a dialog box. The class name is "#32770". You can then use GetWindowThreadProcessId() to make sure the HWND belongs to your target application. Do note, however, that other Win32 API dialogs might reuse that same dialog class, so you will likely have to analyze the window a bit to make sure it really is a MessageBox dialog. #2 is almost, if not completely, impossible to obtain. Especially if you are trying to do all of this outside of that form class. #3 is obtainable from the result of #1. Alternatively, the code that is calling MessageBox() can obtain the HWND directly by installing a thread-local WH_CBT hook via SetWindowHookEx() before calling MessageBox(). The HCBT_ACTIVATE notification contains the HWND. Using a WH_CBT hook is a good way to customize a standard MessageBox dialgo with extra controls, for example. > This requirement comes from the need to close a form programatically, > which includes closing any forms opened by (owned by) that form, which > also includes (potentially) message boxes. I have been able to handle closing > TForm and TCommonDialog descendants, but I'm stuck on message boxes. If the TForm owns the MessageBox dialog, meaning that the TForm::Handle was specified when calling MessageBox(), then the message should close automatically when iwns Owner TForm is closed. If, on the other hand, a NULL owner was specified (which is usually the case), then your operation is going to have a very hard time figuring out whether the dialog belongs to the TForm or not. Gambit
Technical Details
Message-ID:
<447cee05$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<447ce85d$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,21899a732622f4b0 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <447ce85d$1@newsgroups.borland.com> Subject: Re: Handle of MessageBox Date: Tue, 30 May 2006 18:12:05 -0700 Lines: 67 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <447cee05$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1149038085 68.69.17.41 (30 May 2006 18:14:45 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail