Solved

Transparent Form in VB5

Posted on 1998-04-04
7
391 Views
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.

Thanks.
0
Comment
Question by:msali
  • 3
  • 2
  • 2
7 Comments
 
LVL 8

Expert Comment

by:MikeP090797
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  
   );      
Parameters
nLeftRect
Specifies the x-coordinate of the upper-left corner of the region.
nTopRect
Specifies the y-coordinate of the upper-left corner of the region.
nRightRect
Specifies the x-coordinate of the lower-right corner of the region.
nBottomRect
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.
Remarks
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
   );      
Parameters
hWnd
Handle to the window whose window region is to be set.
hRgn
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.
bRedraw
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.  
Remarks
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
GetWindowRgn, WM_WINDOWPOSCHANGING

 


0
 
LVL 8

Expert Comment

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

Author Comment

by:msali
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.

thanks.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:bwoods
ID: 1460369
msali,
  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...
0
 

Author Comment

by:msali
ID: 1460370
bwoods,
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.....

msali
0
 

Accepted Solution

by:
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 FLAGS = SWP_NOMOVE Or SWP_NOSIZE
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

0
 

Author Comment

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

0

Featured Post

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.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

708 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

15 Experts available now in Live!

Get 1:1 Help Now