Transparent frame control not refreshing before being displayed.

Posted on 2004-09-04
Last Modified: 2010-08-05

I am attempting to inplement some code that sets the background colour of a VB6 Frame control to transparent. I am doing this because I am using XP themes on the application and the colours of  the frame and tab control that it sits on are different.

Thia is the code I am using.

Main form
Public Sub Main()
    frmMain.fraMediaplayer.BackColor = &HFF&
    MakeFrameTransparent frmMain.fraMediaplayer
End Sub
Module 1
Option Explicit

Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) _
      As Long

Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject _
      As Long) As Long

Public Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 _
      As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 _
      As Long) As Long

Public Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn _
      As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, _
      ByVal nCombineMode As Long) As Long

Public Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, _
      ByVal x As Long, ByVal y As Long) As Long

Public Declare Function SetWindowRgn Lib "user32" (ByVal hwnd _
      As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long

Public CtrlDc As Long

Public Function GetTransparentFrame(Ctrl As Frame) As Long
    Dim lHeight As Long
    Dim lWidth As Long
    Dim lTemp As Long
    Dim lSkin As Long
    Dim lStart As Long
    Dim lLine As Long
    Dim lColumn As Long
    Dim lBackColor As Long
    lSkin = CreateRectRgn(0, 0, 0, 0)

    With Ctrl
    'Form.ScaleMode = vbTwips
    lHeight = .Height / Screen.TwipsPerPixelY
    lWidth = .Width / Screen.TwipsPerPixelX
    'Form.ScaleMode = vbPixels
    'lHeight = .Height
    'lWidth = .Width
    CtrlDc = GetDC(.hwnd)
    lBackColor = Ctrl.BackColor

    For lLine = 0 To lHeight - 1
        lColumn = 0
        Do While lColumn < lWidth
            Do While lColumn < lWidth And GetPixel(CtrlDc, lColumn, lLine) = lBackColor
                lColumn = lColumn + 1
            If lColumn < lWidth Then
                lStart = lColumn
                Do While lColumn < lWidth And GetPixel(CtrlDc, lColumn, lLine) <> lBackColor
                  lColumn = lColumn + 1
                If lColumn > lWidth Then lColumn = lWidth
                lTemp = CreateRectRgn(lStart, lLine, lColumn, lLine + 1)
                Call CombineRgn(lSkin, lSkin, lTemp, 2)
                Call DeleteObject(lTemp)
            End If
    Next lLine
End With

GetTransparentFrame = lSkin
End Function

Public Sub MakeFrameTransparent(Ctrl As Frame)
    Dim lSkin As Long
    Ctrl.Visible = True
    'Set the background colour.

    lSkin = GetTransparentFrame(Ctrl)
    Call SetWindowRgn(Ctrl.hwnd, lSkin, True)
End Sub


When the form is loaded/displayed I can see the red background of the frame for a short period before it changes to transparent. If I put a break point in on ><, and step through the code, then it works, but take out the breakpoint - or compile into an exe and run - and I see the colour change.

Can the form be updated/painted before being shown on the screen, so that the red backgound is never seen?


Question by:AArnie
  • 4
  • 3
  • 2
  • +1

Expert Comment

ID: 11981960
Trying putting this line:

    MakeFrameTransparent frmMain.fraMediaplayer

in the Form_Load event of frmMain.

Alternatively, you could have the default window position of the form somewhere off screen, and then move it after you execute MakeFrameTransparent, like this:

    frmMain.Move (Screen.Width - frmMain.Width) * 0.5, (Screen.Height - frmMain.Height) * 0.5


Author Comment

ID: 11983607
Hi Burbble

If I move the line: -> "MakeFrameTransparent frmMain.fraMediaplayer" then the backcolor is always red, and if I move the form off of he screen then reposition, it is still red.

If I move the form 50% on the screen and then move it, the half that was on the screen goes transparent, but the half that was off of the screen stays red.

It seems that the form has to be visible on screen before it can be made transparent. If it is not visible on the screen then no matter what I do before it is visible is reverted back before being displayed. Perhaps the backcolor is being buffered in memory somewhere and never updated until it has been displayed.

I'm not sure.


Expert Comment

ID: 11984633
Well, after many hours of contemplation and experimentation, I still have no solution. The problem is that the GetPixel function returns -1 for any part of the Frame that is offscreen (since Frames don't have an AutoRedraw property, they aren't drawn when not onscreen). I don't think there is a solution for this, aside from using a different control (like a PictureBox).

This probably isn't of any use, but I also tried this:

CtrlDc = GetDC(Ctrl.hwnd)
For y = 0 To Ctrl.Height - 1
For x = 0 To Ctrl.Width - 1
    If GetPixel(CtrlDc, x, y) = Ctrl.BackColor Then
        SetPixel CtrlDc, x, y, GetPixel(frmMain.hdc, x + Ctrl.Left, y + Ctrl.Top)
    End If
Next x
Next y

Which also didn't work, because of the GetPixel thing. Plus, since Frames don't AutoRedraw, with this code you can move the Form offscreen and then back on to make the Frame red again.



Author Comment

ID: 11985545
Well, thanks for trying.

Perhaps I'll have to look at a different approach. Maybe I can subclass another control, or find a 3rd party one.

If I get a solution I'll post it here.

Thanks anyway,


Expert Comment

ID: 11985982
Ok, sorry I couldn't help more :/

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

LVL 32

Expert Comment

ID: 11986527
This may be helpful, it explains a workaround (however unsupported):

INFO: Visual Basic 6.0 Does Not Support Windows XP Themes or Visual Styles

Author Comment

ID: 11987919
I already use the workaround mentioned here. This is part of the problem, as I have a tab control on a form, and my frame sits within this. In VB6 the background is a constant colour, but with the XP styles there is a gradient - hence this is why I am after the transparency.

Thanks for the input though. I'll keep trying.


LVL 32

Expert Comment

ID: 12002565
Good reading:

Use Manifests for the Look and Feel of Windows XP

Their sample writes the manifest file on-the-fly.  I tried the sample and the frame control paints normally using the standard themes that come w/XP.

Do you have a third party skinner or theme program installed?

Author Comment

ID: 12563592
Hi people,

Apologies for not repling.

Thanks for your responses. I don't think I found the answer I was looking for however. Finally I found the following 3rd party control, which is currently free, and for the moment suits my requirements.    ->    Freecontrols.

If nobody has any objections I'll put a request in to have points refunded in the next day or so.


Accepted Solution

CetusMOD earned 0 total points
ID: 12638499
Question PAQ'd
250 points refunded.

Community Support Moderator

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now