![]() |
Desktop Development »
Tabs & Property Pages »
Tabs and Property Pages
Advanced
Property Sheet Shell Extension in C#By Hadi AA managed .NET user-control that implements Win32 (COM) property sheet shell extensions. |
C#.NET 1.0, .NET 1.1, Win2K, Win2003VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
According to the platform SDK: "OLE property pages enable an object to display its properties in a tabbed dialog box known as a property sheet. An end user can then view and change the object�s properties."
These property pages are used extensively in Windows. Examples are the tabs shown when viewing the properties of a file, or when viewing the properties of an object in Active Directory (or any other MMC in fact); or pages used in some Control Panel applets and wizards, etc.
By default, there is no support for creating these controls in the .NET Framework. Here, we present a user control that can be used for this purpose.

When you select the properties of a file, Explorer looks up all the registered property pages to load for that file type in the registry, and loads them. Equally, when you view the properties of a User in Active Directory, the MMC looks at the display-specifiers, and calls all the extension pages. The technology that enables all this, is COM. Traditionally, in Visual C++, you would use ATL for the COM server and the MFC CPropertyPage control for the interface. CPropertyPage, is basically a CDialog (equivalent to a Form in .NET), that fills in a PROPSHEETPAGE structure.
Our goal was to write an Active Directory MMC extension in C#. This has many obvious benefits, but proved to be quite a tricky task. In this article, we will describe some of the main points involved, and present a user-control that wraps the tricky parts. It should be noted that our team has used this component successfully and commercially (including a similar control for Wizards), but our work is in no way complete, and improvements are highly welcome.
The sample project will add a tab-page to the properties of .EXE file in Windows Explorer. To use the sample, just extract the program files and run INSTALL.BAT. To change the sample, you should do the following:
SheetControl. (Sheet control is a base user control class, containing all code for handling TABs and the other messages.) Your control will be the property page that will be added. Design it according to your needs.
SheetLoader class. You must generate a GUID for your pages, and also create a new instance of it in IShellPropSheetExt.AddPages(), as demonstrated in the sample.
There are actually quite a few tricks here!
IShellPropSheetExt, IShellExtInit, and IDataObject interfaces. If you look up these COM interfaces, you will notice that marshaling has been used extensively in the definitions.
IShellPropSheetExt.AddPages(). This is were we initialize our user-control, and ...
PROPSHEETPAGE and DLGTEMPLATE structures are filled minimally. (Again, marshaling is extensively used to keep the code safe.)
SetParent() API call, the hWND handle to the empty dialog is set as the parent of our user-control.
WM_NOTIFY correctly.
SheetControl, encapsulates all needed logic. You should simply inherit, and visually design your page as usual.
lpfnAddPage) must be called. Unfortunately, a C++ pointer function can't be marshaled as C# delegates - it will cause a runtime error. This means we can't call lpfnAddPage in managed code! A very small C++ wrapper does this for us.
AddPages() finishes, our form will be deleted by the Garbage Collector! This is because there are no active references to it. .KeepAlive() fixes this by fooling the garbage collector.
Our source is based on a Microsoft sample presented in PDC 2001, which was later removed from GOTDOTNET.
Dotware Technologies is an independent software vendor based in Iran.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 22 Aug 2004 Editor: Smitha Vijayan |
Copyright 2004 by Hadi A Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |