April 20, 2024, 02:34:59 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Color lines in a Listbox

Started by LarryMc, June 02, 2012, 08:56:42 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

In the existing IDE when an error is encounter during compiling the error is highlighted in red in the Build window at the bottom of the IDE.
If it is a warning it is in yellow.

I wanted to duplicate that in the new IDE.
The following code is what I used to figure out how I would do it.
In this demo there are two test when a line is drawn; if the text contains a 1 it is highlighted in red and if a 3, yellow.

Thought somebody might be able to use the info.

$include "windowssdk.inc"
$include "commctrl.inc"
CONST LISTBOX_1 = 1000
DIALOG d1
CREATEDIALOG d1,0,0,300,202,0x80CB0080,0,"Caption",&d1_handler
CONTROL d1,@LISTBOX,"ListBox1",13,10,173,175,0x50A10140|LBS_OWNERDRAWFIXED,LISTBOX_1

$define SUBCLASS1_ID 12345 ' any number

DOMODAL d1
end

SUB d1_handler(),int
SELECT @MESSAGE
CASE @IDINITDIALOG
SetWindowSubclass(d1.hwnd, &MySubclassProc, SUBCLASS1_ID, 0)
CENTERWINDOW d1
OnInitDialog()

CASE @IDCLOSEWINDOW
RemoveWindowSubclass(d1.hwnd, &MySubclassProc, SUBCLASS1_ID)
CLOSEDIALOG d1,@IDOK

CASE @IDCONTROL
SELECT @CONTROLID
CASE LISTBOX_1
/* respond to control notifications here */
ENDSELECT
ENDSELECT
RETURN 0
ENDSUB


sub OnInitDialog()
' add some string, saving its indexes
int item_1_index = SendMessage(d1, LB_ADDSTRING, 0, "item 1", LISTBOX_1)
int item_2_index = SendMessage(d1, LB_ADDSTRING, 0, "item 2", LISTBOX_1)
int item_3_index = SendMessage(d1, LB_ADDSTRING, 0, "item 3", LISTBOX_1)
int item_4_index = SendMessage(d1, LB_ADDSTRING, 0, "", LISTBOX_1)
int item_5_index = SendMessage(d1, LB_ADDSTRING, 0, "item 5", LISTBOX_1)
endsub


sub MySubclassProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,UINT_PTR uIdSubclass,DWORD_PTR dwRefData)
int TextPadding = 2

select (uMsg)
case WM_DRAWITEM
if( (*<DRAWITEMSTRUCT>lParam.CtlType = ODT_LISTBOX) and (*<DRAWITEMSTRUCT>lParam.CtlID = LISTBOX_1) and (*<DRAWITEMSTRUCT>lParam.itemID >= 0))
HWND hListbox = *<DRAWITEMSTRUCT>lParam.hwndItem
int cText = _SendMessage(hListbox, LB_GETTEXTLEN, *<DRAWITEMSTRUCT>lParam.itemID, 0)
if (cText)
pointer iText = new(TCHAR, cText+1)
SendMessage(hListbox, LB_GETTEXT, *<DRAWITEMSTRUCT>lParam.itemID, iText)
if instr(#<string>itext,"1")
if *<DRAWITEMSTRUCT>lParam.itemState & ODS_SELECTED
uint hbrush =GetSysColorBrush(COLOR_HIGHLIGHT)
else
hbrush = CreateSolidBrush(RGB(255, 0, 0))
endif
uint hbrushOld = SelectObject(*<DRAWITEMSTRUCT>lParam.hDC, hbrush)
FillRect(*<DRAWITEMSTRUCT>lParam.hDC, &*<DRAWITEMSTRUCT>lParam.rcItem, hbrush)
elseif instr(#<string>itext,"3")
if *<DRAWITEMSTRUCT>lParam.itemState & ODS_SELECTED
hbrush = GetSysColorBrush(COLOR_HIGHLIGHT)
else
hbrush = CreateSolidBrush(RGB(255, 255, 0))
endif
hbrushOld = SelectObject(*<DRAWITEMSTRUCT>lParam.hDC, hbrush)
FillRect(*<DRAWITEMSTRUCT>lParam.hDC, &*<DRAWITEMSTRUCT>lParam.rcItem, hbrush)
else
FillRect(*<DRAWITEMSTRUCT>lParam.hDC, &*<DRAWITEMSTRUCT>lParam.rcItem,_
GetSysColorBrush(IIF(*<DRAWITEMSTRUCT>lParam.itemState & ODS_SELECTED, _
COLOR_HIGHLIGHT, COLOR_WINDOW)))
endif
else
' Erase item background
FillRect(*<DRAWITEMSTRUCT>lParam.hDC, &*<DRAWITEMSTRUCT>lParam.rcItem, _
GetSysColorBrush(IIF(*<DRAWITEMSTRUCT>lParam.itemState & ODS_SELECTED, _
COLOR_HIGHLIGHT, COLOR_WINDOW)))
endif
if hbrushOld
SelectObject(*<DRAWITEMSTRUCT>lParam.hDC, hbrushOld)
endif
if hbrush
DeleteObject(hbrush)
endif

' any text to draw?
if (cText)
WINRECT rc
CopyRect(&rc, &*<DRAWITEMSTRUCT>lParam.rcItem)
rc.left += TextPadding
UINT bkMode = SetBkMode(*<DRAWITEMSTRUCT>lParam.hDC, TRANSPARENT)
DrawText(*<DRAWITEMSTRUCT>lParam.hDC, iText, cText, &rc, DT_SINGLELINE|DT_VCENTER)
SetBkMode(*<DRAWITEMSTRUCT>lParam.hDC, bkMode)
delete iText
endif
return 0
endif
endselect
return DefSubclassProc(hWnd, uMsg, wParam, lParam)
endsub

Note: Based on a subclassing routine by Sapero for images in a listbox.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library