Jump to content

Font size and weight with _GUICtrlListView_Create


Champak
 Share

Recommended Posts

How do I set the size and weight of fonts with _GUICtrlListView_Create()?

Champak

you need to use the API call GuiCtrlSetFont is based on: _WinAPI_CreateFont()

Some examples, including ownerdrawn listviews for individually coloured items.

_WinAPI_RegisterClassEx(), Create you own class. Drop shadow, change window icon/cursor.

http://www.autoitscript.com/forum/index.php?showtopic=79575

change the font of a single listview item

http://www.autoitscript.com/forum/index.php?showtopic=71681

http://www.autoitscript.com/forum/index.ph...st&p=524257

GUiCtrlListView and the $GUI_BKCOLOR_LV_ALTERNATE

http://www.autoitscript.com/forum/index.ph...nAPI_CreateFont

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>
#include <FontConstants.au3>

;Global $hFont = _WinAPI_CreateFont(14, 6, 0, 0, $FW_BOLD) ; default font Arial
;Global $hFont = _WinAPI_CreateFont(14, 6, 0, 0, $FW_MEDIUM, False, False, False, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $PROOF_QUALITY, $DEFAULT_PITCH, 'Tahoma')
$hFont = _WinAPI_CreateFont(16, 6, 0, 0, $FW_NORMAL, False, False, False, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, 'Arial')

$hGUI = GUICreate("Test", 300, 200)

$hListView = _GUICtrlListView_Create($hGUI, "Items|SubItems", 10, 10, 280, 180, $LVS_REPORT, $WS_EX_CLIENTEDGE)
_WinAPI_SetFont($hListView, $hFont, True)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

For $i = 1 To 10
    _GUICtrlListView_AddItem($hListView, "Item" & $i)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 1)
Next

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_WinAPI_DeleteObject($hFont) ; delete object on exit or if listview destroyed
Edited by rover

I see fascists...

Link to comment
Share on other sites

That's interesting, I'm definately going to play with this a little. Thanks. Do you know why the title column of the listview doesn't resize with the text like the listview item boxes?

Champak

try _GUICtrlListView_SetColumnWidth()

probably a better example on the forum of autosizing

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>
#include <FontConstants.au3>

;Global $hFont = _WinAPI_CreateFont(14, 6, 0, 0, $FW_BOLD) ; default font Arial
Global $hFont = _WinAPI_CreateFont(14, 6, 0, 0, $FW_MEDIUM, False, False, False, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $PROOF_QUALITY, $DEFAULT_PITCH, 'Tahoma')
;$hFont = _WinAPI_CreateFont(16, 6, 0, 0, $FW_NORMAL, False, False, False, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, 'Arial')

$hGUI = GUICreate("Test", 300, 200)

$hListView = _GUICtrlListView_Create($hGUI, "Items List|SubItems", 10, 10, 280, 180, $LVS_REPORT, $WS_EX_CLIENTEDGE)
_WinAPI_SetFont($hListView, $hFont, True)

_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

_GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($hListView, 1, $LVSCW_AUTOSIZE_USEHEADER)

For $i = 1 To 10
    _GUICtrlListView_AddItem($hListView, "Item" & $i)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 1)
Next

;_GUICtrlListView_SetColumnWidth($hListView, 1, $LVSCW_AUTOSIZE)


GUISetState()

Sleep(2000)

_GUICtrlListView_AddItem($hListView, "Item longer name")
_GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_WinAPI_DeleteObject($hFont) ; delete object on exit or if listview destroyed

I see fascists...

Link to comment
Share on other sites

Yeah, I played with the column width before, but it doesn't do anything for the height which is where I'm running into my problem; I should have been a little clearer on that.

It's a known bug with the Microsoft Listview control that it wont resize the header height with a font size change

unless themes are disabled for the control.

Option 1 - use the SetWindowTheme to turn off themes for the header control component of the listview. - see example

Option 2 - use two fonts, one for listview and one for header. - see example

you need to set both at same time and in order when changing listview font (to keep header font same size),

otherwise header font will be set to listview item font.

Option 3 - subclass header control of listview and use HDM_LAYOUT message capture to set change of height in $tagWINDOWPOS structure

and use SetWindowPos call. (could not get this to work properly with a brief attempt, the header resizes but overlaps the listbox items).

see 'resizing header issues explained' reference link

Option 4: destroy the internal listview child window header control and replace it with an external header (GuiHeader UDF)

and synchronize messaging between listview and external header.

see 'resizing header issues explained' reference link

I'm sure someone here can come up with some ruthlessly efficient coding to solve one of the last two options.

some reference links

Google Groups - sometimes a greater resource than a web search (Usenet)

