GetDCEx returns null before showing the form. Relying on the non-client area

I have a C # .NET WinForm application that accesses a non-client area. Everything works fine, and the drawing runs as expected, except when the form loads.

I will catch the WM_NCPAINT tone, but when I try to get DC using GetDCEx, it always returns null until the form is displayed, which is completely logical, but that means the area without the client will not be drawn again until until the window is resized, it means that when the form is first loaded or restored from the minimized state, the CNC area is not redrawn and remains white.

This is similar to Windows 7.

So, how can I draw the CNC area in this situation?

EDIT: I have to add that I don't care about Aero glass and my form completely disables it.

+4
source share
1 answer

Instead of GetDCEx, I use GetWindowDC. Below is the code I'm using and I had no problem with Windows 7. As Hans noted, the best way is to set FormBorderStyle to None, but then I like to enter my own borders using this code from csharptest.net

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, m.LParam, 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, 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 

Of course, once you do this, you will have to handle any resizing, min-max, and closing functions yourself.

+2
source

Source: https://habr.com/ru/post/1344963/


All Articles