In devexpress, skin replacement is very simple. We don't need to create our own skin, so the official documents have a lot of skin packages. The following describes the problem.
Note: devexpress is used here.
8.1.5, which may not be supported in the old version. For the 7.x version, seeSource code.
Step 1: All forms are inherited from devexpress. xtraeditors. xtraform.
Step 2: Add two references:
Devexpress. bonusskins. v8.1
Devexpress. officeskins. v8.1
Step 3: The first line of the main function of the program class at the software entry Code Add:
Devexpress. userskins. bonusskins. Register ();
Devexpress. userskins. officeskins. Register ();
Devexpress. Skins. skinmanager. enableformskins ();
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
Application. Run (New formmain ());
Step 4: Each window contains a defalooklookandfeel control,
Step 5: The software usually has a place to set skin. This place usually needs to enumerate all skin and put all skin in a comboboxedit. The Code is as follows:
Foreach (devexpress. Skins. skincontainer skin in
Devexpress. Skins. skinmanager. Default. skins)
Cmbappstyle. properties. Items. Add (skin. skinname );
Step 6: Set skin and how to set skin. You only need to set defalooklookandfeel for each window. The Code is as follows:
This. defaultlookandfeel1.lookandfeel. skinname
= Cmbappstyle. editvalue. tostring ();
or: String skinname = E. item. caption; // the MDI form is not tested.
devexpress. lookandfeel. userlookandfeel. default. setskinstyle (skinname);
currently, most of the problems are solved. The last problem is how to set skin, change the skins of all opened windows to the corresponding style? (In devexpress8.1.5, this function comes with itself, but it is not completely flawed.) The most basic method is the observer mode. However, I thought that in winforms, there should be methods to enumerate all opened windows. Sure enough, I found: application. openforms: to manage the skin of all windows in a unified manner, I wrote a base class, baseform, which inherits from
devexpress. xtraeditors. xtraform. Put defalooklookandfeel in baseform. And create a public attribute defalooklookandfeel. Then, write
foreach
(Form F in application. openforms)
If (F is
timerapp. forms. baseform)
(F as
timerapp. forms. baseform ). defaultlookandfeel. lookandfeel. skinname =
value;
however, there is a problem with this method. An error occurs, saying "The openforms set has changed and cannot be enumerated! ", Depressed. We had to use the observer mode. The classes to be observed are as follows:
using
system;
using system. collections. generic;
using system. text;
using
timerapp. forms;
Namespace timerapp. utils
{
Public class
Skinsubject
{
Private Static skinsubject subject =
NULL;
Private list <baseform> forms = new
List <baseform> ();
Public static skinsubject
Getinstance ()
{
If (subject = NULL) Subject = new
Skinsubject ();
Return subject;
}
Private skinsubject (){}
/// <Summary>
/// Register the observer
/// </Summary>
/// <Param
Name = "F"> </param>
Public void register (baseform
F)
{
Forms. Add (f );
}
///
<Summary>
/// Deregister the observer
/// </Summary>
/// <Param name = "F"> </param>
Public void
Unregister (baseform F)
{
Forms. Remove (f );
}
/// <Summary>
/// Modify the skin of each observer
///
</Summary>
/// <Param
Name = "skinname"> </param>
Public void Policy (string
Skinname)
{
Foreach (baseform F in
Forms)
F. defaultlookandfeel. lookandfeel. skinname =
Skinname;
}
}
}
This is not a standard observer mode. It also integrates the singon mode, because only one observer has, and the method in the observer does not need to be called back.
Note: Obtain the default skin name in devexpress: devexpress. Skins. skinmanager. defaultskinname