Jump to content

Tab size in Edit


therks
 Share

Recommended Posts

Is there any way to change the size/length of a tab character in an edit control created with GUICtrlCreateEdit?

For those that don't know (I didn't for the longest time) you can enter a tab in your edit controls by pressing Ctrl+Tab (just pressing tab would jump to the next control of course), but the tab is 8 characters long (using Courier New as the font). That's too big for me, I prefer my tabs to be about 4 characters long.

Anyway, just wondering if this is a possibility with regular edit controls. I won't be surprised if it's not, I know Notepad's tab sizes are also 8 characters, and you can't adjust those.

Link to comment
Share on other sites

This is from the Libraries I'm working on, you should be able to do what you want with the following

#include <Misc.au3>

Global Const $EM_SETTABSTOPS         = 0xCB

; #FUNCTION# ====================================================================================================================
; Name...........: _GUICtrlEdit_SetTabStops
; Description ...: Sets the tab stops
; Syntax.........: _GUICtrlEdit_SetTabStops($hWnd, $aTabStops)
; Parameters ....: $hWnd        - Handle to the control
;                  $aTabStops   - Array of tab stops in the following format:
;                  |[0]         - Tab stop 1
;                  |[2]         - Tab stop 2
;                  |[n]         - Tab stop n
; Return values .: True         - All the tabs are set
;                  False        - All the tabs are not set
; Author ........: Gary Frost (gafrost)
; Modified.......:
; Remarks .......:
; Related .......:
; ===============================================================================================================================
Func _GUICtrlEdit_SetTabStops($hWnd, $aTabStops)
;~  If $Debug_Ed Then _GUICtrlEdit_ValidateClassName($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    
    If Not IsArray($aTabStops) Then Return SetError(-1, -1, False)
    
    Local $iNumTabStops, $tTabStops, $sTabStops, $iResult
    
    $iNumTabStops = UBound($aTabStops)
    
    For $x = 0 To $iNumTabStops - 1
        $sTabStops &= "int;"
    Next
    $sTabStops = StringTrimRight($sTabStops, 1)
    $tTabStops = DllStructCreate($sTabStops)
    For $x = 0 To $iNumTabStops - 1
        DllStructSetData($tTabStops, $x + 1, $aTabStops[$x])
    Next
    $iResult = _SendMessage($hWnd, $EM_SETTABSTOPS, $iNumTabStops, DllStructGetPtr($tTabStops)) <> 0
    _WinAPI_InvalidateRect ($hWnd)
    Return $iResult
EndFunc   ;==>_GUICtrlEdit_SetTabStops

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_InvalidateRect
; Description ...: Adds a rectangle to the specified window's update region
; Syntax.........: _WinAPI_InvalidateRect($hWnd[, $tRect = 0[, $fErase = True]])
; Parameters ....: $hWnd        - Handle to windows
;                  $tRect       - tagRECT structure that contains the client coordinates of the rectangle  to  be  added  to  the
;                  +update region. If this parameter is 0 the entire client area is added to the update region.
;                  $fErase      - Specifies whether the background within the update region is  to  be  erased  when  the  update
;                  +region is processed.  If this parameter is True the background is erased  when  the  BeginPaint  function  is
;                  +called. If this parameter is False, the background remains unchanged.
; Return values .: Success      - True
;                  Failure      - False
; Author ........: Paul Campbell (PaulIA)
; Modified.......:
; Remarks .......:
; Related .......:
; ===============================================================================================================================
Func _WinAPI_InvalidateRect($hWnd, $tRect = 0, $fErase = True)
    Local $pRect, $aResult

    If $tRect <> 0 Then $pRect = DllStructGetPtr($tRect)
    $aResult = DllCall("User32.dll", "int", "InvalidateRect", "hwnd", $hWnd, "ptr", $pRect, "int", $fErase)
    Return $aResult[0] <> 0
EndFunc   ;==>_WinAPI_InvalidateRect

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

That does seem to work quite well, but I'm a little confused. The numbers I pass in the array, what do they represent? They're not characters, because I set it to 5 and the tabs were a little wider than one character. I thought maybe they were pixels, but that's not right either. As near as I can figure it's something like quarters of a letter space... confusing? For example, I wanted my tab spaces to be 5 characters wide so after some experimenting I found that using 20 for the tab space was perfect, so I can only assume that each character is 4, so if you set the tab space to 6 then each tab would be 1 1/2 characters.

Anyway, it's working great, so thanks a bunch!

*Edit: What's the _WinAPI_InvalidateRect function there for? I commented it out of the _GUICtrlEdit_SetTabStops just to see what would happen, and I can't see a difference.

Edited by Saunders
Link to comment
Share on other sites

That does seem to work quite well, but I'm a little confused. The numbers I pass in the array, what do they represent? They're not characters, because I set it to 5 and the tabs were a little wider than one character. I thought maybe they were pixels, but that's not right either. As near as I can figure it's something like quarters of a letter space... confusing? For example, I wanted my tab spaces to be 5 characters wide so after some experimenting I found that using 20 for the tab space was perfect, so I can only assume that each character is 4, so if you set the tab space to 6 then each tab would be 1 1/2 characters.

Anyway, it's working great, so thanks a bunch!

*Edit: What's the _WinAPI_InvalidateRect function there for? I commented it out of the _GUICtrlEdit_SetTabStops just to see what would happen, and I can't see a difference.

The tabstops are in dialog template units. They are not really pixel related because they are device independant measurements. One horizontal base unit is equal to a quarter of the average character width for the system font so your assumption is correct.

This explains InvalidateRect

When the tab stops are changed the display of the text in the edit will not be updated so the invalidaterect forces a redraw.

InvalidateRect is very useful for lots of things. For example listviews which have the style $LVS_EX_GRIDLINES. I find that when they are scrolled the grid lines are drawn over the item text and it looks a mess, and so I use

DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle($ListView1), "int", 0, "int", 1);This makes it right again after scrolling

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

The EM_SETTABSTOPS message does not automatically redraw the edit control window. If the application is changing the tab stops for text already in the edit control, it should call the InvalidateRect function to redraw the edit control window.

The values specified in the array are in dialog template units, which are the device-independent units used in dialog box templates. To convert measurements from dialog template units to screen units (pixels), use the MapDialogRect function.

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

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...