Windows窗体应用程序中的闪烁 [英] Flickering in a Windows Form application
问题描述
我在我的应用程序中包含以下代码,但再次发生闪烁.
i include the following code in my application but again the flickering is happening.
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles();
此代码包含在
this code included inside
public Form1()
{}
为什么这件事发生了..?
我的应用程序包含1种形式,其中25种面板用于不同用途.当我使用键盘的向下箭头选择下拉列表项时,表格闪烁.首先,我不知道那东西在闪烁.现在我意识到这是闪烁的,并做了一些操作来消除闪烁.但这不起作用.
Why this thing is happening..?
My application contains 1 form with 25 panels for different uses. when i select drop-down list items using keyboards down arrow,the form is flickering. first i couldn''t realize that that thing is flickering. now i realize that is flickering and did something to remove flickering. but that''s not working.
推荐答案
Stackover流专家帮助我解决了这个问题...感谢Stack溢出....
另一个解决方案:
//TODO:不要忘记使用System.Runtime.InteropServices.
Stackover flow experts help me to solve this issue... Thanks to Stack overflow....
Yet another solution:
//TODO: Don''t forget to include using System.Runtime.InteropServices.
internal static class NativeWinAPI
{
internal static readonly int GWL_EXSTYLE = -20;
internal static readonly int WS_EX_COMPOSITED = 0x02000000;
[DllImport("user32")]
internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32")]
internal static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
}
您的表单构造函数应如下所示:
And your form constructor should look as follows:
public MyForm()
{
InitializeComponent();
int style = NativeWinAPI.GetWindowLong(this.Handle, NativeWinAPI.GWL_EXSTYLE);
style |= NativeWinAPI.WS_EX_COMPOSITED;
NativeWinAPI.SetWindowLong(this.Handle, NativeWinAPI.GWL_EXSTYLE, style);
}
在上面的代码中,您可以将this.Handle更改为MyFlickeringPanel.Handle
您可以在这里阅读更多有关它的信息:扩展窗口样式和CreateWindowEx.
设置WS_EX_COMPOSITED后,窗口的所有后代将使用双缓冲从下到上的绘画顺序.从下到上的绘画顺序允许后代窗口具有半透明(alpha)和透明(color-key)效果,但前提是后代窗口也设置了WS_EX_TRANSPARENT位.双重缓冲允许绘制窗口及其后代,而不会闪烁.
共享|编辑
23分钟前回答了
尼古拉·基尔
864
In the code above, you might change this.Handle to something like MyFlickeringPanel.Handle
You can read a bit more about it here: Extended Window Styles and here: CreateWindowEx.
With WS_EX_COMPOSITED set, all descendants of a window get bottom-to-top painting order using double-buffering. Bottom-to-top painting order allows a descendent window to have translucency (alpha) and transparency (color-key) effects, but only if the descendent window also has the WS_EX_TRANSPARENT bit set. Double-buffering allows the window and its descendents to be painted without flicker.
share|edit
answered 23 mins ago
Nikolay Khil
864
Stackover流专家帮助我解决了这个问题...感谢Stack溢出....
另一个解决方案:
//TODO:不要忘记使用System.Runtime.InteropServices.
收合|复制代码
内部静态类NativeWinAPI
{
内部静态只读int GWL_EXSTYLE = -20;
内部静态只读int WS_EX_COMPOSITED = 0x02000000;
[DllImport("user32")]
内部静态外部int GetWindowLong(IntPtr hWnd,int nIndex);
[DllImport("user32")]
内部静态外部int SetWindowLong(IntPtr hWnd,int nIndex,int dwNewLong);
}
您的表单构造函数应如下所示:
收合|复制代码
公共MyForm()
{
InitializeComponent();
int样式= NativeWinAPI.GetWindowLong(this.Handle,NativeWinAPI.GWL_EXSTYLE);
样式| = NativeWinAPI.WS_EX_COMPOSITED;
NativeWinAPI.SetWindowLong(this.Handle,NativeWinAPI.GWL_EXSTYLE,style);
}
在上面的代码中,您可以将this.Handle更改为MyFlickeringPanel.Handle
您可以在这里阅读更多有关它的信息:扩展窗口样式和CreateWindowEx.
设置WS_EX_COMPOSITED后,窗口的所有后代将使用双缓冲从下到上的绘画顺序.从下到上的绘画顺序允许后代窗口具有半透明(alpha)和透明(color-key)效果,但前提是后代窗口也设置了WS_EX_TRANSPARENT位.双重缓冲允许绘制窗口及其后代,而不会闪烁.
共享|编辑
23分钟前回答了
尼古拉·基尔
864
Stackover flow experts help me to solve this issue... Thanks to Stack overflow....
Yet another solution:
//TODO: Don''t forget to include using System.Runtime.InteropServices.
Collapse | Copy Code
internal static class NativeWinAPI
{
internal static readonly int GWL_EXSTYLE = -20;
internal static readonly int WS_EX_COMPOSITED = 0x02000000;
[DllImport("user32")]
internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32")]
internal static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
}
And your form constructor should look as follows:
Collapse | Copy Code
public MyForm()
{
InitializeComponent();
int style = NativeWinAPI.GetWindowLong(this.Handle, NativeWinAPI.GWL_EXSTYLE);
style |= NativeWinAPI.WS_EX_COMPOSITED;
NativeWinAPI.SetWindowLong(this.Handle, NativeWinAPI.GWL_EXSTYLE, style);
}
In the code above, you might change this.Handle to something like MyFlickeringPanel.Handle
You can read a bit more about it here: Extended Window Styles and here: CreateWindowEx.
With WS_EX_COMPOSITED set, all descendants of a window get bottom-to-top painting order using double-buffering. Bottom-to-top painting order allows a descendent window to have translucency (alpha) and transparency (color-key) effects, but only if the descendent window also has the WS_EX_TRANSPARENT bit set. Double-buffering allows the window and its descendents to be painted without flicker.
share|edit
answered 23 mins ago
Nikolay Khil
864
这篇关于Windows窗体应用程序中的闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!