Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: DELPHI MessageDlg Call from DLL disrupts Control Focus


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 6 - Last Post: Jun 17, 2016 4:16 AM Last Post By: Tallys Ferrante
Tallys Ferrante

Posts: 14
Registered: 5/29/11
DELPHI MessageDlg Call from DLL disrupts Control Focus  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 15, 2016 10:27 AM
I have a DLL with several MessageDlg calls.
When i call any of these MessageDlg, the message is displayed but when I click to close and the message box disappears I can no longer click on the grid (Mouse wheel does work but not the buttons) (no response). If I click on a second control and then back on the grid all is well again.

In a normal project(Executable) works perfectly. the problem only occurs on DLL's. I migrated my project from XE5(that works) to D10.

Someone have the same issue?

Thanks in advanced!
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: DELPHI MessageDlg Call from DLL disrupts Control Focus  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 15, 2016 12:03 PM   in response to: Tallys Ferrante in response to: Tallys Ferrante
Tallys wrote:

When i call any of these MessageDlg, the message is displayed but when
I click to close and the message box disappears I can no longer click
on the grid (Mouse wheel does work but not the buttons) (no response).

That is because none of the HWNDs that belong to the EXE are being used as
the owner for the modal dialog window, so it can't restore focus correctly
when closed. That is why you have to click on the grid again to regain focus.

Raymond Chen has a whole series of articles on his "Old New Thing" blog on
MSDN that discuss issues related to modal window ownership (or lack of).

In a normal project(Executable) works perfectly. the problem only
occurs on DLL's.

That is because calling MessageDlg() inside the EXE has access to the EXE's
TApplication and TForm HWNDs to use as modal dialog owners. A DLL does not
have that same access.

I would suggest getting rid of MessageDlg() inside the DLL altogether. Call
the Win32 MessageBox() or TaskDialog() directly instead, then you can provide
whatever HWND you need as the owner window.

--
Remy Lebeau (TeamB)
Tallys Ferrante

Posts: 14
Registered: 5/29/11
Re: DELPHI MessageDlg Call from DLL disrupts Control Focus  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2016 4:21 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Tallys wrote:

When i call any of these MessageDlg, the message is displayed but when
I click to close and the message box disappears I can no longer click
on the grid (Mouse wheel does work but not the buttons) (no response).

That is because none of the HWNDs that belong to the EXE are being used as
the owner for the modal dialog window, so it can't restore focus correctly
when closed. That is why you have to click on the grid again to regain focus.

Raymond Chen has a whole series of articles on his "Old New Thing" blog on
MSDN that discuss issues related to modal window ownership (or lack of).

In a normal project(Executable) works perfectly. the problem only
occurs on DLL's.

That is because calling MessageDlg() inside the EXE has access to the EXE's
TApplication and TForm HWNDs to use as modal dialog owners. A DLL does not
have that same access.

I would suggest getting rid of MessageDlg() inside the DLL altogether. Call
the Win32 MessageBox() or TaskDialog() directly instead, then you can provide
whatever HWND you need as the owner window.

--
Remy Lebeau (TeamB)

Hello Remy! Thanks for the information.

It is a new behavior? Because in my XE5 project works perfect. My DLL has not only MessageDlg functions, but large modules with multiple forms and multiple MessageDlg calls. Change all MessageDlg to MESSAGEBOX or TaskDialog is impossible now. Is there any other option that I continue using the MessageDlg?

Thanks!
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: DELPHI MessageDlg Call from DLL disrupts Control Focus  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2016 6:00 PM   in response to: Tallys Ferrante in response to: Tallys Ferrante
Tallys wrote:

It is a new behavior?

No.

--
Remy Lebeau (TeamB)
Tallys Ferrante

Posts: 14
Registered: 5/29/11
Re: DELPHI MessageDlg Call from DLL disrupts Control Focus  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2016 9:03 AM   in response to: Tallys Ferrante in response to: Tallys Ferrante
I solved my problem setting the global variable UseLatestCommonDialogs to false!
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: DELPHI MessageDlg Call from DLL disrupts Control Focus  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2016 6:18 PM   in response to: Tallys Ferrante in response to: Tallys Ferrante
Tallys wrote:

I solved my problem setting the global variable UseLatestCommonDialogs
to false!

On Vista+ with themes enabled and UseLatestCommonDialogs=true, MessageDlg()
uses the Win32 TaskDialogIndirect() function. If any of those conditions
are false, it uses a modal TForm instead.

The HWND used as the parent HWND, which controls things like z-order and
input focu handling, is determined like this:

TaskDialog:

Screen.ActiveForm.Handle
if 0 then Application.ActiveFormHandle
if 0 then Application.Handle

Modal TForm:

Application.ActiveFormHandle
if not 0 but is minimized or hidden or disabled then 0
if not 0 but is toolwindow then first available non-toolwindow in the parent
window chain
if 0 then
begin
if Application.MainFormOnTaskbar and assigned Application.MainForm and
Application.MainForm.HandleAllocated then
Application.MainFormHandle
else
Application.Handle
end;

When calling MessageDlg() in a DLL, it is likely to end up with a parent
HWND of 0, unless you have an active TForm in the same DLL.

--
Remy Lebeau (TeamB)
Tallys Ferrante

Posts: 14
Registered: 5/29/11
Re: DELPHI MessageDlg Call from DLL disrupts Control Focus  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 17, 2016 4:16 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Tallys wrote:

I solved my problem setting the global variable UseLatestCommonDialogs
to false!

On Vista+ with themes enabled and UseLatestCommonDialogs=true, MessageDlg()
uses the Win32 TaskDialogIndirect() function. If any of those conditions
are false, it uses a modal TForm instead.

The HWND used as the parent HWND, which controls things like z-order and
input focu handling, is determined like this:

TaskDialog:

Screen.ActiveForm.Handle
if 0 then Application.ActiveFormHandle
if 0 then Application.Handle

Modal TForm:

Application.ActiveFormHandle
if not 0 but is minimized or hidden or disabled then 0
if not 0 but is toolwindow then first available non-toolwindow in the parent
window chain
if 0 then
begin
if Application.MainFormOnTaskbar and assigned Application.MainForm and
Application.MainForm.HandleAllocated then
Application.MainFormHandle
else
Application.Handle
end;

When calling MessageDlg() in a DLL, it is likely to end up with a parent
HWND of 0, unless you have an active TForm in the same DLL.

--
Remy Lebeau (TeamB)

It´s my case Remy, i have modules with multiple forms and MessageDlg calls, so i always have at least one active form in a DLL.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02