Solved

To make a application always on top?

Posted on 2000-04-17
14
416 Views
Last Modified: 2010-05-02
I have designed an application for logging into the system to keep track of the users. so it the usere should not omot that screen how can i make the screen always on the top. and make the kleyboard disabled while that particular form is in the action. Once the user has logged in then the user can acces the keyboard. Can any one help me?
0
Comment
Question by:sathesh_manie
  • 5
  • 3
  • 2
  • +3
14 Comments
 
LVL 2

Expert Comment

by:brice123
ID: 2725446
Here is a general procedure that can be called to make a form the topmost window and to revert to the normal status.

Private Declare Function SetWindowPos Lib "user32" Alias _
    "SetWindowPos" (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

Sub SetTopmostWindow(ByVal hWnd As Long, Optional topmost As Boolean = True)
    Const HWND_NOTOPMOST = -2
    Const HWND_TOPMOST = -1
    Const SWP_NOMOVE = &H2
    Const SWP_NOSIZE = &H1
    SetWindowPos hWnd, IIf(topmost, HWND_TOPMOST, HWND_NOTOPMOST), 0, 0, 0, 0, _
        SWP_NOMOVE + SWP_NOSIZE
End Sub

When you wish to create a topmost window simply call this procedure passing the handle of the window:
 
SetTopmostWindow Me.hWnd

When you want to return to the normal status, call the same function passing False as the second argument:

SetTopmostWindow Me.hWnd, False
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2725504
how do they log in?  they need to type in a password?  so you dont want to completely disable the keyboard do you?
0
 
LVL 2

Expert Comment

by:burntout
ID: 2725619
When you load the login form do it like this..

form1.Show vbModal
0
 
LVL 2

Expert Comment

by:brice123
ID: 2725623
Is it suffisant for you if you disable the task switching? (Alt-Tab, Start, Run, Task Manager, etc...)
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2725627
or simply create an inputbox


Dim password As String

password = InputBox "Enter password"
0
 

Expert Comment

by:AlessandroF
ID: 2725866
With this code you can set a form that is Always on top as the standard account form: The window is not resized, moved or altered in any way.

--------
Example
--------
Window_SetAlwaysOnTop Form1.hwnd, True


---------------------
function declaration
---------------------
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOREDRAW = &H8
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
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

--------------
Function code:
--------------

'-------------------------------------------------------------------------------
' FUNCTION    : Window_SetAlwaysOnTop(HWND, Boolean)
' DESCRIPTION : This function will set a window "always on top".
'               WIN32 API function are called to set/unset the window topmost.
'               The window is not resized, moved or altered in any way.
' PARAMETERS  : - hwnd: Long            Window to set alwasy on top.
'               - AlwaysOnTop: Boolean  Flag to set/unset always on top.
' RETURN      : True if the function succeded,
'               False if an error occured and the operation cannot be performed.
' EXAMPLE     : Window_SetAlwaysOnTop Form1.hwnd, True
'-------------------------------------------------------------------------------
Function Window_SetAlwaysOnTop(hwnd As Long, AlwaysOnTop As Boolean) As Boolean
    If AlwaysOnTop Then
        Window_SetAlwaysOnTop = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOREDRAW Or SWP_NOSIZE Or SWP_NOMOVE)
    Else
        Window_SetAlwaysOnTop = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOREDRAW Or SWP_NOSIZE Or SWP_NOMOVE)
    End If
End Function
0
 

Expert Comment

by:BEVIS
ID: 2725872
copy this into a module
Option Explicit

Public Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
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 SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_SHOWWINDOW = &H40
Public Const SWP_NOACTIVATE = &H10
Public Const HWND_TOPMOST = -1


place a command button and a textbox on your form where you can enter the password that is "Password" in this case

Option Explicit

Private Sub Command1_Click()
Dim x As Long
Dim tbwnd&
Dim showwnd&
If Text1.Text = "Password" Then
  x = SystemParametersInfo(97, False, CStr(1), 0)
  tbwnd& = FindWindow("shell_traywnd", "")
  showwnd& = ShowWindow(tbwnd&, 1)
  Form1.Hide
  Unload Form1
End If
End Sub

Private Sub Form_Load()
Dim x As Long
Dim tbwnd&, wFlags
Dim showwnd&
x = SystemParametersInfo(97, True, CStr(1), 0)
tbwnd& = FindWindow("shell_traywnd", "")
showwnd& = ShowWindow(tbwnd&, 0)
wFlags = SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW Or SWP_NOACTIVATE
SetWindowPos Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, wFlags
End Sub

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:BEVIS
ID: 2725884
Remember to remove the forms control box and windowstate to max
0
 
LVL 1

Accepted Solution

by:
mathies earned 100 total points
ID: 2752809
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 SWP_FRAMECHANGED = &H20
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_SHOWME = SWP_FRAMECHANGED Or SWP_NOMOVE Or SWP_NOSIZE
Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -1



Public Sub StayOnTop(lngObjHwnd As Long, Optional blnRemoveFromTop As Boolean = False)

   'Put a window Stay on Top mode or remove from top

   Dim lngTemp As Long

On Error GoTo ErrorHandle

   If Not blnRemoveFromTop Then
      lngTemp = SetWindowPos(lngObjHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE)
   Else
      lngTemp = SetWindowPos(lngObjHwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE)
   End If
   
   Exit Sub
   
ErrorHandle:
   Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext

End Sub
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2753094
mathies i noticed you running around today posting answers to all these questions, many of which already have sufficient comments and proposed answers which are the same as yours.  whats your deal man?
0
 
LVL 1

Expert Comment

by:mathies
ID: 2753124
If they don't accept comment it's because they need more info!!!
0
 
LVL 1

Expert Comment

by:mathies
ID: 2753140
AzraSound
If you don't like my answers, try it and you will see that everything work fine!
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2753142
or maybe theyve been busy and havent had a chance to respond yet. usually if it doesnt work for them theyll let us know. youre solution is no different from two comments posted already. if those dont suffice neither does yours.  chill out and just post comments.  it looks like youve been on this site awhile, you should know how things work by now.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2753153
i'm not denying that they work, the point is the same damn solution was posted twice before yours.  who cares, post what you want.
0

Featured Post

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

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…
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.
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…
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…

743 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

11 Experts available now in Live!

Get 1:1 Help Now