A trick to make userobject a flicker-free custom drawing canvas


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.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值