Solved

make a borderless form stay behind all other forms

Posted on 2014-02-02
22
280 Views
Last Modified: 2014-02-03
How can i make a border-less form stay behind all other forms ?
0
Comment
Question by:isnoend2001
  • 11
  • 8
  • 2
  • +1
22 Comments
 
LVL 4

Expert Comment

by:ravikantninave
Comment Utility
For example, to show Form2 in this way from Form1, you would use code like this:
Form2.Show vbModal

Open in new window


If you want to make the form go on top of others in your program at a particlar place in your code, but allow the user to switch to another form if they want to (and bring that one on top), you can just change the ZOrder of the form.

ZOrder is the "depth" position on screen, with 0 being the one which is "on top". To bring Form1 to the front you would do this:
Form1.ZOrder 0

Open in new window

To use this API you need to add the following declarations (in the "General" "Declarations" section of a form/module):
    Public 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) As Long
    Public Const HWND_TOPMOST = -1
    Public Const HWND_NOTOPMOST = -2
    Public Const SWP_NOMOVE = &H2
    Public Const SWP_NOSIZE = &H1

Open in new window

To set Form1 to be "on top" of other forms use this code:
    Call SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)

Open in new window

To return the form to normal, this is the code you need:
    Call SetWindowPos(Form1.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)

Open in new window

This code shows Form2, with Form1 being its owner:
Form2.Show vbModeless, Form1

Open in new window

0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Try this
Const HWND_BOTTOM As Long = 1

SetWindowPos Me.hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
0
 

Author Comment

by:isnoend2001
Comment Utility
Thanks for adding that
I think is what i need is the
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long,
the form will always be be behind all other forms and never be brought to the front
0
 

Author Comment

by:isnoend2001
Comment Utility
Martinliss i get
variable not defined on this: SWP_NOACTIVATE
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
You should have an application called 'API Text Viewer' in 'Visual Studio 6 Tools'

If you open it and click on 'File', then 'Load Text File...', you should see 'WIN32API.TXT' in the list. Choose it and you will see a list of API function declarations. If you change the API Type to constants, you will get a list that includes those prefixed SWP_.

You might find the application useful in the future,
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
You don't have to leave VB6 to get API information. Just go to Add-ins|Api Viewer.
0
 

Author Comment

by:isnoend2001
Comment Utility
maybe i'm doing something wrong, but that does not put the form behind other forms.
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
I'm not at home but will get back to you later.
0
 

Author Comment

by:isnoend2001
Comment Utility
ok, thanks
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
Thanks Martin, for that shortcut.

I wish that I could estimate the cumulative hours that it could have saved me since it was introduced.

Unfortunately I don't know when the facility was introduced. I'm pretty sure that it wasn't there at all in VB1. At a guess, I would say VB3, though VB4 with its 16/32 bit versions would seem to be a logical point.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Thanks Martin, for that shortcut.
You're welcome.
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
isnoend2001, please don't take this as the last word on the subject but it may not be possible to do what you want with a borderless form. If you are willing to have a border then you could use a MDIForm as your main form at that type of form, by it's nature, stays in the background.
0
 

Author Comment

by:isnoend2001
Comment Utility
MartinLiss;
The project I just uploaded the frmBackground is the form i am trying to stay behind all other forms. It fills the whole screen and when smaller forms are shown the effect of Bonus screens would not be so dramatic
I was thinking maybe this would work:
Use a form with a border and set the .top to a negative number to hide the titlle bar.
Possible ?.
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Assuming that frmBackground is showing, is there ever going to be a time when more than one other form will need to be shown?
0
 

Author Comment

by:isnoend2001
Comment Utility
yes i think frmCleoOptions, frmCleoPayout, and the commondialog
all open in front of frmCleo
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
When I force frmBackround to be shown, it shows text that says "12 Free Plays Initiated...". How does that happen?
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Never mind, I see that it's frmBonus12 that I'm looking at.
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
Comment Utility
The problem does not seem to be getting frmBackground to be the background, instead it's that when other forms are shown on top of it, clicking frmBackground makes the other forms disappear. I think the only solution is to add a MDIForm to your project and use that instead of frmBackground. If you then make all your other forms MDIChild forms it will do what you want.
0
 

Author Closing Comment

by:isnoend2001
Comment Utility
thanks for your help
Don't know about those MidiForms never used them. will give it more thought
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Here's a bare-bones MDI project. I added the background color.
Q-28354481.zip
0
 

Author Comment

by:isnoend2001
Comment Utility
Thanks i will check it out
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now