Win32/MFC question: Making a dialog window return IDOK

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
I was halfway writing a little app that can search all currently open windows, look for a dialog window of a particular name, and then have it close by returning a user-selectable value (e.g. IDOK, IDCANCEL, IDYES, IDNO, etc) to that dialog's parent.

I got to the point where I found the window in question, but it just hit me I cannot simply PostMessage an IDOK or similar value to the dialog! Doh! :eek:

Any way that I can do it? I suppose that I have to trick the window into thinking that a user has clicked on an OK/Cancel/etc. button. I was thinking I could perhaps post a mouse down/up message on the button but..... that wouldn't be a clean implementation at all since certain responses (such as IDOK and IDCANCEL) do not need buttons, and there's way to identify which button does what anyway.\

Any ideas?


Thanks in advance,
:)atwl
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Without the source code interface, you are out of luck.

When the dialog box closes, a value is returned to the parent (not always the button code), however, if the box is not modal, variables may also be available to the parent.

without knowing the interfacing design, all you can do is try the common sense approach that you layed out.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Try calling EndDialog(HWND, IDOK) where HWND is handle to the dialog box in question.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
*Bad Idea* You can't assume anything about how a program handles dialog operations.
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
Argo: Cool, I didn't think of trying to use EndDialog. Another guy at the CodeGuru forums suggested I pass WM_COMMAND to the dialog, I'll try both techniques when I get home later and see which one works best.

Singh: Yeah, I know it's kinda bad, but the time I was writing the app, I was very annoyed at some bright programmer's idea of leaving asserts in one particlar universally-agreed-to-be-crap-of-a-program, which I experimented and found that ignoring the assert was fine. So instead of standing by to all the annoying assert message boxes that pop up, I'd rather have my automated app do it for me. And since I'm already halfway at it, I might as well complete it if it is doable, maybe release it for anyone who's also annoyed at irritating message boxes.


:)atwl
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
Just to let you guys know, PostMessage works but EndDialog dies with an access violation error. Guess I'll stick to posting WM_COMMAND messages then. Also tried this on regular, non-dialog windows and it doesn't seem to adversely affect them.


:)atwl