Programatically close AfxInpuBox

Windows specific questions.
Post Reply
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Programatically close AfxInpuBox

Post by deltarho[1859] »

@Josep Roca

Hi José

I have a little problem which I hope you can help me with.

I have a parent window which uses your AfxInputBox. I won't explain why, but at some point, I want the parent to close the AfxInputBox window.

I copied AfxInputBox and added a Byref parameter, HwndInputBox, to return the AfxInputBox window handle, hInputBox. HwndInputBox copies hInputBox. I do that, so I can still use 'DIM hInputBox AS HWND = ...' rather than 'hInputBox = ...' otherwise the input box does not get centred on the parent. (That is another story). The parent calls 'MyAfxInputBox( hInputBox, hDlg...'. When the parent executes 'Sendmessage(hInputBox, WM_CLOSE, 0, 0)' the AfxInputBox window does not close, and I cannot figure out why.

In a nutshell, I want the parent to close the AfxInputBox window.

Thanks

David Roberts
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Programatically close AfxInpuBox

Post by Josep Roca »

AxInputBox uses its own window callback procedure and message loop. Therefore, while it is in execution, the parent window can't send messages to it because its message loop is inactive. Your Sendmessage(hInputBox, WM_CLOSE, 0, 0 won't be executed until the input box dialog is closed.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Programatically close AfxInpuBox

Post by deltarho[1859] »

I was aware that AfxInputBox disables the parent window so that it can no longer receive mouse or keyboard input but I did not realize that “the parent window can't send messages to it because its message loop is inactive.”

It would seem then that no one can send AfxInputBox a message except Windows, which does exactly that when we close AfxInputBox.

Ah well, back to the drawing board.

Thanks, José.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Programatically close AfxInpuBox

Post by deltarho[1859] »

What if AfxInputBox was in a separate thread of execution?
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Programatically close AfxInpuBox

Post by Josep Roca »

It won't work. What you have to do is to make the dialog modeless: remove the EnableWindow calls in the function.

This may cause other problems, like having the input box ending being displayed behind the main window, having more than one input box active...
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Programatically close AfxInpuBox

Post by deltarho[1859] »

Thanks, José.

It probably isn't worth the effort.

There is another way to achieve what I want which requires some menu navigation, but with that route, there are no issues. I was looking for a faster way of doing something, but we cannot always achieve that.
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Programatically close AfxInpuBox

Post by Josep Roca »

Another problem is that, even if you make the dialog modeless, as it is wrapped into a function it won't return control to the main application until you close the dialog and the functions ends, so you will have to use it in another thread, and who knows how many more complications. IMO the purpose of an input box dialog is to get an immediate response to a question.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Programatically close AfxInpuBox

Post by deltarho[1859] »

Quite so. Three is good reason why AfxInputBox is modal, and we defeat the object by removing it.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Programatically close AfxInpuBox

Post by deltarho[1859] »

I solved my problem.

What I did not tell José was the parent window is a standalone application which is executed by another application. I am going to call the other application the grandparent, although it is not a parent in a Windows sense.

I do not want the parent to continue running if the grandparent closes. The parent periodically checks whether the grandparent is running and if not, it sends a WM_CLOSE message to itself. This is perfectly legitimate. The parent closes without knowing it was its request.

The problem occurs when the parent executes AfxInputBox and the user closes the grandparent. There is no logical reason why anyone would want to do that, but I bet someone will.

The solution was stirring me in the face. All I had to do was to disable the grandparent just before the parent executes AfxInpuBox. When AfxInputBox closes, the grandparent is re-enabled. So, the grandparent cannot be closed whilst AfxInputBox is open.

So just a few lines of code and AfxInputBox executes as designed.

Had José known about the grandparent, he would probably have said: “Disable the grandparent”.

Phew! :)
Post Reply