Results 1 to 3 of 3

Thread: Create dashed rectangle with GDI

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Create dashed rectangle with GDI

    Hi,

    I'm creating a dashed rectangle using the following code:

    Code:
        Dim hPen As Long, hOldPen As Long
        Dim hBrush As Long, hOldBrush As Long
        Dim hScreenDC As Long, nDrawMode As Long
        
        '-- Get DC of entire screen in order to draw on top of all controls
        hScreenDC = GetDC(0)
        '-- Select GDI object
        hPen = CreatePen(PS_DASH, 0, RGB(0, 0, 0))
        hOldPen = SelectObject(hScreenDC, hPen)
        '-- Draw rectangle
        Rectangle hScreenDC, miSelectX1, miSelectY1, miSelectX2, miSelectY2
        '-- Restore DC
        SelectObject hScreenDC, hOldPen
        ReleaseDC 0, hScreenDC
        '-- Delete GDI objects
        DeleteObject hPen
    However, it's creating a white background and the dash lengths are too long. Take a look at the first rectangle below.

    What I want it to look like is the second rectangle where the dashes are shorter and the background is transparent to show the underlying form color.



    Any help would be appreciated..

    Dan

    Visual Studio 2010

  2. #2
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Instead use the DrawFocusRect API:
    Code:
    Declare Function DrawFocusRect Lib "user32" Alias "DrawFocusRect" (ByVal hdc As Long, lpRect As RECT) As Long
    HTH,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Or use PS_DOT to get a dotted line, and create a transparent brush and select it into the dc.
    Private Const PS_DOT = 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width