Solved

How do you programmatically close dialog boxes?

Posted on 1997-04-02
11
712 Views
Last Modified: 2013-12-04
Hi,

How do you close programmatically all the dialog boxes?  Using
ShellExecute I print a file, e.g. "c:\temp\test.xls".  If this is not a
valid Excel file, Excel will bring up a dialog box complaining about
this.  How can I programmatically control this?  Is there a way I could
either close the window or force these dialog boxes to go to a "garbage
window" by specifying a window in the first argument of ShellExecute?
If I don't do this my program hangs next time I try to print another
Excel file.

Any help would be most appreciated!

Thanks!

Frank Kim

P.S. By the way, what is the purpose of the default directory argument
in ShellExecute and FindExecutable?
0
Comment
Question by:frankkim
  • 7
  • 4
11 Comments
 
LVL 2

Accepted Solution

by:
sapek earned 200 total points
Comment Utility
You might set a CBT hook and handle window creation event. If the window to be created is the unwanted dialog, you can forgive the window to be created at all (you can decide by seeing what is the window title, class and parent for example).
As to the default directory I believe it the directory which is made current when the functions do they work.

Adam

0
 

Author Comment

by:frankkim
Comment Utility
Hi sapek,

I have no experience with CBT hooks so please bear with me.
Are you suggesting I create a window with a CBT hook and
pass the handle to that window to ShellExecute?
Do you have any source code as an example?

Thanks for your answer about the default directory.  It makes
sense.  But why would there be a default directory argument for FindExecutable?
0
 
LVL 2

Expert Comment

by:sapek
Comment Utility
> But why would there be a default directory argument for FindExecutable?

Say you look for x.exe and you have one in system directory and another in c:\temp directory. If you set default dir at c:\temp you should find the copy in this directory. otherwise you should find the copy in system directory (is in path).

>CBT

Do something like this (this is from my MFC app but I think you can figure it out)

LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    HWND hwnd;

    switch (nCode)
    {
        case HCBT_CREATE:
           
            hwnd = (HWND)wParam;
           
            if(don't want this window)
                return 1; // nonzero

            break;
    }

    return(CallNextHookEx( ghCBTHook, nCode, wParam, lParam ));
}

//....

ghCBTHook = SetWindowsHookEx(WH_CBT, CBTProc, AfxGetInstanceHandle(), GetCurrentThreadId());
//...

//ShellExecute

// unhook when you are done (ie you don't expect any unwanted dialog anymore
UnhookWindowsHookEx(ghCBTHook);

0
 

Author Comment

by:frankkim
Comment Utility
Hi Sapek,
How do you determine the class of the window?  Specifically
how do you determine if a window is a dialog box.  I can
determine a window's parent and its title.\
When I open up a bogus Excel file, the dialog window that
says "it's not a valid file" does not seem
to have a parent.  However I don't believe I can rely on
that info to not allow the window to open.

Here's my code.  Thanks so much for the help!

HHOOK ghCBTHook;

LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
      HWND hWindow, hParentWindow;
      char windowTitle[256];

      switch (nCode)
      {
      case HCBT_CREATEWND:
            
            // Get the handle to the window that is about to be created.
            hWindow = (HWND)wParam;
            
            // Get the title of the window.
            GetWindowText(hWindow, windowTitle, sizeof(windowTitle));

            // Get the parent of the window.
            hParentWindow = GetParent(hWindow);

            //return 1; // don't want this window

            if (hParentWindow == NULL) {
                  // Window doesn't have a parent
                  // so it's not what we're looking for.
                  break;
            }

            if(TRUE/*don't want this window*/)
                  //return 1; // nonzero
                  return 0;

            break;
      }
      
      return(CallNextHookEx( ghCBTHook, nCode, wParam, lParam ));
}


int WINAPI WinMain(HINSTANCE  hInstance,
                           HINSTANCE  hPrevInstance,
                           LPSTR  lpCmdLine,
                           int  nShowCmd)
{
      
      //....
      
      ghCBTHook = SetWindowsHookEx(WH_CBT, CBTProc,
            AfxGetInstanceHandle(), GetCurrentThreadId());
      //...
      
//      ShellExecute(NULL, "open", "D:\\temp\\blah.xls",
//            NULL, NULL, SW_SHOWNA);
      ShellExecute(NULL, "open", "C:\\My Documents\\Church\\Riders.xls",
            NULL, NULL, SW_SHOWNA);
      
      // unhook when you are done (ie you don't expect any unwanted dialog anymore
      UnhookWindowsHookEx(ghCBTHook);
      
      return 0;
}

0
 

Author Comment

by:frankkim
Comment Utility
By the way, AfxGetInstanceHandle always returns NULL for
some reason and the ASSERT fails.  Do you know why?

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:sapek
Comment Utility
AfxGetInstanceHandle() - this is MFC function.
The third argument for SetWindowsHookEx should be NULL if CBTProc is in the program (not a dll).
One thing I'm not sure. Is the excel error dialog box created in the context of process calling ShellExecute? If so everything is OK. If not, the above code wont catch creation of the dialog. Instead you must you must set a system wide CBT hook, specifing 0 for last parameter of SetWindowsHookEx. In that case CBTProc must be in a dll (not your program module).
When you call ShellExecute don't pass NULL as first argument. It tells what will be parent of the error dialog box.
I thing that best way to tell (in the CBTProc) that window is an error dialog box is to create in your program an invisible, unused window and pass its handle as first arg to ShellExecute. If you see (in CBTProc) a window whose parent is the invisible window it can only be an error dialog created as a result of calling ShellExecute.

Adam

0
 

Author Comment

by:frankkim
Comment Utility
Instead of passing NULL as the third argument for CBTProc
I pass the hInstance parameter that is passed in through WinMain.
Is that okay?
Thanks again!

0
 

Author Comment

by:frankkim
Comment Utility
The Excel dialog box is created in the context of the ShellExecute in this program.
I created a window as you suggested but my CBTProc still
is only called twice (at the beginning when I set up the hook and once after the ShellExecute).
The second time it is called I get a window that has no parent.
Its style is STATIC with style WS_DISABLED.
How can I determine if this window is going to be a dialog
box?
Here is how I created the window.  Any help would be most
appreciated.
      ghMyInvisibleWindow =
            CreateWindow(
            "STATIC",            // window class name
            "",                        // window caption
            WS_DISABLED,      // window style,
            CW_USEDEFAULT,      // initial x position
            CW_USEDEFAULT,      // initial y position
            CW_USEDEFAULT,      // initial x size
            CW_USEDEFAULT,      // initial y size
            NULL,                  // parent window handle
            NULL,                  // window menu handle
            hInstance,            // program instance handle
            NULL);                  // creation parameters

      ShellExecute(ghMyInvisibleWindow, "open", "D:\\temp\\blah.xls",
            NULL, NULL, SW_SHOWNA);

0
 
LVL 2

Expert Comment

by:sapek
Comment Utility
I hate to write this takes more and more of my time. If you would like me to write you a program to do what you want please adjust to 200 points. Also, it would be easier to send it if you gave me your e-mail address.

Adam

0
 

Author Comment

by:frankkim
Comment Utility
I would love to have you write my program. :)  I've
upgraded the amount of points for this question to 200.
My email address is frankkim@server.net.

0
 

Author Comment

by:frankkim
Comment Utility
Thanks Adam. :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now