Jump to content

Help/Suggestions needed for dynamic ellipses for text inside labels.


Recommended Posts

So let me paint a picture of my GUI as it currently stands: 2 labels and 1 button. The first label is static and is simply there as a legitimate label to describe what the second label is. In this example, the label is "Current Folder: ". The button opens up FileSelectFolder() method so that the user can select the requested folder. The second label dynamically updates to the currently selected folder as a visual aid to the user to let them know they have indeed selected the correct folder. Now here's my dilemma: if the folder location is longer than label 2, it chops off the rest of the folder name. Functionally this is fine as I know it saves the name correctly. But, what I'd rather it do is add ellipses at the end as an indicator that the file name goes on longer than what is shown.

In my tests, this is not possible with a normal GUI label, but if anyone happens to know how to do it I'm very interested in the solution. I did find the _WinAPI_DrawText method, but I'm in completely new territory with this and don't have the foggiest idea how to get started using it. I don't know if I can simply send my label to this, or if I have to completely replace my label with this or what.

If anyone has experience with this specific functionality I'd be extremely appreciative to hear/read the solution.

Link to comment
Share on other sites

You'd need to use something like this.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$sPath = "C:\I am a very long path\Which needs to be\Shortened to fit\In the label.txt"

$hGUI = GUICreate("Test", 500, 500)

GUICtrlCreateLabel("Using $DT_PATH_ELLIPSIS:", 10, 10, 200, 20)
GUICtrlCreateLabel($sPath, 10, 40, 300, 30, $DT_PATH_ELLIPSIS)
GUICtrlSetBkColor(-1, 0xCCCCFF)
GUICtrlCreateLabel("MSDN says:" & @CRLF & _
                   "'replaces characters in the middle of the string with ellipses so that the result fits in the specified rectangle'", 10, 80, 480, 40)

GUICtrlCreateLabel("Using $DT_END_ELLIPSIS:", 10, 200, 200, 20)
GUICtrlCreateLabel($sPath, 10, 230, 300, 30, $DT_END_ELLIPSIS)
GUICtrlSetBkColor(-1, 0xCCFFCC)
GUICtrlCreateLabel("MSDN says:" & @CRLF & _
                   "'if the end of a string does not fit in the rectangle, it is truncated and ellipses are added'", 10, 270, 480, 40)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Note though that the *_ELLIPSIS variables are working backwards from what they're supposed to do. That's a Windows bug, and nothing to do with AutoIt.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Oh wow. This was EXACTLY what I needed. Where would I go to find this kind of reference? I couldn't seem to find it anywhere in the help files at all.

Now this leads to a few other questions :)

1. How do I update Koda based on my code updates? I can update the code after I make changes in Koda, but I don't see where/how I can update the form designer based on custom coding.

2. Part of my GUI has a ListView, and the first column in the ListView needs to be a checkbox. I'd like to add functionality to delete whatever is checked, but I don't know how to poll the checkbox from inside the ListView. My ListView creation code looks like this: 

$ListView1 = GUICtrlCreateListView("Select|Macro|Hotkey|Keybind", 8, 280, 594, 134, -1, BitOR($WS_EX_CLIENTEDGE,$LVS_EX_CHECKBOXES))

3. Lastly: How can I center justify just that checkbox? I know how to center the text for the headers, but that's not exactly what I want for the checkboxes. I'd like the physical checkbox centered in their column. Currently, it's justified full left, which looks a bit awkward to me.

Link to comment
Share on other sites

1) Sorry, I have no idea. I haven't used Koda in years

2) Use _GUICtrlListView_GetItemChecked in a loop to find out which items have been checked.

3) As far as I am aware of, there's no way to center the checkbox in the listview column.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

It's possible to center the checkbox with a custom drawn listview. The unchecked and checked checkbox icons can be extracted from the state image list as index 0 and 1.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>

Global $hLV, $hIL, $hDC, $iCol0Width

Example()

