Subclassing Windows Controls
Purpose: If you want a control to do something that is not already built in to the control, subclassing allows you to add that new functionality at runtime.
In the sample below, each label control is subclassed and reflects the WM_CTLCOLORSTATIC message to the subclassed windows procedure.
Subclassing involves tapping into the windows message stream for a custom control. Text labels, editboxes, listboxes, etc all have their own windows procedure. You usually don't need to concern yourself with these.
This demo creates a form with a black background and two BCX_LABELS that have their foreground and background colors changed.
Example:
GUI
"test"
GLOBAL
Form1AS
CONTROLGLOBAL
Label_1AS
CONTROLGLOBAL
Label_2AS
CONTROLSUB
FORMLOAD Form1=
BCX_FORM
(
"test"
,67
,42
,160
,130
)
Label_1=
BCX_LABEL
(
"Label 1"
,Form1,100
,5
,10
)
Label_2=
BCX_LABEL
(
"Label 2"
,Form1,101
,5
,50
)
BCX_SET_FORM_COLOR
(
Form1,RGB
(
0
,0
,0
)
)
SubClassLabel1(
)
SubClassLabel2(
)
CENTER
(
Form1)
SHOW
(
Form1)
END
SUB
BEGIN
EVENTS
IF
CBMSG
=
WM_CTLCOLORSTATICTHEN
FUNCTION
=
SendMessage(
(
HWND)
lParam, Msg, wParam, lParam)
END
IF
END
EVENTS
SUB
SubClassLabel1GLOBAL
Original_Label_1_WndProcAS
WNDPROC Original_Label_1_WndProc=
SetWindowLong(
Label_1,GWL_WNDPROC,Label_1_WndProc)
END
SUB
SUB
SubClassLabel2GLOBAL
Original_Label_2_WndProcAS
WNDPROC Original_Label_2_WndProc=
SetWindowLong(
Label_2,GWL_WNDPROC,Label_2_WndProc)
END
SUB
CALLBACK
FUNCTION
Label_1_WndProc(
)
IF
CBMSG
=
WM_CTLCOLORSTATICTHEN
FUNCTION
=
SetColor(
RGB
(
0
,225
,0
)
,RGB
(
0
,0
,0
)
,wParam,lParam)
END
IF
FUNCTION
=
CallWindowProc(
Original_Label_1_WndProc,hWnd,Msg,wParam,lParam)
END
FUNCTION
CALLBACK
FUNCTION
Label_2_WndProc(
)
IF
CBMSG
=
WM_CTLCOLORSTATICTHEN
FUNCTION
=
SetColor(
RGB
(
255
,0
,0
)
,RGB
(
0
,0
,0
)
,wParam,lParam)
END
IF
FUNCTION
=
CallWindowProc(
Original_Label_2_WndProc,hWnd,Msg,wParam,lParam)
END
FUNCTION
FUNCTION
SetColor(
TxtColr, BkColr, wParam, lParam)
AS
LRESULTGLOBAL
ReUsableBrushAS
HBRUSH DeleteObject(
ReUsableBrush)
ReUsableBrush=
CreateSolidBrush(
BkColr)
SetTextColor(
(
HDC)
wParam, TxtColr)
SetBkColor(
(
HDC)
wParam, BkColr)
FUNCTION
=
(
LRESULT)
ReUsableBrushEND
FUNCTION