Here is my solution:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
<System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)>
Public Structure Rect
<System.Runtime.InteropServices.FieldOffset(0)> Public left As
Integer
<System.Runtime.InteropServices.FieldOffset(4)> Public top As
Integer
<System.Runtime.InteropServices.FieldOffset(8)> Public right As
Integer
<System.Runtime.InteropServices.FieldOffset(12)> Public bottom As
Integer
End Structure 'Rect
<System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)>
Public Structure Point
<System.Runtime.InteropServices.FieldOffset(0)> Public x As Long
<System.Runtime.InteropServices.FieldOffset(4)> Public y As Long
End Structure
Class LibWrapper
<System.Runtime.InteropServices.DllImport("gdi32.dll",
CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)>
_
Public Shared Function RectVisible(ByVal hDC As IntPtr, ByRef rect
As Rect) As Integer
End Function
<System.Runtime.InteropServices.DllImport("gdi32.dll",
CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)>
_
Public Shared Function SetViewportOrgEx(ByVal hDC As IntPtr, ByVal x
As Integer, ByVal y As Integer, ByRef pt As Point) As Integer
End Function
End Class 'LibWrapper
Public Declare Function CreateCompatibleDC Lib "gdi32" Alias _
"CreateCompatibleDC" (ByVal hdc As IntPtr) As IntPtr
Public Declare Function CreateCompatibleBitmap Lib "gdi32" Alias _
"CreateCompatibleBitmap" (ByVal hdc As IntPtr, ByVal nWidth As Integer,
ByVal nHeight As Integer) As Integer
Public Declare Function DeleteDC Lib "gdi32" Alias "DeleteDC" (ByVal hdc
As IntPtr) As Integer
Public Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (ByVal _
hDestDC As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal _
nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As IntPtr, _
ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) _
As Integer
Public Declare Function PatBlt Lib "gdi32" Alias "PatBlt" (ByVal _
hDestDC As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal _
nWidth As Integer, ByVal nHeight As Integer, ByVal dwRop As Integer) _
As Integer
Public Declare Function SelectObject Lib "gdi32" Alias _
"SelectObject" (ByVal hdc As IntPtr, ByVal hObject As Integer) As
Integer
Public Declare Function DeleteObject Lib "gdi32" Alias _
"DeleteObject" (ByVal hObject As Integer) As Integer
Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const WHITENESS As Integer = &HFF0062
Public Const SRCCOPY As Integer = &HCC0020
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.DoubleBuffer, False)
Me.SetStyle(ControlStyles.Opaque, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.UserPaint, True)
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
Me.Text = "Form1"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(384, 358)
End Sub
#End Region
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim rc As Rect = New Rect
rc.left = e.ClipRectangle.Left
rc.right = e.ClipRectangle.Right
rc.top = e.ClipRectangle.Top
rc.bottom = e.ClipRectangle.Bottom
'Get original hdc
Dim hdcWindow As IntPtr = e.Graphics.GetHdc()
'Create a compatible DC with the one given to us
Dim hdcMemory As IntPtr = CreateCompatibleDC(hdcWindow)
'Create a compatible bitmap in memory DC with the size of the
clipping(Rectangle)
Dim hMemoryBitmap As Integer = CreateCompatibleBitmap(hdcWindow,
Me.Width, Me.Height)
'Select the new bitmap into memory DC
Dim hMemoryBitmapOld As Integer = SelectObject(hdcMemory,
hMemoryBitmap)
'Clear the memory DC
PatBlt(hdcMemory, 0, 0, Me.Width, Me.Height, WHITENESS)
' Just use gdiplus to draw for testing purposes--- I am to lazy to
use GDI
Dim newGraphics As Graphics = Graphics.FromHdc(hdcMemory)
' Draw rectangle to screen.
newGraphics.DrawRectangle(New Pen(Color.Red, 3), 50, 50, 200, 100)
newGraphics.Dispose()
'Blit the memory bitmap back to the real DC.
Dim pt As Point = New Point
pt.x = 0
pt.y = 0
If LibWrapper.RectVisible(hdcWindow, rc) Then
LibWrapper.SetViewportOrgEx(hdcWindow, e.ClipRectangle.X,
e.ClipRectangle.Y, pt)
BitBlt(hdcWindow, 0, 0, _
Me.Width, Me.Height, hdcMemory, _
e.ClipRectangle.X, e.ClipRectangle.Y, SRCCOPY)
Else
BitBlt(hdcWindow, 0, 0, _
Me.Width, Me.Height, hdcMemory, _
0, 0, SRCCOPY)
End If
'Select the original bitmap back into the DC
SelectObject(hdcMemory, hMemoryBitmapOld)
'Delete the compatible bitmap
DeleteObject(hMemoryBitmap)
'Delete the compatible DC
DeleteDC(hdcMemory)
'Release the DC
e.Graphics.ReleaseHdc(hdcWindow)
End Sub
End Class
[quoted text, click to view] <theunissen@comcast.net> wrote in message
news:1128016929.257890.305990@z14g2000cwz.googlegroups.com...
>I figured it out! Thanks to your help pointing me about the
> RectVisible, otherwise I would have never found it this fast.
>