Solved

Subclassing a form based on class name

Posted on 2006-07-20
11
383 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A short film showing how OnPage and Connectwise integration works.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

948 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

16 Experts available now in Live!

Get 1:1 Help Now