Transparent Form in VB5

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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Can you please explain why my answer wasn't good for you?
msaliAuthor Commented:
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.

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

  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...
msaliAuthor Commented:
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.....

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
msaliAuthor Commented:
it works :-) and is beauty.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.