@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
Programatically close AfxInpuBox
-
- Posts: 4315
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
-
- Posts: 564
- Joined: Sep 27, 2016 18:20
- Location: Valencia, Spain
Re: Programatically close AfxInpuBox
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.
-
- Posts: 4315
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Programatically close AfxInpuBox
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é.
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é.
-
- Posts: 4315
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Programatically close AfxInpuBox
What if AfxInputBox was in a separate thread of execution?
-
- Posts: 564
- Joined: Sep 27, 2016 18:20
- Location: Valencia, Spain
Re: Programatically close AfxInpuBox
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...
This may cause other problems, like having the input box ending being displayed behind the main window, having more than one input box active...
-
- Posts: 4315
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Programatically close AfxInpuBox
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.
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.
-
- Posts: 564
- Joined: Sep 27, 2016 18:20
- Location: Valencia, Spain
Re: Programatically close AfxInpuBox
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.
-
- Posts: 4315
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Programatically close AfxInpuBox
Quite so. Three is good reason why AfxInputBox is modal, and we defeat the object by removing it.
-
- Posts: 4315
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Programatically close AfxInpuBox
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!
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!