Jump to content

change the font of a single listview item


 Share

Recommended Posts

Hey guys, please have a look at this for me.

#include <GuiConstants.au3>
#include <GuiListView.au3>

Opt("GUIOnEventMode", 1)
$gui = GUICreate("Test", 172, 239, 443, 307)
GUISetOnEvent($GUI_EVENT_CLOSE, "close")
$listview = GUICtrlCreateListView("test", 8, 8, 153, 217)

GUICtrlCreateListViewItem("Normal Item", $listview)
GUICtrlCreateListViewItem("Normal Item", $listview)
GUICtrlCreateListViewItem("Blue Item", $listview)
GUICtrlSetColor(-1, 0x0000FF)
GUICtrlCreateListViewItem("Normal Item", $listview)
GUICtrlCreateListViewItem("Normal Item", $listview)

GUISetState(@SW_SHOW)

MsgBox(64, "help me", "Help me. I need the blue item to be BOLD and ITALIC. Thanks.")

While 1
    Sleep(100)
WEnd

Func close()
  Exit
EndFunc

The blue item in the list needs to be BOLD and ITALIC. I tried applying a GUICtrlSetFont to it but it doesn't seem to work. Any ideas?

Edited by lemony
Link to comment
Share on other sites

Thanks for your help Linux, but like I mentioned in my first post, I tried GUICtrlSetFont and it doesn't seem to work.

It would be logical that it should work, but for some reason it doesn't.

Any other ideas?

Link to comment
Share on other sites

lemony

You need to use NM_CUSTOMDRAW notify for setting text size and color.

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

Global Const $FW_BOLD = 700

Global $Font = _WinAPI_CreateFont(14, 6, 0, 0, $FW_BOLD)

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

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

_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

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_WinAPI_DeleteObject($Font)

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $hWndFrom, $iCode
    
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tCustDraw = DllStructCreate('hwnd hwndFrom;int idFrom;int code;' & _
                                        'dword DrawStage;hwnd hdc;long rect[4];dword ItemSpec;int ItemState;dword Itemlparam;' & _
                                        'dword clrText;dword clrTextBk;int SubItem;' & _
                                        'dword ItemType;dword clrFace;int IconEffect;int IconPhase;int PartID;int StateID;long rectText[4];int Align', _ ;winxp or later
                                        $lParam)
                    
                    Local $iDrawStage = DllStructGetData($tCustDraw, 'DrawStage')
                    
                    If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
                    
                    Local $iItem = DllStructGetData($tCustDraw, 'ItemSpec')
                    
                    Switch $iItem
                        Case 5
                            DllStructSetData($tCustDraw, 'clrText', 0x5555DD)
                            Local $hDC = DllStructGetData($tCustDraw, "hdc")
                            _WinAPI_SelectObject($hDC, $Font)
                            Return $CDRF_NEWFONT
                    EndSwitch
            EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Link to comment
Share on other sites

WOW, thanks a lot rasim!!!!!!!!! I'm glad it is possible for it to work. :)

Strange though, that GUICtrlSetColor works (as you can see in my example) but GUICtrlSetFont doesn't. Could this be a possible bug in AutoIt?

Maybe I should let the developers know.

Link to comment
Share on other sites

WOW, thanks a lot rasim!!!!!!!!! I'm glad it is possible for it to work. :)

Strange though, that GUICtrlSetColor works (as you can see in my example) but GUICtrlSetFont doesn't. Could this be a possible bug in AutoIt?

Maybe I should let the developers know.

Why do I get these errors when I run rasim's script?

C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Test.au3(6,28) : ERROR: $FW_BOLD previously declared as a 'Const'

Global Const $FW_BOLD = 700

~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Test.au3(49,53) : WARNING: $CDDS_PREPAINT: possibly used before declaration.

If $iDrawStage = $CDDS_PREPAINT Then

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Test.au3(49,85) : WARNING: $CDRF_NOTIFYITEMDRAW: possibly used before declaration.

If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Test.au3(58,49) : WARNING: $CDRF_NEWFONT: possibly used before declaration.

Return $CDRF_NEWFONT

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Test.au3(49,53) : ERROR: $CDDS_PREPAINT: undeclared global variable.

