Copyright © 1998-2010 MultiMedia Soft

SetCustomPaintFunction method

Previous pageReturn to chapter overviewNext page


Provides a pointer to a custom drawing functions that will be called immediately before completing the button's rendering.

For further details about custom graphic rendering see the How to perform custom graphic rendering over the control surface section.



Visual Basic

control.SetCustomPaintFunction pFunction as long


Visual C++

void control.SetCustomPaintFunction (long pFunction);







Pointer to a custom drawing functions


Return value





Let's see a sample that demonstrates how to paint a flicker free red circle over the button surface:


Visual Basic example

Inside a BAS module, add the following declarations that will allow accessing the Windows GDI primitives

Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long

Public Declare Function Ellipse Lib "gdi32" (ByVal hDC As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long

Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject&) As Long

Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long

Inside the same BAS module add the following subroutine that will perform our custom rendering

Sub MyPaintFunction(ByVal hDC As Long)

 Dim brushColor As Long

 Dim brushOld As Long

 brushColor = CreateSolidBrush(RGB(255, 0, 0))

 brushOld = SelectObject(hDC, brushColor)

 Ellipse hDC, 10, 10, 50, 50

 SelectObject hDC, brushOld

 DeleteObject brushColor

End Sub

Inside the Form_Load subroutine, call the SetCustomPaintFunction method passing the pointer to the MyPaintFunction subroutine, obtained through the AddressOf function.

Private Sub Form_Load()

 BtnEnh1.SetCustomPaintFunction AddressOf MyPaintFunction

End Sub


Note that you can call the SetCustomPaintFunction method from wherever you want, also passing different drawing function depending upon your rendering needs.



Visual C++ example

Write your own function like the one below (note that the function must have the _stdcall declaration)

void _stdcall MyPaintFunction (long hDC)


     HBRUSH brushColor = ::CreateSolidBrush (RGB (255,0,0));

     HBRUSH brushOld = (HBRUSH) ::SelectObject ((HDC) hDC, brushColor);

     ::Ellipse((HDC) hDC, 10, 10, 50, 50);

     ::SelectObject ((HDC) hDC, brushOld);

     ::DeleteObject (brushColor);


Supposing that we have created a member variable m_btn1 for one of our controls, we will have to call the following line

m_btn1.SetCustomPaintFunction ((long) MyPaintFunction);