We help IT Professionals succeed at work.
Get Started

Selection Rectangle in VBA

2,378 Views
Last Modified: 2013-12-26
How can I draw a selection rectangle on a spesific window ?
Like clicking the left mouse button and drawing a rectangle while keeping it pressed.

I tried the following code on desktop (in order to see if it is working)
It drew a dotted rectangle around the icons that are inside the rectangle, but didn't select (delete) them.

What am I doing wrong ?

PS. I'm using VBA.


Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

Sub SelectOnScreen()

    Dim I As Integer
    Dim rctPosition As RECT
    Dim hdc As Long
 
    With rctPosition
        .Left = 5
        .Top = 4
        .Right = 80
        .Bottom = 135
    End With

    'get the desktop hDc
    hdc = GetDC(ByVal 0)

    Call DrawFocusRect(hdc, rctPosition)
    Call ReleaseDC(0, hdc)
    
    For I = 1 To 2000
    DoEvents
    Next
    
    SendKeys ("{DEL}")

End Sub

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 2 Answers and 13 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE