Announcement

Collapse
No announcement yet.

Programmatically Change Menu Font Size

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Programmatically Change Menu Font Size

    Title says it all, but in case it doesn't..
    Is there a way to change the font size of a Menu created with MENU NEW, etc. from within the program and applicable only to that program?
    I know about methods (registry hacks, etc.) to do it globally which could be accomplished from within an application, but I don't think that's a good idea.

    Thanks,
    Bill

  • #2
    Can be done using OwnerDrawn menu. Erik Christensen once posted some very fine examples both for SDK and DDT menus - see https://forum.powerbasic.com/forum/u...m-text-strings

    Example how it looks:
    Click image for larger version

Name:	MenuFont.jpg
Views:	72
Size:	6.5 KB
ID:	796222

    Comment


    • #3
      Borje,

      Is there a way to use the handle provided by FONT NEW instead of the one which results from CreateFont?
      I already have the required font handles from FONT NEW.

      Thanks so much,
      Bill

      Comment


      • #4
        No, FONT NEW only returns a number that PB uses internally for graphics and xprint commands. Must use "real" font handles returned from CreateFont. I use this fuction for this purpose:
        '
        Code:
        '====================================================================
        ' Create a desirable font and return its handle. Original code by Dave Navarro
        ' NOTE: enhanced with proper enumeration of character set via EnumCharSet
        ' call like: hFont = MakeFontEx("Times New Roman", 10, %FW_BOLD, 0, 0)
        '--------------------------------------------------------------------
        FUNCTION MakeFontEx(BYVAL FontName AS STRING, BYVAL PointSize AS LONG, BYVAL fBold AS LONG, _
                            BYVAL fItalic AS LONG, BYVAL fUnderline AS LONG) AS DWORD
          LOCAL hDC AS DWORD, CharSet AS LONG, CyPixels AS LONG
        
          hDC = GetDC(%HWND_DESKTOP)
            CyPixels  = GetDeviceCaps(hDC, %LOGPIXELSY)
            EnumFontFamilies hDC, BYVAL STRPTR(FontName), CODEPTR(EnumCharSet), BYVAL VARPTR(CharSet)
          ReleaseDC %HWND_DESKTOP, hDC
          PointSize = 0 - (PointSize * CyPixels) \ 72
        
          FUNCTION = CreateFont(PointSize, 0, _  'height, width(default=0)
                     0, 0, _                     'escapement(angle), orientation
                     fBold, _                    'weight (%FW_DONTCARE = 0, %FW_NORMAL = 400, %FW_BOLD = 700)
                     fItalic, _                  'Italic
                     fUnderline, _               'Underline
                     %FALSE, _                   'StrikeThru
                     CharSet, %OUT_TT_PRECIS, _
                     %CLIP_DEFAULT_PRECIS, %DEFAULT_QUALITY, _
                     %FF_DONTCARE , BYCOPY FontName)
        END FUNCTION
        '====================================================================
        ' Get type of character set - ansi, symbol.. a must for some fonts.
        '--------------------------------------------------------------------
        FUNCTION EnumCharSet (elf AS ENUMLOGFONT, ntm AS NEWTEXTMETRIC, _
                                 BYVAL FontType AS LONG, CharSet AS LONG) AS LONG
          CharSet = elf.elfLogFont.lfCharSet
        END FUNCTION
        '

        Comment


        • #5
          No, FONT NEW only returns a number that PB uses internally for graphics and xprint commands.
          And CONTROL SET FONT
          Dale

          Comment


          • #6

            Only one more question (I hope):
            How can I get balloon style tooltips to appear appropriately for this owner-drawn Menu?
            I have them (tooltips) working for every other control I use, but I haven't been able to get them working for Menus.


            Comment


            • #7
              s there a way to use the handle provided by FONT NEW instead of the one which results from CreateFont?

              Sounds like a new feature suggestion: "Provide function to get Windows GDI font handle from PB font handle." Maybe the companiin function, too: "Create function to convert a Windows' GDI funto handle to a font handle usable with PB 'GRAPHICS" statements.

              But for now...

              Create a hidden control. Use PB Graphics commands to create a font and assign to the control. Send WM_GETFONT message to control ... which returns a Windows Handle.



              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                How can I get balloon style tooltips to appear appropriately for this owner-drawn Menu?
                AFAIK Menus are not designed for tooltips. You will have to "CreateWindow" using a suitable style and show the window as needed. Fortunately, all PB programmers at some point have learned how to create Windows without the use of DDT so you can probably knock that off pretty quickly. .

                Note that with menus it is relatively common to put helpful text in the window's status bar (usually at the bottom of the client are) as the user "surfs" his/her menu options. Exercise left to user, although hints could be forthcoming on request.

                That, or since the menu is owner-drawn anyway, paint something which looks like a "tooltip" when you paint the menu item.

                MCM
                Michael Mattias
                Tal Systems Inc. (retired)
                Racine WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  For top menu items one can use GetMenuItemRect + MapWindowPoints to get each item's RECT (top menu items has no ID, but has position, starting with 0 for 1:st item, etc. - and rc.nTop will be negative, above client area) and then use that to set tooltip area. For dropped menu items, maybe the WM_MENUSELECT message can be used, but don't know how and think it would become messy. Like Michael says, displaying info text in StatusBar is better and more usual way. Can do example later, but is "busy" watching a movie right now..

                  Comment


                  • #10
                    Ok, wrapped up a small example. Not complete, but shows a way to get tooltip for top menu items and a way to use StatusBar for dropped menu items info. Didn't investigate how to call up tooltip for dropped menu items, but can imagine it could cause trouble - as it does for top menu items too, because tooltip sometimes sort of conflicts with standard "mouse-over-menu-item" behavious.
                    Click image for larger version  Name:	MenuToolTop.jpg Views:	0 Size:	6.2 KB ID:	796325
                    '
                    Code:
                    #COMPILE EXE
                    #DIM ALL
                    #INCLUDE "WIN32API.INC"
                    %IDC_STATUSBAR = 250
                    %IDM_OPEN = %WM_USER + 500
                    %IDM_SAVE = %WM_USER + 501
                    
                    '====================================================================
                    FUNCTION PBMAIN()
                      LOCAL hDlg AS DWORD
                    
                      DIALOG NEW 0, "Menu ToolTip Test", , , 120, 60, _
                                 %WS_CAPTION OR %WS_SYSMENU, 0 TO hDlg
                      CONTROL ADD STATUSBAR, hDlg, %IDC_STATUSBAR, "", 0,0,0,0
                      '------------------------------------------------------------------
                    
                      DIALOG SHOW MODAL hDlg CALL DlgProc()
                    END FUNCTION
                    
                    '====================================================================
                    CALLBACK FUNCTION DlgProc() AS LONG
                      LOCAL hMenu, hSubMenu AS DWORD, sText AS STRING, rc AS RECT
                    
                      SELECT CASE AS LONG CB.MSG
                      CASE %WM_INITDIALOG
                          '--------------------------------------------------------------
                          MENU NEW BAR TO hMenu
                          MENU ATTACH hMenu, CB.HNDL
                          MENU NEW POPUP TO hSubMenu
                          MENU ADD POPUP, hMenu, "&File", hSubMenu, %MF_ENABLED
                            MENU ADD STRING, hSubMenu, "&Open", %IDM_OPEN, %MF_ENABLED
                            MENU ADD STRING, hSubMenu, "&Save", %IDM_SAVE, %MF_ENABLED
                          MENU NEW POPUP TO hSubMenu
                          MENU ADD POPUP, hMenu, "&Edit", hSubMenu, %MF_ENABLED
                          MENU ATTACH hMenu, CB.HNDL
                          '--------------------------------------------------------------
                          GetMenuItemRect(CB.HNDL, hMenu, 0, rc)  ' top menu item 0 (here &File)
                          MapWindowPoints(0, CB.HNDL, BYVAL VARPTR(rc), 2)
                          SetToolTipsArea CB.HNDL, rc, "Top item 1"
                          '--------------------------------------------------------------
                          GetMenuItemRect(CB.HNDL, hMenu, 1, rc)  ' top menu item 1 (here &Save)
                          MapWindowPoints(0, CB.HNDL, BYVAL VARPTR(rc), 2)
                          SetToolTipsArea CB.HNDL, rc, "Top item 2"
                          '--------------------------------------------------------------
                    
                      CASE %WM_MENUSELECT
                        SELECT CASE LO(WORD, CB.WPARAM)
                        CASE %IDM_OPEN : sText = " Open file"
                        CASE %IDM_SAVE : sText = " Save file"
                        CASE ELSE      : sText = ""
                        END SELECT
                        STATUSBAR SET TEXT CB.HNDL, %IDC_STATUSBAR, 1, 0, sText
                    
                      END SELECT
                    
                    END FUNCTION
                    
                    '====================================================================
                    ' Set tooltips for any given area in a dialog or control
                    ' (I like %TTS_BALLOON style - remove if you don't like it)
                    '--------------------------------------------------------------------
                    FUNCTION SetToolTipsArea (BYVAL hWnd AS DWORD, rc AS RECT, BYVAL sTxt AS STRING) AS LONG
                      LOCAL hInst AS DWORD, ti AS TOOLINFO
                      STATIC hToolTip AS DWORD
                    
                      hInst = GetModuleHandle("")
                    
                      IF hToolTip  = 0 THEN
                          InitCommonControls 'Make sure the common controls library is loaded
                          hToolTip = CreateWindowEx(0, "tooltips_class32", "", _
                                                    %TTS_ALWAYSTIP OR %TTS_BALLOON, _
                                                    0,0,0,0,0, BYVAL 0, hInst, BYVAL 0)
                      END IF
                    
                      ti.cbSize   = SIZEOF (ti)
                      ti.uFlags   = %TTF_SUBCLASS OR %TTF_CENTERTIP
                      ti.hwnd     = hWnd
                      ti.rrect    = rc  '<- may be defined as ti.rect in some include files..
                      ti.hinst    = hInst
                      ti.lpszText = STRPTR(sTxt)
                    
                      FUNCTION = SendMessage(hToolTip, %TTM_ADDTOOL, 0, BYVAL VARPTR(ti))
                    
                    END FUNCTION
                    '

                    Comment

                    Working...
                    X