Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default Remove the X button from a user form? Api?

Hello I'm just wondering if anyone can help me with the following question:

I have multiple user forms for which on some I wish to remove the X button
at the top that closes the form. I'm guessing I'll have to use the windows
API for this? I've only just started learning the fundementals of Windows API
Calls, and thus I'm a little stuck.

I'm just wondering what I would have to do, to do this, my user form's name
is "frm_Mod_Gen_CEmp"

Thanks in advance for any help.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Remove the X button from a user form? Api?

Hi,

I'm not aware of a way to remove it and will be interested to see if you get
a response but you can disable it. Right click on the userform, view code and
paste this in:-

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "That button doesn't work"
End If
End Sub


Mike


"NateBuckley" wrote:

Hello I'm just wondering if anyone can help me with the following question:

I have multiple user forms for which on some I wish to remove the X button
at the top that closes the form. I'm guessing I'll have to use the windows
API for this? I've only just started learning the fundementals of Windows API
Calls, and thus I'm a little stuck.

I'm just wondering what I would have to do, to do this, my user form's name
is "frm_Mod_Gen_CEmp"

Thanks in advance for any help.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default Remove the X button from a user form? Api?

Hello,

Thank you for the speedy reply, your solution is most appreciated.

I agree that it would be interesting to see if anyone has found a way to
remove the 'X' completely, however your code gets the job done, so I'm
thankful :)

"Mike H" wrote:

Hi,

I'm not aware of a way to remove it and will be interested to see if you get
a response but you can disable it. Right click on the userform, view code and
paste this in:-

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "That button doesn't work"
End If
End Sub


Mike


"NateBuckley" wrote:

Hello I'm just wondering if anyone can help me with the following question:

I have multiple user forms for which on some I wish to remove the X button
at the top that closes the form. I'm guessing I'll have to use the windows
API for this? I've only just started learning the fundementals of Windows API
Calls, and thus I'm a little stuck.

I'm just wondering what I would have to do, to do this, my user form's name
is "frm_Mod_Gen_CEmp"

Thanks in advance for any help.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Remove the X button from a user form? Api?

To disable it you can use the Windows API:

In a normal module have this:

Option Explicit
Public Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function DrawMenuBar Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As Long, _
ByVal bRevert As Long) As Long
Private Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) _
As Long
Private Const MF_BYPOSITION = &H400
Private Const MF_REMOVE = &H1000

Sub DisableCloseButton(hwnd As Long)

Dim hMenu As Long
Dim menuItemCount As Long

hMenu = GetSystemMenu(hwnd, 0)

If hMenu Then
menuItemCount = GetMenuItemCount(hMenu)
RemoveMenu hMenu, _
menuItemCount - 1, _
MF_REMOVE Or MF_BYPOSITION
DrawMenuBar hwnd
End If

End Sub


In the UserForm code have this:

Private Sub UserForm_Activate()
DisableCloseButton GetActiveWindow()
End Sub

You will need some sort of control on the form to unload it:

Private Sub CommandButton1_Click()
Unload Me
End Sub


RBS


"NateBuckley" wrote in message
...
Hello I'm just wondering if anyone can help me with the following
question:

I have multiple user forms for which on some I wish to remove the X button
at the top that closes the form. I'm guessing I'll have to use the windows
API for this? I've only just started learning the fundementals of Windows
API
Calls, and thus I'm a little stuck.

I'm just wondering what I would have to do, to do this, my user form's
name
is "frm_Mod_Gen_CEmp"

Thanks in advance for any help.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default Remove the X button from a user form? Api?

Excellent Stuff, Cheers.

I shall now attempt to go through that and try to understand the calls.

Thanks again!

"RB Smissaert" wrote:

To disable it you can use the Windows API:

In a normal module have this:

Option Explicit
Public Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function DrawMenuBar Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As Long, _
ByVal bRevert As Long) As Long
Private Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) _
As Long
Private Const MF_BYPOSITION = &H400
Private Const MF_REMOVE = &H1000

Sub DisableCloseButton(hwnd As Long)

Dim hMenu As Long
Dim menuItemCount As Long

hMenu = GetSystemMenu(hwnd, 0)

If hMenu Then
menuItemCount = GetMenuItemCount(hMenu)
RemoveMenu hMenu, _
menuItemCount - 1, _
MF_REMOVE Or MF_BYPOSITION
DrawMenuBar hwnd
End If

End Sub


In the UserForm code have this:

Private Sub UserForm_Activate()
DisableCloseButton GetActiveWindow()
End Sub

You will need some sort of control on the form to unload it:

Private Sub CommandButton1_Click()
Unload Me
End Sub


RBS


"NateBuckley" wrote in message
...
Hello I'm just wondering if anyone can help me with the following
question:

I have multiple user forms for which on some I wish to remove the X button
at the top that closes the form. I'm guessing I'll have to use the windows
API for this? I've only just started learning the fundementals of Windows
API
Calls, and thus I'm a little stuck.