If $iDrawStage = $CDDS_PREPAINT Then

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Test.au3(8,56) : ERROR: _WinAPI_CreateFont(): undefined function.

Global $Font = _WinAPI_CreateFont(14, 6, 0, 0, $FW_BOLD)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Test.au3 - 3 error(s), 3 warning(s)

Link to comment
Share on other sites

Omega

Why do I get these errors when I run rasim's script?

What the Autoit version you are use? Example maked for v3.2.12.0.

Different font and color for Item and SubItem:

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

Global $Font1 = _WinAPI_CreateFont(14, 6, 0, 0, $FW_BOLD)
Global $Font2 = _WinAPI_CreateFont(14, 6, 0, 0, $FW_BOLD, True)

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

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

_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

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_WinAPI_DeleteObject($Font1)
_WinAPI_DeleteObject($Font2)

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $hWndFrom, $iCode
    
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    
                    Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
                    
                    If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
                    If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
                    
                    Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
                    
                    Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                    
                    Local $iColor, $hDC
                    
                    Switch $iItem
                        Case 5
                            $hDC = DllStructGetData($tCustDraw, "hdc")
                            
                            If $iSubItem = 0 Then
                                $iColor = 0xFF4466
                                _WinAPI_SelectObject($hDC, $Font1)
                            Else
                                $iColor = 0x5555DD
                                _WinAPI_SelectObject($hDC, $Font2)
                            EndIf
                            
                            DllStructSetData($tCustDraw, "clrText", $iColor)
                            
                            Return $CDRF_NEWFONT
                    EndSwitch
            EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Edited by rasim
Link to comment
Share on other sites

Omega

What the Autoit version you are use? Example maked for v3.2.12.0.

Different font and color for Item and SubItem:

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

Global $Font1 = _WinAPI_CreateFont(14, 6, 0, 0, $FW_BOLD)
Global $Font2 = _WinAPI_CreateFont(14, 6, 0, 0, $FW_BOLD, True)

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

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

_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

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_WinAPI_DeleteObject($Font1)
_WinAPI_DeleteObject($Font2)

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $hWndFrom, $iCode
    
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    
                    Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
                    
                    If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
                    If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
                    
                    Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
                    
                    Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                    
                    Local $iColor, $hDC
                    
                    Switch $iItem
                        Case 5
                            $hDC = DllStructGetData($tCustDraw, "hdc")
                            
                            If $iSubItem = 0 Then
                                $iColor = 0xFF4466
                                _WinAPI_SelectObject($hDC, $Font1)
                            Else
                                $iColor = 0x5555DD
                                _WinAPI_SelectObject($hDC, $Font2)
                            EndIf
                            
                            DllStructSetData($tCustDraw, "clrText", $iColor)
                            
                            Return $CDRF_NEWFONT
                    EndSwitch
            EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Indeed, I didnt know about the new release :) thanks you... It works fine now, although I will need some time to understand the code :)
Link to comment
Share on other sites

Ha! I finally found a useful post! I've been searching for 2 days, but it seems on this forum if you don't get the search wording just right, you can't find *&#^$^$.

Anyway, thanks rasim. Now just gotta figure out how to do this conditionally and I'll be all set :)

Link to comment
Share on other sites

@wraithdu,

Yeah it doesn't let you find 3 letter words either, the only way you can do a search is using an external search e.g google is a good choice. Great thing no registration is required over general support or it's impossible to use 3rd parth search tool.

Link to comment
Share on other sites

  • 4 months later...

@wraithdu,

Yeah it doesn't let you find 3 letter words either, the only way you can do a search is using an external search e.g google is a good choice. Great thing no registration is required over general support or it's impossible to use 3rd parth search tool.

hi i have used your script because i wanne set some items bolt but now i have another problen how i can read with item is selected with a _GUICtrlListView_Create ????????????

Link to comment
Share on other sites

  • 2 weeks later...

jep whats wrong on that ?

hi i have a bug when i set tsome item of my listview in Bolt

there are 5 itmens set in bolt in my listview but when i select the first down and then push my down arrow a couple of time all my listview item lose there bolt.. does somebody know how to fix that?

Link to comment
Share on other sites

  • 7 years later...

He'll never answer you, he hasn't been here in 7 years. Open a new thread to document your issue.

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

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