Func Example()
  GUICreate("ListView", 250, 250, 100, 200)

  Local $idLV = GUICtrlCreateListView( "col0                |col1|col2|col3", 10, 10, 230, 230 )
  GUICtrlCreateListViewItem( "|item0|col02|col03", $idLV )
  GUICtrlCreateListViewItem( "|item1|col12|col13", $idLV )
  GUICtrlCreateListViewItem( "|item2|col22|col23", $idLV )
  GUICtrlCreateListViewItem( "|item3|col32|col33", $idLV )
  GUICtrlCreateListViewItem( "|item4|col42|col43", $idLV )
  $hLV = GUICtrlGetHandle( $idLV )

  ; Checkboxes
  Local $iExStyle = _GUICtrlListView_GetExtendedListViewStyle( $hLV )
  _GUICtrlListView_SetExtendedListViewStyle( $hLV, BitOR( $iExStyle, $LVS_EX_CHECKBOXES ) )

  ; State image list
  $hIL = _GUICtrlListView_GetImageList( $hLV, 2 ) ; 2 => Image list with state images

  ; Width of column 0
  $iCol0Width = _GUICtrlListView_GetColumnWidth( $hLV, 0 )

  ; ListView device context
  $hDC = _WinAPI_GetDC( $hLV )

  GUIRegisterMsg( $WM_NOTIFY, "WM_NOTIFY" )
  GUISetState( @SW_SHOW )

  While 1
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
  WEnd

  _WinAPI_ReleaseDC( $hLV, $hDC )
EndFunc


Func WM_NOTIFY( $hWnd, $iMsg, $wParam, $lParam )
  #forceref $hWnd, $iMsg, $wParam
  Local $tNMHDR, $hWndFrom, $iCode
  $tNMHDR = DllStructCreate( $tagNMHDR, $lParam )
  $hWndFrom = HWnd( DllStructGetData( $tNMHDR, "hWndFrom" ) )
  $iCode = DllStructGetData( $tNMHDR, "Code" )

  Switch $hWndFrom
    Case $hLV
      Switch $iCode
        Case $NM_CUSTOMDRAW
          Local $tNMLVCUSTOMDRAW = DllStructCreate( $tagNMLVCUSTOMDRAW, $lParam )
          Local $dwDrawStage = DllStructGetData( $tNMLVCUSTOMDRAW, "dwDrawStage" )
          Switch $dwDrawStage                ; Holds a value that specifies the drawing stage
            Case $CDDS_PREPAINT
              ; Before the paint cycle begins
              Return $CDRF_NOTIFYITEMDRAW    ; Notify the parent window of any item-related drawing operations

            Case $CDDS_ITEMPREPAINT
              ; Before painting an item
              Return $CDRF_NOTIFYSUBITEMDRAW ; Notify the parent window of any subitem-related drawing operations

            Case BitOR( $CDDS_ITEMPREPAINT, $CDDS_SUBITEM )
              ; Before painting a subitem
              Local $iSubItem = DllStructGetData( $tNMLVCUSTOMDRAW, "iSubItem" )     ; Subitem index
              If $iSubItem = 0 Then
                Local $iItem = DllStructGetData( $tNMLVCUSTOMDRAW, "dwItemSpec" )    ; Item index
                Local $aRect = _GUICtrlListView_GetSubItemRect( $hLV, $iItem, 1, 0 ) ; $aRect[1] = item y-coordinate
                ; Icon index in state image list: 0 => unchecked, 1 => checked
                Local $hIcon = _GUIImageList_GetIcon( $hIL, _GUICtrlListView_GetItemChecked( $hLV, $iItem ) )
                _WinAPI_DrawIconEx( $hDC, ( $iCol0Width - 16 ) / 2, $aRect[1], $hIcon, 16, 16 )
                _GUIImageList_DestroyIcon( $hIcon )
                Return $CDRF_SKIPDEFAULT      ; Skip default drawing operations
              EndIf
              Return $GUI_RUNDEFMSG
          EndSwitch

        Case $NM_CLICK
          Local $aTest = _GUICtrlListView_SubItemHitTest( $hLV )
          If $aTest[0] > -1 And $aTest[1] = 0 And Not $aTest[5] Then _
            _GUICtrlListView_SetItemChecked( $hLV, $aTest[0], Not _GUICtrlListView_GetItemChecked( $hLV, $aTest[0] ) )
      EndSwitch
  EndSwitch
  Return $GUI_RUNDEFMSG
EndFunc

 

Link to comment
Share on other sites

Is it better to use a lot more custom stuff like this as opposed to using Koda? Koda "can" be kind of handy, particularly as I'm just getting started with UI stuff, but it's very high maintenance. I've gone in and made size changes directly in the code, but that is never reflected when I go back into Koda. This leads to a pretty frustrating experience. It's not nearly as seamless as C# and Visual Studio.

Thanks to both of you for those examples. I'll be experimenting to get them in use for my application.

Is it considered bad form to continue asking questions, in particular that's not UI related? I'm experiencing something strange on my code base and wanted to get input on it as well, but I recognize that on many forums they'd prefer this be asked in the correct forum instead of adding addendum questions of differing topics to the same topic thread.

Link to comment
Share on other sites

  • Moderators

RamboMadCow,

If the new question is unrelated then please just open a new thread in the correct section - you can always link back to this one if necessary.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...