Transparent Form in VB5

Posted on 1998-04-04
Last Modified: 2012-08-13
Hi Experts,
Is it possible to make a transparent Form in VB5?  Actually what i want to do is to lanuch my app which opens with a form (supposedly a transparent form) so that i can see the image of what ever is at the back.  e.g., say i am working in MS-Access and then i launch my app.  At this time eventhough my app is loaded but i can still see the Access Screen.  Now when i drag my mouse line is drawn and i can highlight any area i want to.  Basically i want to use it as a training tool in my office where i can draw a line or a circle around a any thing and talk about it.

second option will be to capture screen image of previous screen and show it in an image box in my form - but that is my second choice.

ps: drawing lines and boxes is no problem - pls focus the answer towards making it transparent.

Question by:msali
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2

Expert Comment

ID: 1460366
You can set form's region to 1/100 of form's height ,so it won't actualy be drawn. You can do that using SetWindowRgn and CreateRectRgn:

The CreateRectRgn function creates a rectangular region.
HRGN CreateRectRgn(

    int nLeftRect,      // x-coordinate of region's upper-left corner
    int nTopRect,      // y-coordinate of region's upper-left corner
    int nRightRect,      // x-coordinate of region's lower-right corner  
    int nBottomRect       // y-coordinate of region's lower-right corner  
Specifies the x-coordinate of the upper-left corner of the region.
Specifies the y-coordinate of the upper-left corner of the region.
Specifies the x-coordinate of the lower-right corner of the region.
Specifies the y-coordinate of the lower-right corner of the region.
Return Value
If the function succeeds, the return value is the handle to the region.
If the function fails, the return value is NULL.
The region will be exclusive of the bottom and right edges.
See Also
CreateRectRgnIndirect, CreateRoundRectRgn, DeleteObject, SelectObject

The SetWindowRgn function sets the window region of a window. The window region determines the area within the window where the operating system permits drawing. The operating system does not display any portion of a window that lies outside of the window region
int SetWindowRgn(

    HWND hWnd,      // handle to window whose window region is to be set
    HRGN hRgn,      // handle to region
    BOOL bRedraw      // window redraw flag
Handle to the window whose window region is to be set.
Handle to a region. The function sets the window region of the window to this region.
If hRgn is NULL, the function sets the window region to NULL.
Boolean value that specifies whether the operating system redraws the window after setting the window region. If bRedraw is TRUE, the operating system does so; otherwise, it does not.
Typically, you set bRedraw to TRUE if the window is visible.
Return Values
If the function succeeds, the return value is non-zero.
If the function fails, the return value is zero.  
If the bRedraw parameter is TRUE, the system sends the WM_WINDOWPOSCHANGING and WM_WINDOWPOSCHANGED messages to the window.
The coordinates of a window's window region are relative to the upper-left corner of the window, not the client area of the window.
After a successful call to SetWindowRgn, the operating system owns the region specified by the region handle hRgn. The operating system does not make a copy of the region. Thus, you should not make any further function calls with this region handle. In particular, do not close this region handle.  
To obtain the window region of a window, call the GetWindowRgn function.
See Also



Expert Comment

ID: 1460367
Can you please explain why my answer wasn't good for you?

Author Comment

ID: 1460368
No there is nothing wrong with it except that your solution makes the size of the form small instead making it transparent.  If i do not find any other answer i would be glad to award you the points.

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


Expert Comment

ID: 1460369
  I regret to say that you are probably going to have to go with your second choice.  It really isn't that difficult. - I can give you code (VB5) that uses a couple of API calls to quickly StretchBlt the screen image to your app's form as it loads, if you need it - You could of course try MikeP's suggestion, as it would work for the transparency issue... However, depending on exactly what you want to do with your app, it might give you a problem with mouse-events. i.e. You won't get any!  And if your app gets no mouse-events, you can't draw with the mouse... Of course, there is always the option of subclassing the background app to steal it's mouse-events, then drawing directly to the screen yourself... I wouldn't relish that, though.  Therefore, my suggestion is to make your app sit in the system tray.  Work in your background app ( Access, whatever ), and when you need an easel, double-click your system-tray icon, and voila, it expands to full screen size and copies the current background screen into it's form so you can draw all you like... when you finish, minimize it back to the system-tray!  Quite kewl...   Good luck.  I'm posting this as a comment in case someone else out there knows a secret for transparent forms - I won't lock it unless you want that code I mentioned.  In the meantime, I just had an idea that might work for a transparent form... gotta go test it first...

Author Comment

ID: 1460370
I would love to have the code and award the points to u.  I will obviously double the points if you could add to your code on how to save the same image as a .bmp on the disk (on a clikc of a button).

thanks and hope to hear from you soon.....


Accepted Solution

bwoods earned 100 total points
ID: 1460371
Option Explicit
'Here it is, quick, dirty, and easy as pie, including the
'code to save it as a bmp with the click of a button...
'It's yours, good luck and enjoy

'To use this code, create a project, create a form
'No need to change any of the form's properties, size,
'etc. Put a button on the form, anywhere, doesn't matter
'Name the button cmdSave, and paste all this into
'the form's code page. It SHOULD run as is with no modification
' at all, but you know how "SHOULD" goes... all I can say is,
'it ran as is on my machine!

'Later, I would suggest making the
'form caption-bar-less, so that it really looks like you
'are drawing on the other app... you'll just have to put
'some means to close/minimize your app in there, since
'you won't have the caption bar (and thus no close button!)

'NOTE: There is no error handling here!  Error-handling
'      is a personal religion.  However, you definitely
'      need it when using API calls and VB together.
'      Notice that nearly every API call returns a long
'      value.  This value will tell you information about
'      the success of the API call ( or how many bytes were
'      retrieved, moved, stored, etc, etc ), but to know
'      what the return value means, you have to research
'      each one.  I would highly recommend Dan Appleman's
'      Basic 5.0 Programmer's Guide to the Win32 API from
'      Ziff Davis Press. It is not necessary to check every
'      return value, but at the least, put error handling
'      around every API call in case it fails - A botched
'      API call will bring your program ( and sometimes the
'      system! ) to its knees!

'Module level variable
'would be in a common module, prob'ly
Private tempLong&

'API requirements
'these would normally of course be public in a common module, too

'**  Constant Definitions:

Private Const HWND_TOPMOST& = -1
Private Const SWP_NOMOVE& = &H2
Private Const SWP_NOSIZE& = &H1
Private Const SRCCOPY = &HCC0020

'**  Function Declarations:

Private Declare Function DeleteDC& Lib "gdi32" (ByVal hdc As Long)
Private Declare Function SetWindowPos& Lib "user32" (ByVal hwnd As Long, _
         ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, _
         ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
Private Declare Function CreateDC& Lib "gdi32" Alias "CreateDCA" _
        (ByVal lpDriverName As String, ByVal lpDeviceName As String, _
         ByVal lpOutput As String, lpInitData As Long)
  'NOTE: lpInitData above is normally "as DEVMODE", but we don't need it here
  '      so we are just using a long

Private Declare Function StretchBlt& Lib "gdi32" (ByVal hdc As Long, _
        ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _
        ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, _
        ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, _
        ByVal dwRop As Long)

Private Sub cmdSave_Click()
   SavePicture Image, App.Path & "\" & "TEST.BMP" ' Save picture from form to a file
   'the file-name is hard-coded here, but you could throw up
   ' a common-dialog (SAVE AS) to let the user decide at RT what
   ' to call it
End Sub

Private Sub Form_Load()
    'Make the easel a TOPMOST window
    'tempLong = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
    SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS
    'Make the form exactly cover the screen
    Move 0, 0, Screen.Width, Screen.Height
    'Copy a clone of the desktop onto the form, to
    'serve as a background for the drawing
    'I'm not including the drawing since you say
    'you know how to do that...
    CopyScreen Me
End Sub

'This function does the screen copy dirtywork
'not much to it...
'the beauty of this sub is that it is universal!
'you can pass it just about anything you can draw
'on, and it'll copy the screen to it
'This includes pictureboxes, etc.  However, we don't \
'need a picturebox, as you can see...

Sub CopyScreen(canvas As Object)
    Dim screendc&
    canvas.AutoRedraw = True 'this is necessary because of the way
                             'VB uses an internal bitmap for redrawing
    'get a dc to the "DISPLAY" device itself, in this case, screen
    screendc = CreateDC("DISPLAY", "", "", 0&)
    'this proc's reason to live...
    tempLong = StretchBlt(canvas.hdc, 0, 0, canvas.Width, canvas.Height, _
                    screendc, 0, 0, Screen.Width, Screen.Height, SRCCOPY)
    'always clean up DC's
    tempLong = DeleteDC(screendc)
    canvas.AutoRedraw = False 'this is necessary because of the way
                              'VB uses an internal bitmap for redrawing
End Sub


Author Comment

ID: 1460372
it works :-) and is beauty.


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 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