It is an eternal theme for software skin replacement. winforms skin replacement, and B/S skin replacement now. Haha.
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 that devexpress 8.1.5 is used here and may not be supported in old versions. For the 7.x version, see Source 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 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 ();
Now, most of the problems have been solved. The last problem is how to change the skin of all opened windows to the corresponding style when setting the skin? (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. Create a common looklookandfeel. 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, and an error occurs. "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= NewSkinsubject ();
ReturnSubject;
}
Private Skinsubject () {}
/**/ /// <Summary>
///Register an observer
/// </Summary>
/// <Param name = "F"> </param>
Public Void Register (baseform F)
{
Forms. Add (f );
}
/**/ /// <Summary>
///Unregister 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 Notify ( String Skinname)
{
Foreach(Baseform FInForms)
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