http://groups.google.ca/groups/search?hl=e...amp;qt_s=Search

ListView column header height

http://forums.microsoft.com/MSDN/ShowPost....11&SiteID=1

A Multiline Header Control Inside a CListCtrl - see comments section

http://www.codeproject.com/KB/list/headerctrlex.aspx

ListView descendent with custom drawn header

http://groups.google.ca/group/borland.publ...396aa9b95?hl=en

Creating Hints For a ListView's HeaderControl

http://groups.google.ca/group/borland.publ...d3c5e8d67?hl=en

ListView Header

resizing header issues explained

http://groups.google.ca/group/microsoft.pu...8299128eb?hl=en

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>
#include <FontConstants.au3>

Global $hFont1 = _WinAPI_CreateFont(25, 6, 0, 0, $FW_MEDIUM, False, False, False, _
$DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $PROOF_QUALITY, $DEFAULT_PITCH, 'Tahoma')
Global $hFont2 = _WinAPI_CreateFont(20, 6, 0, 0, $FW_BOLD) ; default font Arial

$hGUI = GUICreate("Test", 300, 200)

;$cListView = GUICtrlCreateListView("Items List|SubItems", 10, 10, 280, 180, $LVS_REPORT, $WS_EX_CLIENTEDGE)
;$hListView = GUICtrlGetHandle($cListView)
$hListView = _GUICtrlListView_Create($hGUI, "Items List|SubItems", 10, 10, 280, 180, $LVS_REPORT, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hHeader = HWnd(_GUICtrlListView_GetHeader($hListView)) ; get handle to header control

;turn of theme for header control to enable autosizing
;DllCall("UxTheme.dll", "int", "SetWindowTheme", "hwnd", $hHeader, "wstr", "", "wstr", "")

; or set different font for header and items
_WinAPI_SetFont($hListView, $hFont1, True)
_WinAPI_SetFont($hHeader, $hFont2, True)

_GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($hListView, 1, $LVSCW_AUTOSIZE_USEHEADER)

For $i = 1 To 5
    _GUICtrlListView_AddItem($hListView, "Item" & $i)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 1)
Next

;_GUICtrlHeader_SetItemAlign($hHeader, 0, 0)
;_GUICtrlHeader_SetItemFormat($hHeader, 0, BitOR($HDF_LEFT, $HDF_STRING))

GUISetState()

;test resizing of themeless header
;Sleep(2000) ; also uncomment SetWindowTheme dllcall
;_WinAPI_SetFont($hListView, $hFont1, True)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_WinAPI_DeleteObject($hFont1) ; delete object on exit or if listview destroyed
_WinAPI_DeleteObject($hFont2)

I see fascists...

Link to comment
Share on other sites

Before I try option 1, how do I turn it back on?

Thanks.

What?, you didn't even try it? :P