I'm just wondering what I would have to do, to do this, my user form's
name
is "frm_Mod_Gen_CEmp"

Thanks in advance for any help.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default Remove the X button from a user form? Api?

I've successfully removed it.

I've added two more api declarations at the top

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

And two more constants

Private Const GWL_STYLE As Long = (-16)
Private Const WS_CAPTION As Long = &HC00000

These have gone into a module called mdlWinAPI with the others,



Then in the module I've changed it around a bit

Sub DisableCloseButton(hwnd As Long)
Dim hMenu As Long
Dim menuItemCount As Long
hMenu = GetSystemMenu(hwnd, 0)
If hMenu Then
IStyle = GetWindowLong(hwnd, GWL_STYLE)
IStyle = IStyle And Not WS_CAPTION
X = SetWindowLong(hwnd, GWL_STYLE, IStyle)
'menuItemCount = GetMenuItemCount(hMenu)
'RemoveMenu hMenu, menuItemCount - 1, MF_REMOVE Or MF_BYPOSITION
DrawMenuBar hwnd
End If
End Sub

I didn't have to change the call or anything in UserForm_Activate.

I found this link
http://www.tek-tips.com/faqs.cfm?fid=5196
Which I took some of the lines from in order to get this working with the
previous suggestion.

Could someone explain what is happening with the calls of "SetWindowLong"
and "GetWindowLong", i'm following it up to that point, but unfortunetly
don't know what this means.

Thanks for everyones help.





"NateBuckley" wrote:

Excellent Stuff, Cheers.

I shall now attempt to go through that and try to understand the calls.

Thanks again!

"RB Smissaert" wrote:

To disable it you can use the Windows API:

In a normal module have this:

Option Explicit
Public Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function DrawMenuBar Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As Long, _
ByVal bRevert As Long) As Long
Private Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) _
As Long
Private Const MF_BYPOSITION = &H400
Private Const MF_REMOVE = &H1000

Sub DisableCloseButton(hwnd As Long)

Dim hMenu As Long
Dim menuItemCount As Long

hMenu = GetSystemMenu(hwnd, 0)

If hMenu Then
menuItemCount = GetMenuItemCount(hMenu)
RemoveMenu hMenu, _
menuItemCount - 1, _
MF_REMOVE Or MF_BYPOSITION
DrawMenuBar hwnd
End If

End Sub


In the UserForm code have this:

Private Sub UserForm_Activate()
DisableCloseButton GetActiveWindow()
End Sub

You will need some sort of control on the form to unload it:

Private Sub CommandButton1_Click()
Unload Me
End Sub


RBS


"NateBuckley" wrote in message
...
Hello I'm just wondering if anyone can help me with the following
question:

I have multiple user forms for which on some I wish to remove the X button
at the top that closes the form. I'm guessing I'll have to use the windows
API for this? I've only just started learning the fundementals of Windows
API
Calls, and thus I'm a little stuck.

I'm just wondering what I would have to do, to do this, my user form's
name
is "frm_Mod_Gen_CEmp"

Thanks in advance for any help.



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default Remove the X button from a user form? Api?

This code sort of does what the OP asked, but not what the user wants to do.
Telling him that the button doesn't work, or to use the close button, is a
usability inefficiency (not to mention a PITA). You should make the X button
close the form as if the user canceled it, since that's what he obviously
wanted to do.

The following assumes there is a button called btnCancel which gracefully
closes the form and resets the application:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
btnCancel
End If
End Sub

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Mike H" wrote in message
...
Hi,

I'm not aware of a way to remove it and will be interested to see if you
get
a response but you can disable it. Right click on the userform, view code
and
paste this in:-

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "That button doesn't work"
End If
End Sub


Mike


"NateBuckley" wrote:

Hello I'm just wondering if anyone can help me with the following
question:

I have multiple user forms for which on some I wish to remove the X
button
at the top that closes the form. I'm guessing I'll have to use the
windows
API for this? I've only just started learning the fundementals of Windows
API
Calls, and thus I'm a little stuck.

I'm just wondering what I would have to do, to do this, my user form's
name
is "frm_Mod_Gen_CEmp"

Thanks in advance for any help.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form Buttons Connected to a Macro - Want to remove the button Wksht Excel Worksheet Functions 2 June 13th 07 06:58 PM
User form and Command button Marilyn Excel Discussion (Misc queries) 3 May 9th 07 12:50 AM
User form x button Greg[_27_] Excel Programming 4 May 12th 06 04:56 PM
Button to call a user form? Hru48[_3_] Excel Programming 3 September 1st 05 06:22 PM
A "previous" button on a user form Anthony Slater Excel Discussion (Misc queries) 3 November 29th 04 05:57 PM


All times are GMT +1. The time now is 06:11 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"