Winforms Flickering While Drawing Image on Taskbar

2019-09-12 13:28发布

问题:

Right now I'm drawing a small 16x16 image on the title bar of my window. It works nicely, except for an obnoxious flicker that I cant figure out how to get rid of.

I'm simply drawing the image like this:

Protected Overrides Sub WndProc(ByRef m As Message)
    If m.Msg = WM_SIZE Then
        wnd_size = New Size(New Point(CInt(m.LParam)))
    End If
    If m.Msg = WM_ACTIVATE _
    OrElse m.Msg = WM_SIZE _
    OrElse m.Msg = WM_SYNCPAINT _
    OrElse m.Msg = WM_NCACTIVATE _
    OrElse m.Msg = WM_NCCREATE _
    OrElse m.Msg = WM_NCPAINT _
    OrElse m.Msg = WM_NCACTIVATE _
    OrElse m.Msg = WM_NCHITTEST _
    OrElse m.Msg = WM_PAINT _
    OrElse m.Msg = WM_MOUSEMOVE Then
        Dim g As Graphics = Graphics.FromHdc(CType(GetWindowDC(CInt(Handle)), IntPtr))
        g.DrawImage(My.Resources.drag, 0, 0, 16, 16)
    End If
    MyBase.WndProc(m)
End Sub

Its repainting the entire title bar each time something is changed on it(click, mouseover the corner buttons, etc), and its during the repaint I get the flicker.

Anyone else ever get this problem?

回答1:

In light of previous comments on this, I've decided that its not really worth pursuing. The flicker effect wont go away unless I completely draw the title bar manually, which is a clunky workaround. Instead I've redone my thinking on how to handle the program in its entirety. The only viable solution is to completely remove the windows border and draw a psuedo border/bar on top of the form. See http://www.codeproject.com/KB/miscctrl/gTitleBar.aspx

Or better yet, just let the title bar be.