Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Subclassing a form based on class name

Posted on 2006-07-20
11
Medium Priority
?
390 Views
Last Modified: 2008-02-20
I need to detect the creation of windows of a certain windows class name.  
For example, if a user opens a browser window ("IEFrame"), I'd like intercept the windows message for that event, and have access to that window's handle so that I can supress the display of that window, and re-display it at a later time.

How would I do this?

Thanks!
0
Comment
Question by:glennuser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17145807
This can be done using Windows API. You need SetWindowsHookEx with WH_CBT parameter (system-wide CBT hook). Callback CBTProc function is called with HCBT_CREATEWND parameter when new window is created. C# doesn't support this directly, you need to use PInvoke. I think this is better to use C++/CLI for this.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17145859
I cannot find C# sample for this, this is really not task for C#. If you know C++/CLI, take a look at Managed Spy sample:
http://msdn.microsoft.com/msdnmag/issues/06/04/ManagedSpy/

It contains ManagedSpyLib library written in C++/CLI, which installs system-wide WH_CALLWNDPROC hook and can be called from C# client. You must replace hook type, remove all spying stuff from this library, add your own code and change library interface. In this case you can detect windows creation from C# program.

About second task, supress the display of that window, I don't know whether it is possible.
0
 
LVL 3

Expert Comment

by:BBK_
ID: 17146077
I agree with AlexFM, Hooking is the Ideal approach. But there is another way also. If you want to check some window in the system , use FindWindow API, and pass the class name as the parameter, run this in a Threading timer. So when ever you find an expected window, you can pass WM_Quit message to that window to Close that instance of that window. If you use the ProcessInformation, you will also get the path of the Executable from the Window Handle, so that you can re invoke the application in a later time also.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:glennuser
ID: 17146464
BBK:
But if I use FindWindow API doesn't that presuppose that the Window has already appeared?
I need to suppress the display the window before it first appears.
0
 
LVL 3

Expert Comment

by:BBK_
ID: 17146474
Yes, it will appear for a moment, I mean once the window appeared , then only you will get the window handle in "EnumWindows" API.
0
 

Author Comment

by:glennuser
ID: 17146558
BBK:
I'm afraid that momentary flash of the window wouldn't pass muster.
0
 

Author Comment

by:glennuser
ID: 17146574
If I had control of launching the application, would I have more options open?

Meaning, if my code was responsible for launching IE, could I do it in such a way that I would be able to accomplish this?
0
 
LVL 12

Expert Comment

by:topdog770
ID: 17147882
If you launch the application, then yes.. you can keep track of the process id that's created and control the started application as you described, showing or not showing the window, etc.
0
 

Author Comment

by:glennuser
ID: 17149012
TopDog:
But even if I have the process id, I can't do what I need until I get a handle.
It looks to me that from the process class I can get a MainWindowHandle, but its value is 0 until -- again -- the window is created, and then its too late.

Am I going about this the wrong way?
0
 
LVL 12

Accepted Solution

by:
topdog770 earned 2000 total points
ID: 17149093
From
==================================================
http://msdn2.microsoft.com/en-us/library/system.diagnostics.processstartinfo.windowstyle.aspx

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    /// <summary>
    /// Shell for the sample.
    /// </summary>
    class MyProcess
    {
       
        /// <summary>
        /// Opens the Internet Explorer application.
        /// </summary>
        void OpenApplication(string myFavoritesPath)
        {
            // Start Internet Explorer. Defaults to the home page.
            Process.Start("IExplore.exe");
                   
            // Display the contents of the favorites folder in the browser.
            Process.Start(myFavoritesPath);
 
        }
       
        /// <summary>
        /// Opens urls and .html documents using Internet Explorer.
        /// </summary>
        void OpenWithArguments()
        {
            // url's are not considered documents. They can only be opened
            // by passing them as arguments.
            Process.Start("IExplore.exe", "www.northwindtraders.com");
           
            // Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
        }
       
        /// <summary>
        /// Uses the ProcessStartInfo class to start new processes, both in a minimized
        /// mode.
        /// </summary>
        void OpenWithStartInfo()
        {
           
            ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");

======> // Here is key.. you can use the startinfo struct and tell the application to start with the
ProcessWindowStyle set to Hidden!

            startInfo.WindowStyle = ProcessWindowStyle.Minimized;
           
            Process.Start(startInfo);
           
            startInfo.Arguments = "www.northwindtraders.com";
           
            Process.Start(startInfo);
           
        }

        static void Main()
        {
                    // Get the path that stores favorite links.
                    string myFavoritesPath =
                    Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
               
                    MyProcess myProcess = new MyProcess();
         
            myProcess.OpenApplication(myFavoritesPath);
            myProcess.OpenWithArguments();
            myProcess.OpenWithStartInfo();

               }    
    }
}



==================================================
Here's a link with several examples and more info then probably needed..

http://www.codinghorror.com/blog/archives/000133.html

http://msdn.microsoft.com/msdnmag/issues/02/07/CQA/

0
 

Author Comment

by:glennuser
ID: 17151389
Thanks for everyone's feedback.

TopDog, your code worked perfectly for IE, thanks.
Do you think you have any insight into why this approach does not work for some of the Office applications (Word, Outlook, OneNote seem to ignore it).  

I suppose its up to the application to honor the WindowStyle?
Perhaps I should open up a second question for these apps?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

609 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