Customizing The Control Box

Hello,

i was wondering on how, or if it is possible to customize the control box were your minimize and maximize and the red close button are?
if anyone who's how Help will be greatly appreciated
thanks
kgoAsked:
Who is Participating?
 
JMoon5FTMConnect With a Mentor Commented:
You need to subclass your window and handle WM_NCPAINT messages, like so:

Private Declare Function SetWindowLongA lib "user32" (byval hWnd as long, byval Offset as long, byval newValue as long) as long
private declare function CallWindowProcA lib "user32" (byval pProc as long, byval hWnd as long, byval message as long, byval wParam as long, byval lParam as long) as long

Dim DefProc as Long

sub EnableCustomBorderPaint()
   DefProc = SetWindowLongA(form1.hwnd, -4, addressof MyProc)
End Sub

Function MyProc(byval hWnd as long, byval message as long, byval wParam as long, byval lParam as long) as long
   if message = WM_NCPAINT then
      'Call Windows API functions to paint the window
   else
      'Pass the message on for default handling
      MyProc = CallWindowProcA(DefProc, hWnd, message, wParam, lParam)
   end if
end function
0
 
trkcorpCommented:
Yes, total control can be attained with APIs.  Please elaborate on exactly what kind of form you want to control. (MDI, modal, child?)  Or just try some of this, I took it from an MDI form where only a Close(X) button is allowed, also note that you should control the form menu(upper left) as well as the buttons(upper right):

Option Explicit

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 GetWindowLong Lib "user32" Alias "GetWindowLongA" _
 (ByVal hWnd As Long, ByVal nIndex 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 GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long

Const MF_BYPOSITION = &H400&
Const GWL_STYLE = (-16)
Const WS_THICKFRAME = &H40000
Const WS_MINIMIZEBOX = &H20000
Const WS_MAXIMIZEBOX = &H10000
Const WS_SYSMENU = &H80000

Private Const HELP_FINDER = 11


Private Sub RemoveMenus(frm As Form, _
  remove_restore As Boolean, _
  remove_move As Boolean, _
  remove_size As Boolean, _
  remove_minimize As Boolean, _
  remove_maximize As Boolean, _
  remove_separator As Boolean, _
  remove_close As Boolean)
Dim hMenu As Long
 
' Get the form's system menu handle.
hMenu = GetSystemMenu(hWnd, False)
'Do NOT rearrange this code as it operates by relative position!
If remove_close Then DeleteMenu hMenu, 6, MF_BYPOSITION
If remove_separator Then DeleteMenu hMenu, 5, MF_BYPOSITION
If remove_maximize Then DeleteMenu hMenu, 4, MF_BYPOSITION
If remove_minimize Then DeleteMenu hMenu, 3, MF_BYPOSITION
If remove_size Then DeleteMenu hMenu, 2, MF_BYPOSITION
If remove_move Then DeleteMenu hMenu, 1, MF_BYPOSITION
If remove_restore Then DeleteMenu hMenu, 0, MF_BYPOSITION
End Sub

Private Sub MDIForm_Load()
Dim CurStyle As Long
Dim NewStyle As Long

CurStyle = GetWindowLong(Me.hWnd, GWL_STYLE)
CurStyle = CurStyle And Not (WS_MINIMIZEBOX)
CurStyle = CurStyle And Not (WS_MAXIMIZEBOX)
CurStyle = CurStyle And Not (WS_THICKFRAME)
'CurStyle = CurStyle And Not (WS_SYSMENU)

NewStyle = SetWindowLong(Me.hWnd, GWL_STYLE, CurStyle)

RemoveMenus Me, True, False, _
   True, True, True, True, False

Me.Show
End Sub


0
 
CleanupPingCommented:
kgo:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
DanRollinsCommented:
Moderator, my recommended disposition is:

    Accept JMoon5FTM's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
0
 
trkcorpCommented:
Well, I think my answer was better, more to the point and a "real" answer to the question...
0
 
JMoon5FTMCommented:
Your suggestion modifies the menu - kgo specifically asks to modify the red close button.  This requires custom painting, not menu alteration.
0
 
trkcorpCommented:
JMoon5FTM,
The question was "i was wondering on how, or if it is possible to customize the control box were your minimize and maximize and the red close button are?"
And you said "Your suggestion modifies the menu - kgo specifically asks to modify the red close button.  This requires custom painting, not menu alteration."

Well, you obviously did not try to run the code... Yes, it does modify the menu but it ALSO gives him a means of totally controlling the upper right control buttons as well - that is the first thing that occurs in the form load event.  In the example:

CurStyle = GetWindowLong(Me.hWnd, GWL_STYLE)
CurStyle = CurStyle And Not (WS_MINIMIZEBOX)
CurStyle = CurStyle And Not (WS_MAXIMIZEBOX)
CurStyle = CurStyle And Not (WS_THICKFRAME)
'CurStyle = CurStyle And Not (WS_SYSMENU)
NewStyle = SetWindowLong(Me.hWnd, GWL_STYLE, CurStyle)

removes the minimize and maximize buttons from the window and also gives him a clue as to how to disable/remove the system menu altogether - and the function call:

RemoveMenus Me, True, False, _
   True, True, True, True, False

removes all but the move and close options from the window's system menu thereby making the menu consistent with the window's control buttons.

What good does it do to disable or remove a minimize button, for example, and not alter the window's system menu to prevent using it to do the same function? If you don't, you have lost the control you tried to attain by altering the buttons... my example allows the programmer how to take complete control of the window's control functions.  It MORE than answers his question, it affords him the complete control a thinking person will ultimately seek.
0
All Courses

From novice to tech pro — start learning today.