在表单显示之前,GetDCEx返回null在非客户区域上绘图

问题描述

| 我有一个C#.NET WinForm应用程序,它绘制到非客户区域。一切正常,除了加载表单时,绘图均按预期进行。 我很好地捕获了WM_NCPAINT,但是当我尝试使用GetDCEx获取DC时,它总是返回null,直到显示完表单为止,这是完全合乎逻辑的,但这意味着在重新调整窗口大小之前,不会再次绘制非工作区,这意味着首先将表格加载到最小状态或从最小状态恢复,然后重新绘制NC区域并保持白色。 这似乎是Windows 7独有的。 那么在这种情况下我该如何吸引到NC区域呢? 编辑:我应该补充一点,我不在乎航空玻璃,并且我的表格完全禁用了它。     

解决方法

我使用GetWindowDC而不是GetDCEx。下面是我使用的代码,而Windows 7则没有问题。正如Hans所说,最好的方法是将FormBorderStyle设置为None,但是我想使用csharptest中的代码来设置自己的边框。净
Imports System.Runtime.InteropServices

Public Class NCForm    
  Inherits Form

  Public Sub New()
    Me.FormBorderStyle = FormBorderStyle.None
  End Sub

  Protected Overrides Sub WndProc(ByRef m As Message)
    MyBase.WndProc(m)

    If m.Msg = Win32.WM_NCCALCSIZE Then        
      If m.WParam <> IntPtr.Zero Then
        Dim tmpResize As Win32.NCCALCSIZE_PARAMS = Marshal.PtrToStructure(m.LParam,GetType(Win32.NCCALCSIZE_PARAMS))
        With tmpResize.rcNewWindow
          .Left += 2
          .Top += 2
          .Right -= 2
          .Bottom -= 2
        End With
        Marshal.StructureToPtr(tmpResize,m.LParam,False)
      Else
        Dim tmpResize As Win32.RECT = Marshal.PtrToStructure(m.LParam,GetType(Win32.RECT))
        With tmpResize
          .Left += 2
          .Top += 2
          .Right -= 2
          .Bottom -= 2
        End With
        Marshal.StructureToPtr(tmpResize,False)
      End If
      m.Result = New IntPtr(1)

    ElseIf m.Msg = Win32.WM_NCPAINT Then
      Dim tmpDC as IntPtr = Win32.GetWindowDC(m.HWnd)
      Using tmpG As Graphics = Graphics.FromHdc(tmpDC)
        tmpG.DrawRectangle(Pens.Red,New Rectangle(0,Me.Width - 1,Me.Height - 1))
        tmpG.DrawRectangle(SystemPens.Window,New Rectangle(1,1,Me.Width-3,Me.Height - 3))
      End Using
      Win32.ReleaseDC(m.HWnd,tmpDC)
    End If
  End Sub
当然,一旦执行此操作,则必须自己处理任何调整大小,最小-最大,关闭功能。