Word2000: AppendMenu API to add Minimize Button to Title Bar

V

VBA Coder

Has anyone had any success using the AppendMenu API function to add the
Minimize and Maximize button to a Word Form title bar? My sample code is
below which does not seem to add anything.

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal
hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal
lpNewItem As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA"
(ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long)
As Long

' Window styles constants
Private Const WS_MINIMIZEBOX As Long = &H20000 'Style to add a Minimize
box on the title bar
Private Const WS_MAXIMIZEBOX As Long = &H10000 'Style to add a Maximize
box to the title bar

Public Function ShowMinMaxButtons(sTitle As String)
Dim sClass As String
Dim lAppHandle As Long
Dim hWndForm As Long

' Get the Handle to the Form that we want to add the button to. Use the
Caption of the Form (sTitle).
hWndForm = FindWindow(vbNullString, sTitle)
If hWndForm = 0 Then Exit Function

' Get the Class Name of the Window Handle
sClass = sClassName(hWndForm)
lAppHandle = FindWindow(sClass, vbNullString)

AppendMenu hWndForm, WS_MINIMIZEBOX , 1, "Minimze"
AppendMenu hWndForm, WS_MAXIMIZEBOX , 2, "Maximize"

End Function


Public Function sClassName(ByVal hWindow As Long) As String
Dim lLen As Long
Dim sClass As String * 128

lLen = GetClassName(hWindow, sClass, 127)
If lLen Then
sClassName = Left$(sClass, lLen)
Else
sClassName = ""
End If

End Function
 
V

VBA Coder

With a little help from the following Excel example,
http://www.bmsltd.co.uk/Excel/Default.htm, I was able to get it to work in
Word, but not by using the AppendMenu API function, rather, I used the
SetWindowLong API instead. My General Declarations of the API functions and
Constants, along with my subroutine is below:

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&, ByVal
bRevert&) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal
nCmdShow As Long) As Long
Private Declare Function SetFocus Lib "user32" (ByVal hWnd As Long) As Long

Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

' Window styles constants
Private Const GWL_STYLE As Long = (-16) 'The offset of a window's
style
Private Const GWL_EXSTYLE As Long = (-20) 'The offset of a window's
extended style
Private Const WS_CAPTION As Long = &HC00000 'Style to add a titlebar
Private Const WS_SYSMENU As Long = &H80000 'Style to add a system
menu
Private Const WS_THICKFRAME As Long = &H40000 'Style to add a sizable
frame
Private Const WS_MINIMIZEBOX As Long = &H20000 'Style to add a Minimize
box on the title bar
Private Const WS_MAXIMIZEBOX As Long = &H10000 'Style to add a Maximize
box to the title bar
Private Const WS_POPUP As Long = &H80000000 'Standard option, cleared
when showing a task bar icon
Private Const WS_VISIBLE As Long = &H10000000 'Standard option, cleared
when showing a task bar icon
Private Const WS_EX_DLGMODALFRAME As Long = &H1 'Controls if the window
has an icon
Private Const WS_EX_APPWINDOW As Long = &H40000 'Application Window: shown
on taskbar
Private Const WS_EX_TOOLWINDOW As Long = &H80 'Tool Window: small
titlebar

'Constants for hide or show a window
Private Const SW_HIDE As Long = 0
Private Const SW_SHOW As Long = 5



Public Sub ShowMinMaxButtons(sCaption As String)
Dim lStyle As Long, hMenu As Long
Dim bCaption As Boolean, bSysMenu As Boolean, bSizeable As Boolean
Dim bMinimize As Boolean, bMaximize As Boolean
Dim bAppWindow As Boolean, bIcon As Boolean
Dim bToolWindow As Boolean, sClass As String

' Get the Form Handle based on the Class Name of the form - use the Caption
of the Form
hWndForm = FindWindow(vbNullString, sCaption)
If hWndForm = 0 Then Exit Sub

' Get the Class Name of the Window Handle by calling the sClassName function
sClass = sClassName(hWndForm)
hWndForm = FindWindow(sClass, sCaption)

' Old method - this shows what the Class Name should be
'If Val(Application.Version) < 9 Then
' hWndForm = FindWindow("ThunderXFrame", sCaption) 'Word97
'Else
' hWndForm = FindWindow("ThunderDFrame", sCaption) 'Word2000
'End If

' Check if we have a Form Handle
If hWndForm = 0 Then Exit Sub

lStyle = GetWindowLong(hWndForm, GWL_STYLE)

'Build up the basic window style flags for the form
bCaption = True
bSysMenu = True
bSizeable = True
bMinimize = True
bMaximize = True
bAppWindow = False

If bCaption Then lStyle = lStyle Or WS_CAPTION Else lStyle = lStyle And Not
WS_CAPTION
If bSysMenu Then lStyle = lStyle Or WS_SYSMENU Else lStyle = lStyle And Not
WS_SYSMENU
If bSizeable Then lStyle = lStyle Or WS_THICKFRAME Else lStyle = lStyle And
Not WS_THICKFRAME
If bMinimize Then lStyle = lStyle Or WS_MINIMIZEBOX Else lStyle = lStyle And
Not WS_MINIMIZEBOX
If bMaximize Then lStyle = lStyle Or WS_MAXIMIZEBOX Else lStyle = lStyle And
Not WS_MAXIMIZEBOX
If bAppWindow Then lStyle = lStyle And Not WS_VISIBLE And Not WS_POPUP Else
lStyle = lStyle Or WS_VISIBLE Or WS_POPUP

'Set the basic window styles
SetWindowLong hWndForm, GWL_STYLE, lStyle

lStyle = GetWindowLong(hWndForm, GWL_EXSTYLE)

'Build up and set the extended window style
bIcon = False ' Set to False if you want an icon to show - opposite of what
you'd think
bAppWindow = False
bToolWindow = False

If bIcon Then lStyle = lStyle Or WS_EX_DLGMODALFRAME Else lStyle = lStyle
And Not WS_EX_DLGMODALFRAME
If bAppWindow Then lStyle = lStyle Or WS_EX_APPWINDOW Else lStyle = lStyle
And Not WS_EX_APPWINDOW
If bToolWindow Then lStyle = lStyle Or WS_EX_TOOLWINDOW Else lStyle = lStyle
And Not WS_EX_TOOLWINDOW

SetWindowLong hWndForm, GWL_EXSTYLE, lStyle

' Hide the Window first so it repaints (refreshes) the form properly
ShowWindow hWndForm, SW_HIDE
' Now Show the window with the changes
ShowWindow hWndForm, SW_SHOW

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top