[RESOLVED] Draw semi transparent rectangle in picture box-VBForums
Results 1 to 10 of 10

Thread: [RESOLVED] Draw semi transparent rectangle in picture box

  1. #1

    Thread Starter
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,158

    Resolved [RESOLVED] Draw semi transparent rectangle in picture box

    hi experts,
    How to draw a white or some colored semi transparent rectangle in a picture box?
    kindly reply with some solutions,
    thank u,

    with regards,
    Seenu...
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  2. #2
    Addicted Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    144

    Re: Draw semi transparent rectangle in picture box

    vb Code:
    1. Option Explicit
    2.  
    3. '=========Gdi32 Api========
    4. Private Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    5. Private Declare Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hdc As Long) As Long
    6. Private Declare Function CreateCompatibleBitmap Lib "gdi32.dll" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    7. Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Long) As Long
    8. Private Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As Long) As Long
    9. Private Declare Function GdiAlphaBlend Lib "gdi32.dll" (ByVal hdc As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal hdc As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal BLENDFUNCT As Long) As Long
    10. Private Declare Function CreatePen Lib "gdi32.dll" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
    11. Private Declare Function CreateSolidBrush Lib "gdi32.dll" (ByVal crColor As Long) As Long
    12. '=========user32 Api========
    13. Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As Long) As Long
    14. Private Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    15.  
    16. '=========Oleaut32 Api========
    17. Private Declare Function OleTranslateColor Lib "oleaut32.dll" (ByVal lOleColor As Long, ByVal lHPalette As Long, ByVal lColorRef As Long) As Long
    18.  
    19. '=========Kernel32 Api========
    20. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
    21. Private Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As Long, ByVal hObject As Long) As Long
    22.  
    23. Private Type UcsRgbQuad
    24.     R                       As Byte
    25.     G                       As Byte
    26.     B                       As Byte
    27.     a                       As Byte
    28. End Type
    29.  
    30. Private Type BLENDFUNCTION
    31.     BlendOp                 As Byte
    32.     BlendFlags              As Byte
    33.     SourceConstantAlpha     As Byte
    34.     AlphaFormat             As Byte
    35. End Type
    36.  
    37. Private Sub DrawAlphaSelection(hdc As Long, ByVal X As Long, ByVal y As Long, ByVal Width As Long, ByVal Height As Long, ByVal Color As OLE_COLOR)
    38.  
    39.     Dim BF                  As BLENDFUNCTION
    40.     Dim hDCMemory           As Long
    41.     Dim hBmp                As Long
    42.     Dim hOldBmp             As Long
    43.     Dim DC                  As Long
    44.     Dim lColor              As Long
    45.     Dim hPen                As Long
    46.     Dim hBrush              As Long
    47.     Dim lBF                 As Long
    48.  
    49.     BF.SourceConstantAlpha = 128
    50.  
    51.     DC = GetDC(0)
    52.     hDCMemory = CreateCompatibleDC(0)
    53.     hBmp = CreateCompatibleBitmap(DC, Width, Height)
    54.     hOldBmp = SelectObject(hDCMemory, hBmp)
    55.  
    56.     hPen = CreatePen(0, 1, Color)
    57.     hBrush = CreateSolidBrush(pvAlphaBlend(Color, vbWhite, 120))
    58.     DeleteObject SelectObject(hDCMemory, hBrush)
    59.     DeleteObject SelectObject(hDCMemory, hPen)
    60.     Rectangle hDCMemory, 0, 0, Width, Height
    61.  
    62.     CopyMemory VarPtr(lBF), VarPtr(BF), 4
    63.     GdiAlphaBlend hdc, X, y, Width, Height, hDCMemory, 0, 0, Width, Height, lBF
    64.  
    65.     SelectObject hDCMemory, hOldBmp
    66.     DeleteObject hBmp
    67.     ReleaseDC 0&, DC
    68.     DeleteDC hDCMemory
    69.     DeleteObject hPen
    70.     DeleteObject hBrush
    71.  
    72. End Sub
    73.  
    74. Private Function pvAlphaBlend(ByVal clrFirst As Long, ByVal clrSecond As Long, ByVal lAlpha As Long) As Long
    75.  
    76.     Dim clrFore             As UcsRgbQuad
    77.     Dim clrBack             As UcsRgbQuad
    78.  
    79.     OleTranslateColor clrFirst, 0, VarPtr(clrFore)
    80.     OleTranslateColor clrSecond, 0, VarPtr(clrBack)
    81.     With clrFore
    82.         .R = (.R * lAlpha + clrBack.R * (255 - lAlpha)) / 255
    83.         .G = (.G * lAlpha + clrBack.G * (255 - lAlpha)) / 255
    84.         .B = (.B * lAlpha + clrBack.B * (255 - lAlpha)) / 255
    85.     End With
    86.     CopyMemory VarPtr(pvAlphaBlend), VarPtr(clrFore), 4
    87.  
    88. End Function
    89.  
    90. Private Sub Form_Paint()
    91.     Cls
    92.     DrawAlphaSelection Me.hdc, 10, 50, 100, 200, vbRed
    93.     DrawAlphaSelection Me.hdc, 50, 30, 200, 100, vbBlue
    94.     DrawAlphaSelection Me.hdc, 200, 80, 100, 100, vbGreen
    95.     DrawAlphaSelection Me.hdc, 80, 200, 200, 30, vbYellow
    96.     DrawAlphaSelection Me.hdc, 130, 70, 50, 200, vbMagenta
    97. End Sub

    Link

    leandroascierto.com Visual Basic 6 projects

  3. #3

    Thread Starter
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,158

    Re: Draw semi transparent rectangle in picture box

    thats gr8 Leandro. thank u very much...
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  4. #4

    Thread Starter
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,158

    Re: Draw semi transparent rectangle in picture box

    Leandro,
    u hav any simple blurring sample code like this?
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  5. #5
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Draw semi transparent rectangle in picture box

    LeandroA

    Impressive ..

    Spoo

  6. #6

    Thread Starter
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,158

    Re: Draw semi transparent rectangle in picture box

    LeandroA, how to draw without the border rectange?
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  7. #7
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    12,353

    Re: Draw semi transparent rectangle in picture box

    Quote Originally Posted by seenu_1st View Post
    LeandroA, how to draw without the border rectange?
    seenu_1st, one of two suggestions:

    1) Use FillRect instead of Rectangle API. If so, no need to create a pen either
    2) Instead of using "Color" for the CreatePen function, blend the color same as the CreateSolidBrush call does.
    Last edited by LaVolpe; Jul 29th, 2011 at 09:17 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,158

    Re: Draw semi transparent rectangle in picture box

    thanks Lavolpe, can u pls change that code with any one of ur suggestion?
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  9. #9
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    12,353

    Re: Draw semi transparent rectangle in picture box

    Quote Originally Posted by seenu_1st View Post
    thanks Lavolpe, can u pls change that code with any one of ur suggestion?
    Like this? Untested
    Code:
    hPen = CreatePen(0, 1, pvAlphaBlend(Color, vbWhite, 120)
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,158

    Re: Draw semi transparent rectangle in picture box

    perfect! thanks...
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.