Recently, when developing a winform system, we used a large number of Custom User Controls, fully applied object-oriented methods, and used domain-driven design and development methods. In the early stage of this method, a large number of Custom User Controls were developed. As a result, these controls were placed on the form when running, and flickering was found on the switching interface. The more controls, the more powerful the flickering. After searching for a long time, I finally found this method, which is very sharp and effective. Paste it here. Thanks to the original author.
The solution is to rewrite createparams on form and usercontrol. The program is as follows,
Code on Form
C #
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
VB
Protected Overrides ReadOnly Property CreateParams As System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H2000000
' Turn on WS_EX_COMPOSITED
Return cp
End Get
End Property
Usercontrol code
C #
protected override CreateParams CreateParams {
get {
var parms = base.CreateParams;
parms.Style &= ~0x02000000; // Turn off WS_CLIPCHILDREN
return parms;
}
}
VB
Protected Overrides ReadOnly Property CreateParams As System.Windows.Forms.CreateParams
Get
Dim parms As System.Windows.Forms.CreateParams = MyBase.CreateParams
'Turn off WS_CLIPCHILDREN
parms.Style = (parms.Style And (Not &H2000000))
Return parms
End Get
End Property
Exam materials: How
To fix the flickering in user controls