There are times we want to create our own control with windows GDI functions,there is no choice
but custom visual userobject to base on. typical approach is map a user event to pbm_paint and
drawing in that event.
The problem is it's executing sequence:for UOs PB will first fill it with it's background color (done
by it's WinProc resident in PBVM),then call our user event:the result is our UI flickers on time of
drawing. and we have no way to 'override' it,that's UO's default behavour.depressed.
The open source project Kodigo introduced a solution,use PBNI to create a custom userobject
,which handles WM_PAINT in it's WinProc,thus avoid the problem.by the way, the advanced gui
sample shipped with pb11 also based on that .
yet there is another way out without PBNI.
OK,let code talks.
(it's for PB9,pay attention to external function declaration when migrate to pb10)
forward
global type uo_canvas from userobject
end type
end forward
global type uo_canvas from userobject
integer width = 201
integer height = 200
event wm_paint pbm_paint
event wm_erasebkgnd pbm_erasebkgnd
end type
global uo_canvas uo_canvas
type prototypes
Function uLong SetWindowLong(uLong hwnd, uLong nIndex,uLong dwNewLong) Alias For
'SetWindowLongA' LIBRARY "USER32.DLL"
Function ulong GetModuleHandle(string modulename) library 'kernel32.dll' alias for
'GetModuleHandleA'
Function ulong GetProcAddress(ulong hmod,string procname) library 'kernel32.dll' alias for
'GetProcAddress'
end prototypes
event wm_paint;
//put your drawing code here
//typically I bracket my code with GetDC and ReleaseDC here
//instead of the BeginPaint and EndPaint pair
//to prevent frequently call GetDC,call it in the constuctor
//and store it in instance variable,and in destructor,release it
end event
event wm_erasebkgnd;return 1
end event
on uo_canvas.create
end on
on uo_canvas.destroy
end on
event constructor;
//the trick is here
//substitute custom visual UO's WinProc with External Visual UO's WinProc
//that makes custom user object flicker free
constant long GWL_WNDPROC=-4
ulong ll_wndpoc,ll_hmod
ll_hmod=GetModulehandle( "PBVM90.DLL") //change to PBVM105.DLL for pb10.5
if ll_hmod>0 then
ll_wndpoc=GetProcaddress( ll_hmod, "FN_UserExternalWnd")
if ll_wndpoc>0 then SetWindowLong(handle(this), GWL_WNDPROC, ll_wndpoc)
end if
end event
in the constructor,by calling SetWindowLong with GWL_WNDPROC, we set the custom visual UO's
WinProc to FN_UserExternalWnd,which is the WinProc of External Visual UserObject,and now,it's
flicker free.
It's just a trick,anyway,it really works,i have tested it in pb9 and pb10.5 under windowsxp.