internal static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
internal static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
protected void OnWM_PAINT(ref Message m) { PAINTSTRUCT ps = new PAINTSTRUCT(); // Have to call BeginPaint whenever processing a WM_PAINT message IntPtr hDC = User32.BeginPaint(m.HWnd, ref ps); RECT rectRaw = new RECT(); // Grab the screen rectangle of the window User32.GetWindowRect(this.Handle, ref rectRaw); // Convert to a client size rectangle Rectangle rectWin = new Rectangle(0, 0, rectRaw.right - rectRaw.left, rectRaw.bottom - rectRaw.top); // Create a graphics object for drawing using (Graphics g = Graphics.FromHdc(hDC)) { // Create bitmap for drawing onto Bitmap memoryBitmap = new Bitmap(rectWin.Width, rectWin.Height); using(Graphics h = Graphics.FromImage(memoryBitmap)) { // Draw the background area DrawBackground(h, rectWin); // Draw the actual menu items DrawAllCommands(h); } // Blit bitmap onto the screen g.DrawImageUnscaled(memoryBitmap, 0, 0); } // Don't forget to end the paint operation! User32.EndPaint(m.HWnd, ref ps); }