<a href="http://www.micropoll.com/akira/mpview/585320-168921">Click Here for Poll</a><a href="http://www.questionpro.com" title="online surveys">Online Survey</a><BR> | <a href="http://www.micropoll.com" title="Website Polls">Website Polls</a><BR> | <BR><a href="http://www.micropoll.com/akira/MicroPoll?mode=html&id=168921">View MicroPoll</A></div>

Visual Studio 2010!

Read now >

Windows Mobile Development Thoughts

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Special Windows Message Hooking Techniques for .NET
By John Mueller

Rate This Article: Add This Article To:

Special Windows Message Hooking Techniques for .NET - ' Impossible Tasks '
( Page 2 of 4 )

Plop. A new programming task has been dumped on your desk. The assignment requires you to to modify the appearance of an existing dialog box. You don't have to change how it works, just its appearance — but you don't have the code for any part of it. Worse, you also have to disable a control that has proven dangerous for the average user to use. It sure looks to be a long weekend.

Most developers would feel that a task like this is hopeless. Without the code, you don't have access to the user interface — or do you?

At one time, developers wrestled with the concept of controls presenting a public face using windows. In fact, everything you see on-screen is a window, of one sort or another. In this article, I'll help you understand the importance of windows as part of your visual controls, and show you how, using the Win32 API, you can draw upon a wealth of functions for working with them.

Because you can disable a window using a Win32 API call, you can also disable controls using the same technique. That works even when you don't have the code required to work with that window. The Spy++ application, described in the Working with Windows Messages in .NET article in this series, is an essential part of working with controls as windows.

In addition to viewing controls as windows, you must also view control events as messages. We've already discussed this issue in detail in this series; now it's time to put the windows together with the messages to produce applications that can monitor and interact with other applications, even when you don't have the code for that other application.

Considering Controls as Windows

The big thing to understand immediately is that controls and windows aren't synonymous. When you work with a control, you can access the events, properties, and methods of that control. Working with a window means that you have access to the physical presence on screen and nothing more. For example, you can call the MoveWindow() function to move a window around and the GetWindowText() function to obtain the value of the window (normally, its Text property), but you can't access everything in the same way that you do a control; as far as your application is concerned, you're working with a window.

Let's look at a simple example. Open a copy of Notepad and use the File | Print command to display the Print dialog box. Now, open a copy of Spy++. Select the Search | Find Window command to display the Window Search dialog box. Use the Finder Tool to locate the Print button in the Notepad Print dialog box. Click OK. You'll see a list of windows similar to the one shown in Figure1.

Figure 1: Spy++ helps you locate information about windows in other applications.

Notice the hierarchy displayed in Figure 1. Always keep the hierarchy in mind as you write code, because some control dependencies aren't apparent from their physical presentation on-screen. These hierarchies control your access to the window, though, so you need to know which control contains another control.

Because this is a common dialog box, every control is accessible, so it makes a good choice for any experimentation you want to perform. Controls that you can't access are grayed out. In some cases, that's because you haven't displayed it yet; in other cases, it means that the vendor has made the control inaccessible in some way and you can't access the control window at all. Spy++ isn't clueless about the kind of window that you're working with; it identifies the Print button as a window of the Button class with a caption of &Print. If you right-click on this entry and choose Properties from the context menu, you'll learn a lot more about the Print button, as shown in Figure 2.

Figure 2: Discovering the details about controls on a form.

I won't get into details about these entries in this article. Generally, you'll find information about the control that includes its size, caption, styles used to create the window (such as whether it's a tab stop), associated windows, class information, and both the process identifier (PID) and thread identifier (TID). In many cases, you won't even need this information to do something with the control.



 
 
>>> More Using Microsoft Visual Studio Articles          >>> More By John Mueller