Results 1 to 4 of 4

Thread: Draw a semi transparent rectangle over image box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    104

    Draw a semi transparent rectangle over image box

    Hi dear friends I want to make a semi-transparent rectangle over image box. I googled I found it but it was for VB.Net (http://www.vbforums.com/showthread.p...over-image-box ) how can I do it in VB 6.0 click the above link to see the code (vb.net)

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Draw a semi transparent rectangle over image box

    Well it isn't clear exactly what you want to accomplish. The .Net thread seems chaotic to me so I'm not sure what they were trying to do or whether or not it might be what you want to do.

    But there aren't any operations built into VB6 to do this so you have to use GDI calls, GDI+ calls, or a wrapper library for one of those APIs, or some 3rd party thing that does it some other way.

    I have a class that wraps a lot of GDI operations and extending it to call AlphaBlend was pretty easy. It compiles down quite small, but you can also chop out the things it has that you don't need in your program.

    Taking a stab at the problem I came up with:

    Code:
    Option Explicit
    
    Private Sub mnuDoit_Click()
        Dim AlphaPicture As StdPicture
        Dim TextRECT As RECT
    
        With New Drawing
            .Clear .ARound(Image1.Picture.Width * .PxPerHmX), _
                   .ARound(Image1.Picture.Height * .PxPerHmY), _
                   vbBlue 'We'll "gray it" with vbBlue.
            .ForeColor = vbWhite
            With .Font
                .Name = "Comic Sans MS"
                .Size = 28
            End With
            TextRECT.Right = .Width
            TextRECT.Bottom = .Height
            .DrawText "Grayed out!", TextRECT, vbCenter, TRANSPARENT, True, valignMiddle
            Set AlphaPicture = .Picture
            Set .Picture = Image1.Picture
            .PaintPicture AlphaPicture, Alpha:=64 'Mostly transparent.
            Set Image1.Picture = .Picture
        End With
    End Sub
    Plopping text on it was just an embellishment.

    Name:  sshot1.png
Views: 1226
Size:  1.3 KB

    Name:  sshot2.png
Views: 1209
Size:  2.7 KB


    You can also just pull out the API calls and plop them inline within your Form too of course.
    Attached Files Attached Files

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    Re: Draw a semi transparent rectangle over image box

    Mahmood,

    Maybe this thread will also be of use to you. It contains concepts that I use on certain Modal forms that overlay non-Modal forms.

    Enjoy,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Draw a semi transparent rectangle over image box

    Well you could combine that with a new feature introduced in Windows 8 and accomplish it pretty easily.

    Here I have a Visible = False blue PictureBox that I set to a layered window with Alpha at 64 positioned on top of the Image control.

    The "Do it" operation merely assigns Visible = True:

    Code:
    Option Explicit
    
    Private Declare Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongA" ( _
        ByVal hWnd As Long, _
        ByVal nIndex As Long) As Long
    
    Private Declare Function SetLayeredWindowAttributes Lib "user32" ( _
        ByVal hWnd As Long, _
        ByVal crKey As Long, _
        ByVal bAlpha As Byte, _
        ByVal dwFlags As Long) As Long
    
    Private Declare Function SetWindowLong Lib "user32" _
        Alias "SetWindowLongA" ( _
        ByVal hWnd As Long, _
        ByVal nIndex As Long, _
        ByVal dwNewLong As Long) As Long
    
    Private Const GWL_EXSTYLE = -20
    Private Const WS_EX_LAYERED = &H80000
    Private Const LWA_COLORKEY = &H1&
    Private Const LWA_ALPHA = &H2&
    
    Private Sub Form_Load()
        With Picture1
            .Move Image1.Left, Image1.Top, Image1.Width, Image1.Height
            SetWindowLong .hWnd, _
                          GWL_EXSTYLE, _
                          GetWindowLong(.hWnd, GWL_EXSTYLE) Or WS_EX_LAYERED
            SetLayeredWindowAttributes .hWnd, 0, 64, LWA_ALPHA
        End With
    End Sub
    
    Private Sub mnuDoit_Click()
        Picture1.Visible = True
    End Sub

    Name:  SShot IDE.png
Views: 1053
Size:  1.4 KB

    Running in the IDE with no "Win8 aware" manifest


    Name:  SShot EXE.png
Views: 994
Size:  1.5 KB

    Running as a compiled EXE with a "Win8 aware" manifest


    Sadly most people aren't paying attention to the wealth of new things we got beginning in Vista, let alone Windows 8 and beyond. More reason to kick the habit of laying back on that dogeared, long in the tooth, creaky, old Windows XP.
    Attached Files Attached Files

Tags for this Thread

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