IonicWind Software

IWBasic => GUI Central => Topic started by: LarryMc on June 02, 2012, 08:56:42 PM

Title: Color lines in a Listbox
Post by: LarryMc on June 02, 2012, 08:56:42 PM
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.