turn it back on?, what a great suggestion! :(

Edit: 2012 - Here is a good reason to not use forum smilies.

These are not the smilies I used in 2008.

Successive forum software upgrades in the past 4 years must have changed the names or indexing for these icons.

I used the winking and smile smilies./Edit

OffTopic: Is it just me and my Firefox, or has the forum appearance suddenly 'de-themed'. (12:30 GMT 10/29/08)

I'm getting a plain white background, and no advanced features

a last resort method when the forum server load is high?

anyway, with some further investigation...

a workaround UDF and example using SetWindowTheme and WM_SETFONT to resize listview header with font size change

not the best or least buggy method, but just to show 'it is possible'.

(to use one of the most trite phrases found in forum post titles)

some problem with gridline alignment of items if scroll bar adjusted when listview updated with many items using

_GUICtrlListView_BeginUpdate/EndUpdate prior to last call to _GUICtrlListView_SetFont

not an issue with this UDF but this example demonstrates the listview blanks second column when

_GUICtrlListView_BeginUpdate() turns off repainting. It occurs even if _GUICtrlListView_SetFont() not called.

other methods involve customdraw, subclassing and WM_NOTIFY message handling

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#include <GuiListView.au3>

Opt('MustDeclareVars', 1)
Global $hGUI, $hFont1, $hFont2, $cListView, $hListView, $sRet

$hFont1 = _WinAPI_CreateFont(30, 6, 0, 0, $FW_MEDIUM, False, False, False, _
        $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $PROOF_QUALITY, $DEFAULT_PITCH, 'Tahoma')
$hFont2 = _WinAPI_CreateFont(20, 6, 0, 0, $FW_BOLD) ; default font Arial

$hGUI = GUICreate("Listview header resizing test", 500, 300)

$cListView = GUICtrlCreateListView("Items List|SubItems", 10, 10, 480, 280)
$hListView = GUICtrlGetHandle($cListView)
;$hListView = _GUICtrlListView_Create($hGUI, "Items List|SubItems", 10, 10, 480, 280, $LVS_REPORT, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

; Set large font for both header and items
$sRet = _GUICtrlListView_SetFont($hListView, $hFont1)
ConsoleWrite('-_GUICtrlListView_SetFont = ' & $sRet & @CRLF & '>Error code: ' & @error & @CRLF)

For $i = 1 To 5
    _GUICtrlListView_AddItem($hListView, "Item" & $i)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 1)
Next

_GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($hListView, 1, $LVSCW_AUTOSIZE_USEHEADER)

GUISetState()

Sleep(2000)
; Set smaller font for both header and items
_GUICtrlListView_SetFont($hListView, $hFont2)

Sleep(2000)
; Set large font for items and small font for header
_GUICtrlListView_SetFont($hListView, $hFont1, $hFont2)

Sleep(2000)
_GUICtrlListView_BeginUpdate($hListView)
For $i = 1 To 1000
    _GUICtrlListView_AddItem($hListView, "Longer Item" & $i)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem" & $i, 1)
Next
_GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($hListView, 1, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_EndUpdate($hListView)


Sleep(2000)
; Set small font for items and large font for header
_GUICtrlListView_SetFont($hListView, $hFont2, $hFont1)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

; delete objects on exit or if listview destroyed
If $hFont1 Then _WinAPI_DeleteObject($hFont1)
If $hFont2 Then _WinAPI_DeleteObject($hFont2)
GUIDelete()
Exit

; =========================================================================================
; Name...........: _GUICtrlListView_SetFont
; Description ...: Set font for a list-view controls items and header text
; Syntax.........: _GUICtrlListView_SetFont($hWnd, $hFontLV, $hFontHD = 0)
; Parameters ....: $hWnd - Handle to the control
; $hFontLV - Handle to font
; $hFontHD - Handle to header font (Optional)
; Return values .: Success - True
; Failure - False
; Author ........: rover
; Remarks .......: Use optional header font parameter for a different font/size of header
; =========================================================================================
Func _GUICtrlListView_SetFont($hWnd, $hFontLV, $hFontHD = 0)
    If $Debug_LV Then _GUICtrlListView_ValidateClassName($hWnd)
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    Local $aReturn, $hHeader, $hDLL
    $hHeader = HWnd(_GUICtrlListView_GetHeader($hWnd)) ; get handle to header control
    
    If Not IsHWnd($hWnd) Or Not IsPtr($hFontLV) Or Not IsHWnd($hHeader) Then Return SetError(1, 0, False)
    
    _SendMessage($hWnd, $__LISTVIEWCONSTANT_WM_SETREDRAW, 0) ; disable repainting

    $hDLL = DllOpen("UxTheme.dll")
    ; turn off theme for header control to enable header autosizing
    $aReturn = DllCall($hDLL, "int", "SetWindowTheme", "hwnd", $hHeader, "wstr", "", "wstr", "")
    If @error Or $aReturn[0] Then
        DllClose($hDLL)
        Return SetError(2, 0, False)
    EndIf
    
    If IsPtr($hFontHD) Then ; set font for items and if available separate font for header
        _SendMessage($hWnd, $__LISTVIEWCONSTANT_WM_SETFONT, $hFontLV, True, 0, "hwnd")
        _SendMessage($hHeader, $__LISTVIEWCONSTANT_WM_SETFONT, $hFontHD, True, 0, "hwnd")
    Else ; set same font for header and items
        ; resizing header down to a smaller font size causes listview repaint problems, so repainting is enabled
        _SendMessage($hWnd, $__LISTVIEWCONSTANT_WM_SETREDRAW, 1) ; enable repainting
        _SendMessage($hWnd, $__LISTVIEWCONSTANT_WM_SETFONT, $hFontLV, True, 0, "hwnd")
    EndIf
    
    ; restore control theme painting
    $aReturn = DllCall($hDLL, "int", "SetWindowTheme", "hwnd", $hHeader, "ptr", 0, "ptr", 0)
    If @error Or $aReturn[0] Then
        DllClose($hDLL)
        Return SetError(3, 0, False)
    EndIf
    DllClose($hDLL)
    
    _SendMessage($hWnd, $__LISTVIEWCONSTANT_WM_SETREDRAW, 1) ; enable repainting
    _WinAPI_RedrawWindow($hWnd, 0, 0, $RDW_INVALIDATE)
    Return SetError(0, 0, $aReturn[0] <> 1)
EndFunc ;==>_GUICtrlListView_SetFont
Edited by rover

I see fascists...

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