Results 1 to 12 of 12

Thread: [RESOLVED] Preventing form from being closed but allowing it to be moved

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    932

    Resolved [RESOLVED] Preventing form from being closed but allowing it to be moved

    I want to arrange on a program that the small form that shows can be moved by the user BUT can't be closed or minimized. I can't seem to do this with the BorderStyle property.
    Thanks all!

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,071

    Re: Preventing form from being closed but allowing it to be moved

    Set the ControlBox property to False. That should remove the min/max/close buttons. But ensure you have a way for the user to close the form some other way or your code always closes it.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    932

    Re: Preventing form from being closed but allowing it to be moved

    Thank You LaVolpe! That was it (of course).
    Thanks all!

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,071

    Re: [RESOLVED] Preventing form from being closed but allowing it to be moved

    Tip. When I use a form like that, I typically show it as owned, i.e., frmToolbox.Show , Me.

    This way if the user minimizes or closes the owner form, then Windows will minimize/close the owned form.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    932

    Re: [RESOLVED] Preventing form from being closed but allowing it to be moved

    I'm OK now. I don't want the user to be able to minimise. And there is a specific EXIT cmd button on the form which shuts down in an orderly fashion.
    Thanks all!

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,260

    Re: [RESOLVED] Preventing form from being closed but allowing it to be moved

    Code:
    
    Option Explicit
    '
    Private Const GWL_STYLE             As Long = &HFFFFFFF0
    Private Const SWP_NOSIZE            As Long = &H1&
    Private Const SWP_NOMOVE            As Long = &H2&
    Private Const SWP_NOZORDER          As Long = &H4&
    Private Const SWP_FRAMECHANGED      As Long = &H20&  '  The frame changed: send WM_NCCALCSIZE
    Private Const WS_MINIMIZEBOX        As Long = &H20000
    Private Const WS_MAXIMIZEBOX        As Long = &H10000
    Private Const MF_BYCOMMAND          As Long = &H0&
    Private Const SC_MINIMIZE           As Long = &HF020&
    Private Const SC_MAXIMIZE           As Long = &HF030&
    '
    Private Declare Function GetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) 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) 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
    '
    
    Public Sub MinimizeCommandOffSystemMenu(TheHwnd As Long)
        DeleteMenu GetSystemMenu(TheHwnd, False), SC_MINIMIZE, MF_BYCOMMAND
    End Sub
    
    Public Sub MaximizeCommandOffSystemMenu(TheHwnd As Long)
        DeleteMenu GetSystemMenu(TheHwnd, False), SC_MAXIMIZE, MF_BYCOMMAND
    End Sub
    
    Public Sub MinimizeButtonOnWindow(TheHwnd As Long, b As Long)
        ' System menu must be handled separately.
        If b Then
            AddWindowStyle TheHwnd, WS_MINIMIZEBOX
        Else
            RemoveWindowStyle TheHwnd, WS_MINIMIZEBOX
        End If
    End Sub
    
    Public Sub MaximizeButtonOnWindow(TheHwnd As Long, b As Long)
        ' System menu must be handled separately.
        If b Then
            AddWindowStyle TheHwnd, WS_MAXIMIZEBOX
        Else
            RemoveWindowStyle TheHwnd, WS_MAXIMIZEBOX
        End If
    End Sub
    
    Private Sub AddWindowStyle(TheHwnd As Long, StyleToAdd As Long)
        Dim lStyle As Long
        lStyle = GetWindowLongA(TheHwnd, GWL_STYLE)
        If (lStyle Or StyleToAdd) <> lStyle Then
            SetWindowLongA TheHwnd, GWL_STYLE, lStyle Or StyleToAdd
            SetWindowPos TheHwnd, 0&, 0&, 0&, 0&, 0&, SWP_FRAMECHANGED Or SWP_NOMOVE Or SWP_NOZORDER Or SWP_NOSIZE ' Repaint nonclient area.
        End If
    End Sub
    
    Private Sub RemoveWindowStyle(TheHwnd As Long, StyleToRemove As Long)
        Dim lStyle As Long
        lStyle = GetWindowLongA(TheHwnd, GWL_STYLE)
        If (lStyle And Not StyleToRemove) <> lStyle Then
            SetWindowLongA TheHwnd, GWL_STYLE, lStyle And Not StyleToRemove
            SetWindowPos TheHwnd, 0&, 0&, 0&, 0&, 0&, SWP_FRAMECHANGED Or SWP_NOMOVE Or SWP_NOZORDER Or SWP_NOSIZE ' Repaint nonclient area.
        End If
    End Sub
    
    And an example of using it:

    Code:
    
    Private Sub Form_Load()
        MinimizeButtonOnWindow Me.hWnd, False
        MinimizeCommandOffSystemMenu Me.hWnd
    End Sub
    
    
    That just gets rid of the ability to minimize without tampering with anything else. It seems that you've eliminated the System Menu (i.e., ControlBox) which would also do it. So, my post may just be extra info.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,071

    Re: [RESOLVED] Preventing form from being closed but allowing it to be moved

    @Elroy. If you are going to to use APIs to do this at runtime, then why not have a way to toggle the options (including those on the system menu)? Otherwise, just use VB ControlBox, MaxButton, and/or MinButton properties.

    Don't see an option to disable the Close button.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,260

    Re: [RESOLVED] Preventing form from being closed but allowing it to be moved

    Ahhh, I've got them. Also, yeah, I've never had a need to turn things back on. I'm a bit surprised I even have the option for the min/max buttons in the titlebar.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,071

    Re: [RESOLVED] Preventing form from being closed but allowing it to be moved

    I've handled toggling one way that seems fool-proof.

    You have the code for the boxes, but the system menus... Don't want to delete them if toggling is an option. What you do is change their menu IDs and then disable menu item vs. delete (SetMenuItemInfo). When you want to re-enable them, change menu IDs back & re-enable the menu item.

    Tip: Never use the option to delete or disable the last item in the system menu, assuming it is the Close menu item. It may not be. Other apps and even projects can add custom menus at the end of the system menu.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,260

    Re: [RESOLVED] Preventing form from being closed but allowing it to be moved

    Quote Originally Posted by LaVolpe View Post
    I've handled toggling one way that seems fool-proof.

    You have the code for the boxes, but the system menus... Don't want to delete them if toggling is an option. What you do is change their menu IDs and then disable menu item vs. delete (SetMenuItemInfo). When you want to re-enable them, change menu IDs back & re-enable the menu item.

    Tip: Never use the option to delete or disable the last item in the system menu, assuming it is the Close menu item. It may not be. Other apps and even projects can add custom menus at the end of the system menu.
    Yeah, I know. It's just the way I did it for my purposes. I saw that when I posted it. It works for me, but "disable" instead of "delete" is a good idea. An easy fix, but I'll leave it to el84, or others.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    932

    Re: [RESOLVED] Preventing form from being closed but allowing it to be moved

    Elroy: With great respect (you have helped me a lot in the past) your ream of code seems much too complex a way to achieve what I wanted. The change to the Control Box property was all I needed and works perfectly, which is why I marked the thread resolved.
    Thanks all!

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,260

    Re: [RESOLVED] Preventing form from being closed but allowing it to be moved

    Hi el84. Yeah, I sort of mis-read your post #5. When I first read it, I thought that you had figured out the "close" issue, and now wanted a solution for the "minimize" issue. I re-read your post #5 after I had composed mine, and that's why I concluded with "So, my post may just be extra info."

    I'm glad you got it sorted.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

Posting Permissions

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



Click Here to Expand Forum to Full Width