Consulting

Results 1 to 8 of 8

Thread: Prevent User from moving userform

  1. #1
    VBAX Regular
    Joined
    May 2007
    Location
    SE Michigan
    Posts
    11
    Location

    Prevent User from moving userform

    Any one have thoughts on how to prevent a user from moving a userform by grabbing the title bar?
    I set the top and left to 0 and height and width to usableheight and usablewidth. This sets the userform to as large as the user's resolution will permit. The user is still able to grab the title bar and move the form. I can leave a blank sheet behind it and make the rest very hidden but it's a hassle to have to move the form again to access all controls. I can also reset the location of the userform with every event but that's kind of a worse case scenario. I really want to run my user forms full screen and prevent their movement but I can not quite get there.
    Anyone with a suggestion on this?

    Thanks in advance,
    Chris
    "There are 10 types of people in the world - those that get binary and those that don't."

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    How about this

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
        ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
        ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
        ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function GetSystemMenu Lib "user32" ( ByVal hWnd As Long, _
        ByVal bRevert As Long) As Long
    Private Declare Function DeleteMenu Lib "user32" ( ByVal hMenu As Long, _ByVal nPosition As Long, _
        ByVal wFlags As Long) As Long
    Private Declare Function ShowWindow Lib "user32" ( ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    Private Declare Function EnableWindow Lib "user32" ( ByVal hWnd As Long, ByVal fEnable As Long) As Long
    Private Declare Function DrawMenuBar Lib "user32" ( ByVal hWnd As Long) As Long
    Private Declare Function SetFocus Lib "user32" ( ByVal hWnd As Long) As Long
    Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" ( _
        ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
        ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long
    Private Declare Function LockWindowUpdate Lib "user32" ( ByVal hWndLock As Long) As Long
    
    'Window styles
    Private Const GWL_STYLE As Long = (-16)  
    Private Const GWL_EXSTYLE As Long = (-20) 
    Private Const WS_CAPTION As Long = &HC00000 
    Private Const WS_EX_APPWINDOW As Long = &H40000 
    Private Const WS_EX_TOOLWINDOW As Long = &H80  
    
    Private hWnd As Long
    
    Private Sub UserForm_Activate()
        Dim lStyle As Long, hMenu As Long
        hWnd = FindWindow("ThunderDFrame", Me.Caption)
        If hWnd = 0 Then Exit Sub
        lStyle = GetWindowLong(hWnd, GWL_STYLE)
        lStyle = lStyle And Not WS_CAPTION
        SetWindowLong hWnd, GWL_STYLE, lStyle
        lStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
        lStyle = lStyle And Not WS_EX_APPWINDOW
        lStyle = lStyle And Not WS_EX_TOOLWINDOW
        SetWindowLong hWnd, GWL_EXSTYLE, lStyle
        DrawMenuBar hWnd
        SetFocus hWnd
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    May 2007
    Location
    SE Michigan
    Posts
    11
    Location
    Thank you for the quick response. I am going to work my way through your code this weekend. Right now it's a bit confusing but I will work it out. I am going to hold off on marking this solved until Monday. I will post responses or marked this solved on monday morning. Again, thanks for you help and have a great weekend!

    Chris
    "There are 10 types of people in the world - those that get binary and those that don't."

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    Bob, is this the same in principle as building an interface layer which the user only sees?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Similar Ted. Here, you would need to hide the application as well, and then this takes over. Personally, I wouldn't remove the caption, I would just disable Minimize and maximize if I didn't want them messing with it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    MS Excel MVP VBAX Mentor Andy Pope's Avatar
    Joined
    May 2004
    Location
    Essex, England
    Posts
    344
    Location
    This will not disable the ability to try and move the userform but it will leave the userform in position.

    Private m_vntTop
    Private m_vntLeft
    
    Private Sub UserForm_Activate()
        m_vntTop = Me.Top
        m_vntLeft = Me.Left
    End Sub
    
    Private Sub UserForm_Layout()
        If Not IsEmpty(m_vntTop) Then Me.Move m_vntLeft, m_vntTop
    End Sub
    Cheers
    Andy

  7. #7
    VBAX Regular
    Joined
    May 2007
    Location
    SE Michigan
    Posts
    11
    Location
    I started reviewing you code, Bob (I gather that is your name). Looks like I will need to read up on programming with API calls before I start messing with this. Thank you for the direction.
    Andy, I have used something like your approach in my activate routine and in every change event to relocate the userform to me.top = 0 and me.left = 0. This keeps the userform infront of the user while in use. I will just keep my data sheets veryhidden to prevent the user from messing with them. Thank you very much for your help. I will consider this solved and mark it as such.
    Chris
    "There are 10 types of people in the world - those that get binary and those that don't."

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If it works, you don't have to read-up on anything, just use it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •