Solved

Subclassing a form based on class name

Posted on 2006-07-20
11
387 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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