Announcement

Collapse
No announcement yet.

Will a given string be completly displayed in a dialogs title/caption area

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

  • Will a given string be completly displayed in a dialogs title/caption area

    First question is re. terminology, is the text that appears at the top of a dialog called the title or the caption?

    Google tells me that GetSystemMetrics(SM_CXSIZE) or GetTitleBarInfo will give me the available width of title/caption area , am I on the right track? Neither seem to allow for the icon or the buttons.There is a SM_CYCAPTION but no SM_CXCAPTION?

    Poffs tells me that either DrawText or GetTextExtentPoint32 will give me the length of a given string. Any particular reason to use one or the other?

    Assuming it is possible, is there a built in mechanism to truncate a UNC path to a particular length?

    Assuming a propotional font and SDK style.

    I'd like to be able to determine if it is possible to display the path plus a version number, just the path or a truncated version of the path.

  • #2
    Originally posted by James McNab View Post

    Assuming a propotional font and SDK style.

    I'd like to be able to determine if it is possible to display the path plus a version number, just the path or a truncated version of the path.
    You can still create a GRAPHIC BITMAP and find the size of the text in the default font, or a font of your choosing.
    If you want to do it all in SDK, then you can use textmetrics to get the size of each character.
    I don't recall specifically how to do that, since I haven't used it since writing in C in 1998 or so.
    The world is strange and wonderful.*
    I reserve the right to be horrifically wrong.
    Please maintain a safe following distance.
    *wonderful sold separately.

    Comment


    • #3
      Caption compression



      Code:
      'Use José's includes.
      #COMPILE EXE '#Win 9.07 (D:\Basic\Bas\Jose Roca\Forum\Jose\Windows API Headers\1.19 (PB9.x)\uz)#
      #DIM ALL
      #INCLUDE "Win32Api.inc"
      #INCLUDE "ShlWApi.inc"
      
      'TYPE TITLEBARINFOEX DWORD               'rgrect(0) Reserved.        rgstate(0) The title bar itself  STATE_SYSTEM_UNAVAILABLE 0x00000001 The element is unavailable
      '  cbSize                       AS DWORD 'rgrect(1) Reserved.        rgstate(1) Reserved.             STATE_SYSTEM_PRESSED     0x00000008 The element is in the pressed state
      '  rcTitleBar                   AS RECT  'rgrect(2) Minimize button  rgstate(2) Minimize button.      STATE_SYSTEM_INVISIBLE   0x00008000 The element is invisible
      '  rgstate(%CCHILDREN_TITLEBAR) AS DWORD 'rgrect(3) Maximize button  rgstate(3) Maximize button.      STATE_SYSTEM_OFFSCREEN   0x00010000 The element has no visible representation
      '  rgrect(%CCHILDREN_TITLEBAR)  AS RECT  'rgrect(4) Help button.     rgstate(4) Help button.          STATE_SYSTEM_FOCUSABLE   0x00100000 The element can accept the focus
      'END TYPE 'CCHILDREN_TITLEBAR = 5        'rgrect(5) Close button.    rgstate(5) Close button.
      
      GLOBAL hDlg AS DWORD
      
      %LabelInfo = 101
      '______________________________________________________________________________
      
      FUNCTION StateInfo(s AS DWORD) AS STRING
      
       '0 The title bar itself, 1 Reserved, 2 Minimize button, 3 Maximize button, 4 Help button, 5 Close button.
       'STATE_SYSTEM_OFFSCREEN mean the element has no visible representation.
       FUNCTION = $TAB & "(" & HEX$(s, 8) & ") " & _
                  IIF$((s AND %STATE_SYSTEM_PRESSED),     "PRESSED",     "unPressed") & ", " & _
                  IIF$((s AND %STATE_SYSTEM_UNAVAILABLE), "UNAVAILABLE", "available") & ", " & _
                  IIF$((s AND %STATE_SYSTEM_OFFSCREEN),   "OFFSCREEN",   "onScreen")  & ", " & _
                  IIF$((s AND %STATE_SYSTEM_INVISIBLE),   "INVISIBLE",   "visible")   & ", " & _
                  IIF$((s AND %STATE_SYSTEM_FOCUSABLE),   "FOCUSABLE",   "unFocusable")
      END FUNCTION
      '______________________________________________________________________________
      
      FUNCTION RectInfo(r AS RECT) AS STRING
      
       MapWindowPoints(%HWND_DESKTOP, hDlg, BYVAL VARPTR(r), 2) 'Relative to client area instead of desktop area
       '.rgrect(0) and .rgrect(1) are reserved.
       FUNCTION = STR$(r.nRight - r.nLeft) & " x" & _
                  STR$(r.nBottom - r.nTop) & " at " & STR$(r.nLeft) & ", " & STR$(r.nTop)
      
      END FUNCTION
      '______________________________________________________________________________
      
      FUNCTION GetTitleBarInf() AS LONG
       LOCAL  TBI            AS TITLEBARINFOEX
       LOCAL  DialogRect     AS RECT
       LOCAL  sTitleInfo     AS STRING
       STATIC sTitleInfoPrev AS STRING
       LOCAL  CaptionWidth   AS LONG
      
       TBI.cbSize = SIZEOF(TITLEBARINFOEX)
       IF SendMessage(hDlg, %WM_GETTITLEBARINFOEX, 0, VARPTR(TBI)) THEN
      
         GetWindowRect(hDlg, DialogRect)
      
         CaptionWidth = TBI.rcTitleBar.nRight - TBI.rcTitleBar.nLeft - _
                        (TBI.rgrect(2).nRight - TBI.rgrect(2).nLeft) - _ 'Minimize button
                        (TBI.rgrect(3).nRight - TBI.rgrect(3).nLeft) - _ 'Maximize button
                        (TBI.rgrect(4).nRight - TBI.rgrect(4).nLeft) - _ 'Help button
                        (TBI.rgrect(5).nRight - TBI.rgrect(5).nLeft)     'Close button
      
         sTitleInfo = $CRLF & _
                      "Truncates a file path to fit a dialog caption." & $CRLF & _
                      "Using WM_GETTITLEBARINFOEX, GetTextExtentPoint32, " & $CRLF & _
                      "SystemParametersInfo(.lfCaptionFont), and PathCompactPath." & $CRLF & _
                      $CRLF & _
                      "Resize the dialog to see the path in caption shrinking or expanding..." & $CRLF & _
                      $CRLF & _
                      "Extra code: You may click dialog caption button to see their state." & $CRLF & _
                      $CRLF & _
                      $CRLF & _
                      "- WM_GETTITLEBARINFOEX infos..." & $CRLF & _
                      "Dialog width "    & $TAB & $TAB & STR$(DialogRect.nRight - DialogRect.nLeft) & $CRLF & _
                      "Text caption width "     & $TAB & STR$(CaptionWidth)  & " (Excluding minimize, maximize, close, and help button)" & $CRLF & _
                      "TitleBar " & $TAB & $TAB & $TAB & RectInfo(TBI.rcTitleBar) & $CRLF & _
                      "Minimize"         & $TAB & $TAB & RectInfo(TBI.rgrect(2))  & $CRLF & _
                      "Maximize "        & $TAB & $TAB & RectInfo(TBI.rgrect(3))  & $CRLF & _
                      "Help "     & $TAB & $TAB & $TAB & RectInfo(TBI.rgrect(4))  & $CRLF & _
                      "Close "    & $TAB & $TAB & $TAB & RectInfo(TBI.rgrect(5))  & $CRLF & _
                      $CRLF & _
                      "TitleBar " & $TAB & $TAB & StateInfo(TBI.rgstate(0)) & $CRLF & _
                      "Minimize button " & $TAB & StateInfo(TBI.rgstate(2)) & $CRLF & _
                      "Maximize button " & $TAB & StateInfo(TBI.rgstate(3)) & $CRLF & _
                      "Help button "     & $TAB & StateInfo(TBI.rgstate(4)) & $CRLF & _
                      "Close button "    & $TAB & StateInfo(TBI.rgstate(5))
      
         IF sTitleInfo <> sTitleInfoPrev THEN
           CONTROL SET TEXT hDlg, %LabelInfo, sTitleInfo
         END IF
         sTitleInfoPrev = sTitleInfo
       END IF
      
       FUNCTION = CaptionWidth - 10
      
      END FUNCTION
      '______________________________________________________________________________
      
      CALLBACK FUNCTION DlgProc
       LOCAL CaptionSpec          AS NONCLIENTMETRICS
       LOCAL CaptionFixedTextSize AS SIZEL
       LOCAL zCaptionPath         AS ASCIIZ * %MAX_PATH
       LOCAL sCaptionFixed        AS STRING
       LOCAL hDc                  AS DWORD
       LOCAL hCaptionFont         AS DWORD
       LOCAL hCaptionFontGenuine  AS DWORD
       LOCAL CaptiontextFullWidth AS DWORD
      
       SELECT CASE CBMSG
      
         CASE %WM_APP
           sCaptionFixed       = "Uncompressible text part - "
           zCaptionPath        = "D:\Basic\Compressible file name\CaptionTitleBar\WindowCaptionTextWidth01.bas"
           hDc                 = GetDC(hDlg)
           CaptionSpec.cbSize  = SIZEOF(NONCLIENTMETRICS)
           SystemParametersInfo(%SPI_GETNONCLIENTMETRICS, CaptionSpec.cbSize, BYVAL VARPTR(CaptionSpec), 0) 'Get caption font used
           hCaptionFont        = CreateFontIndirect(CaptionSpec.lfCaptionFont)                              'Use same font as in caption
           hCaptionFontGenuine = SelectObject(hdc, hCaptionFont)                                            'Select the font
           GetTextExtentPoint32(hDc, BYVAL STRPTR(sCaptionFixed), LEN(sCaptionFixed), CaptionFixedTextSize) 'Measure uncompressible text
           CaptiontextFullWidth = GetTitleBarInf()                                                          'Get total text caption width
           PathCompactPath(hDc, zCaptionPath, CaptiontextFullWidth - CaptionFixedTextSize.cx)               'Compress the path to fit
           SelectObject(hdc, hCaptionFontGenuine)                                                           'Clean up
           DeleteObject(hCaptionFont)                                                                       'Clean up
           ReleaseDC(hDlg, hDc)                                                                             'Clean up
           SetWindowText(hDlg, sCaptionFixed & zCaptionPath)                                                'Update caption
      
         CASE %WM_NCLBUTTONDOWN, %WM_CAPTURECHANGED
           GetTitleBarInf() 'Update info on caption left click down and dialog capture release received on click up
      
         CASE %WM_SIZE 'Will be called on sizing and also on program start, so it will init text info
           IF CBWPARAM <> %SIZE_MINIMIZED THEN
             GetTitleBarInf()
             PostMessage(hDlg, %WM_APP, 0, 0) 'First init
           END IF
      
        END SELECT
      
      END FUNCTION
      '______________________________________________________________________________
      
      FUNCTION PBMAIN()
       LOCAL hIcon AS DWORD
      
       DIALOG FONT "Segoe UI", 9
       DIALOG NEW %HWND_DESKTOP, "", , , 350, 210, %WS_CAPTION OR _
       %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_SYSMENU OR %WS_THICKFRAME, 0 TO hDlg
      
       hIcon = ExtractIcon(GetModuleHandle(""), "Shell32.dll", 294) 'o
       SetClassLong(hDlg, %GCL_HICON, hIcon)
       SendMessage(hDlg, %WM_SETICON, %ICON_SMALL, hIcon)
      
       CONTROL ADD LABEL, hDlg, %LabelInfo, "", 5, 5, 340, 200
      
       DIALOG SHOW MODAL hDlg CALL DlgProc
      
       DestroyIcon(hIcon)
      
      END FUNCTION
      '______________________________________________________________________________
      '
      Last edited by Pierre Bellisle; 12 Jan 2019, 11:40 AM.

      Comment


      • #4
        Pierre's sample works with PBWin10 includes too.

        Some other techniques to truncate long pathnames were discussed in this thread.
        Rgds, Dave

        Comment


        • #5
          Code was updated...

          Still have to double check stuff like...
          Code:
          NonClientMetricsTry.lfCaptionFont.lfHeight = -22
          SystemParametersInfo(%SPI_SETNONCLIENTMETRICS, SIZEOF(NONCLIENTMETRICS), BYVAL VARPTR(NonClientMetricsTry), %SPIF_SENDCHANGE)
          Last edited by Pierre Bellisle; 12 Jan 2019, 02:15 AM.

          Comment


          • #6
            Pierre, I don't know what to say, it is one thing to dig out a snippet but to create a working program that totally resolves the problem, really is above and beyond.

            Dave, I used Poffs repeatedly but failed to find that thread, thanks.

            Comment


            • #7
              Originally posted by James McNab View Post
              Pierre, I don't know what to say, it is one thing to dig out a snippet but to create a working program that totally resolves the problem, really is above and beyond.

              Dave, I used Poffs repeatedly but failed to find that thread, thanks.
              I see two possibilities.
              Either these things are so easy for Pierre that it is nothing for him to do it,
              or he takes the time to make the effort at a greater personal expense.
              Either is quite impressive.
              The world is strange and wonderful.*
              I reserve the right to be horrifically wrong.
              Please maintain a safe following distance.
              *wonderful sold separately.

              Comment


              • #8
                I got no merit, only stubborn deoxyribonucleic acid molecules. Life is good!

                Comment


                • #9
                  Originally posted by Kurt Kuzba View Post
                  ...... Either is quite impressive.
                  It certainly is!

                  Comment


                  • #10
                    Code updated...

                    Q> ...am I on the right track?
                    Forgot to answer... Yes you where, your pre-code analysis was right on the spot...
                    Last edited by Pierre Bellisle; 12 Jan 2019, 06:59 PM.

                    Comment

                    Working...
                    X