VBA: Finding Hdc in an Excel worksheet or UserForm

Last update on July 9, 2009 10:42 AM by jak58
Published by jak58

VBA: Finding Hdc in an Excel worksheet or UserForm







Here are two small examples on how to find Hdc in a worksheet:
  • By clicking on Sheet1 the UserForm is displayed.
  • Put the pointer on UF, hold the left mouse button down and drag the mouse.
  • By closing the UF sub continues and draw an arc on the sheet.


Getting started
  • A new workbook
  • Add a UserForm name = UserForm1


Paste the following code in sheet1:

Private Declare Function Arc Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, 

ByVal X3 As Long, ByVal Y3 As Long, ByVal X4 As Long, ByVal Y4 As Long) As Long
Private Declare Function ArcTo Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, 

ByVal X3 As Long, ByVal Y3 As Long, ByVal X4 As Long, ByVal Y4 As Long) As Long


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim B As Long
    'activate a UC and draw on it  (finding your HDC)
    UserForm1.Show
    'Finding your HDC in Excel worksheet
    monhdc = 0
    Do While myhdc = 0
        myhdc = GetForegroundWindow()
        B = myhdc
        myhdc = GetDC(myhdc)
    Loop

    'Draw directly on Worksheet
    B = Arc(myhdc, 120, 500, 320, 400, 320, 400, 780, 500)
End Sub


In the userform module

Paste the following code:


Private Type POINTAPI
  X As Long
  Y As Long
End Type
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, _
  ByVal X As Long, ByVal Y As Long, lpPoint As Any) As Long

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Byte
Private monhdc As Long
Dim Buff As Boolean


Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Buff = True
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    Do While myhdc = 0
        myhdc = GetForegroundWindow()
        myhdc = GetDC(monhdc)
    Loop
    If Button <> 1 Then Exit Sub
    hRPen = CreatePen(PS_SOLID, 10, RGB(0, 255, 0))
    DeleteObject SelectObject(myhdc, hRPen)
    If Buff Then
        MoveToEx myhdc, X * 1.32, Y * 1.32, &H0
        Buff = False
    End If
    LineTo myhdc, X * 1.32, Y * 1.32
    DoEvents

End Sub
Best answers for « VBA: Finding Hdc in an Excel worksheet or UserForm » in :
Adding a VBA CommandButton with its respective the code ShowAdding a VBA CommandButton with its respective the code Paste these two sub in a general module (Module1 for example). Sub CreateButton() Dim Obj As Object Dim Code As String Sheets("Sheet1").Select 'create button ...
Connect a database (MDB) to excel Show[VBA] Connecting a database (MDB) to excel Below is a tips of how to connect an Access database (MDB) in an application excel Add reference Microsoft DAO object librairy X.X In a general module (eg Module1) paste the code below...
VB6 Finding the RGB values of a color ShowVB6 Finding the RGB values of a color Dim R as integer Dim G as integer Dim B as integer Sub FindRGB(Col As Long) R = &HFF& And Col G = (&HFF00& And Col ) \ 256 B = (&HFF0000 And Col ) \ 65536 End Sub Note: Here...
Download Advanced Find&Replace for Excel ShowDescription This application is designed by Add-in Express LTD. Advanced Find & Replace is an add-on for Microsoft Excel. It allows you to run multiple searches for values, formulas and comments. The search results are displayed in a floating...
Download Excel Viewer ShowExcel Viewer 2003 will allow you to open, view and print spreadsheet workbooks though you do not necessarily have to install Excel on your computer. Features: It will allow you to make a copy of the information found in Excel Viewer 2003 and...
Hyperlinks ShowIntroduction to anchors Hypertext links or hyperlinks (anchors) are HTML elements that, when clicked on, enable readers to visit a new address. Hyperlinked text is underlined by default. Hyperlinks are what connect web pages to one another. They...
Spreadsheets - Cell Selection ShowCell Selection Spreadsheets are powerful tools for working with data. However, to work with data, it is necessary to have tools to rapidly choose the required cells. Line Selection An entire line can be chosen by clicking directly on the line...