$PBExportHeader$uo_imflatbutton.sru
$PBExportComments$扁平按钮
forward
global type uo_imflatbutton from commandbutton
end type
type tagrect from structure within uo_imflatbutton
end type
type us_bitmap from structure within uo_imflatbutton
end type
end forward

type tagrect from structure
	long		left
	long		top
	long		right
	long		bottom
end type

type us_bitmap from structure
	long		bmtype
	long		bmwidth
	long		bmheight
	long		bmwidthbytes
	long		bmplanes
	long		bmbitspixel
	blob		bmbits
end type

global type uo_imflatbutton from commandbutton
integer width = 302
integer height = 100
integer textsize = -9
integer weight = 400
fontcharset fontcharset = gb2312charset!
fontpitch fontpitch = variable!
string facename = "宋体"
string text = "确定"
event ue_paint pbm_paint
event ue_lbuttondown pbm_lbuttondown
event ue_mousemove pbm_mousemove
event ue_lbuttonup pbm_lbuttonup
event ue_size pbm_size
event ue_dbclk pbm_lbuttondblclk
event ue_keyup pbm_keyup
event ue_keydown pbm_keydown
event ue_enable pbm_enable
event ue_textchange ( )
end type
global uo_imflatbutton uo_imflatbutton

type prototypes

//从指定窗口的结构中取得信息
FUNCTION uLong GetWindowuLong(uLong  hwnd, uLong nIndex) Alias For GetWindowLongA LIBRARY "USER32.DLL"
//在窗口结构中为指定的窗口设置信息
FUNCTION uLong SetWindowuLong(uLong  hwnd, uLong nIndex,uLong dwNewLong) Alias For SetWindowLongA LIBRARY "USER32.DLL"
//用指定的刷子填充一个矩形
FUNCTION ulong FillRect(ulong hdc,ref tagrect lpRect,ulong hBrush) LIBRARY "user32.dll" alias for "FillRect;Ansi"
//用纯色创建一个刷子
FUNCTION ulong CreateSolidBrush(ulong crColor) LIBRARY "gdi32.dll"
//为任何一种标准系统颜色取得一个刷子 
FUNCTION ulong GetSysColorBrush(ulong nIndex) LIBRARY "user32.dll"
//判断指定windows显示对象的颜色
FUNCTION ulong GetSysColor(ulong nIndex) LIBRARY "user32.dll"
//指定阴影刷子、虚线画笔以及字符中的空隙的填充方式
FUNCTION ulong SetBkMode(ulong hdc,ulong nBkMode) LIBRARY "gdi32.dll"
//为指定的设备场景设置背景颜色。背景颜色用于填充阴影刷子、虚线画笔以及字符（如背景模式为OPAQUE）中的空隙。也在位图颜色转换期间使用
FUNCTION ulong SetBkColor(ulong hdc,ulong crColor) LIBRARY "gdi32.dll"
//设置当前文本颜色。这种颜色也称为“前景色” 
FUNCTION ulong SetTextColor(ulong hdc,ulong crColor) LIBRARY "gdi32.dll"
////将文本描绘到指定的矩形中
FUNCTION ulong DrawText(ulong hdc,string lpStr,ulong nCount,ref tagrect lpRect,ulong wFormat) LIBRARY "user32.dll" ALIAS FOR "DrawTextA;Ansi"
//用指定的样式描绘一个矩形的边框
FUNCTION boolean DrawEdge(ulong hdc,ref tagrect qrc,ulong edge,ulong grfFlags) LIBRARY "user32.dll" alias for "DrawEdge;Ansi"
//释放由调用GetDC或GetWindowDC函数获取的指定设备场景。它对类或私有设备场景无效（但这样的调用不会造成损害）
FUNCTION ulong ReleaseDC(ulong hwnd,ulong hdc) LIBRARY "user32.dll"
//删除专用设备场景或信息场景，释放所有相关窗口资源
FUNCTION ulong DeleteDC(ulong hdc) LIBRARY "gdi32.dll"
//每个设备场景都可能有选入其中的图形对象。其中包括位图、刷子、字体、画笔以及区域等等。一次选入设备场景的只能有一个对象。选定的对象会在设备场景的绘图操作中使用。例如，当前选定的画笔决定了在设备场景中描绘的线段颜色及样式
FUNCTION ulong SelectObject(ulong hdc,ulong hObject) LIBRARY "gdi32.dll"
//用这个函数删除GDI对象，比如画笔、刷子、字体、位图、区域以及调色板等等。对象使用的所有系统资源都会被释放
FUNCTION ulong DeleteObject(ulong hObject) LIBRARY "gdi32.dll"
//用指定的样式、宽度和颜色创建一个画笔 
FUNCTION ulong CreatePen(ulong nPenStyle,ulong nWidth,ulong crColor) LIBRARY "gdi32.dll"
//为指定的设备场景指定一个新的当前画笔位置。前一个位置保存在lpPoint中
FUNCTION ulong MoveToEx(ulong hdc,ulong ix,ulong iy,ulong lpPoint) LIBRARY "gdi32.dll"
//用当前画笔画一条线，从当前位置连到一个指定的点。这个函数调用完毕，当前位置变成x,y点
FUNCTION ulong LineTo(ulong hdc,ulong ix,ulong iy) LIBRARY "gdi32.dll"
//校验窗口的全部或部分客户区。这样便可告之windows指定的区域不需要重画 
FUNCTION ulong ValidateRect(ulong hwnd,ref tagrect lpRect) LIBRARY "user32.dll" alias for "ValidateRect;Ansi"
//获取指定窗口的设备场景
FUNCTION ulong GetDC(ulong hwnd) LIBRARY "user32.dll"
//载入一个位图、图标或指针
FUNCTION ulong LoadImage(ulong hInst,string lpsz,ulong un1,ulong n1,ulong n2,ulong un2) LIBRARY "user32.dll" ALIAS FOR "LoadImageA;Ansi"
//在指定的位置画一个图标
FUNCTION boolean DrawIconEx(ulong hdc,ulong xLeft,ulong yTop,ulong hIcon,ulong cxWidth,ulong cyWidth,ulong istepIfAniCur,ulong hbrFlickerFreeDraw,ulong diFlags) LIBRARY "user32.dll"
FUNCTION ulong GetTextFace(ulong hdc,ulong nCount,ref string lpFacename) LIBRARY "gdi32.dll" ALIAS FOR "GetTextFaceA;Ansi"
FUNCTION boolean DrawState(ulong hDC,ulong hBrush,ulong lpDrawStateProc,ulong lParam,ulong wParam,ulong n1,ulong n2,ulong n3,ulong n4,ulong un) LIBRARY "user32.dll" ALIAS FOR "DrawStateA"
FUNCTION ulong DestroyIcon(ulong hIcon) LIBRARY "user32.dll"

FUNCTION ulong Rectangle(ulong hdc,ulong X1,ulong Y1,ulong X2,ulong Y2) LIBRARY "gdi32.dll" 


function long LoadLibrary( ref string lpLibFileName2 ) LIBRARY "KERNEL32"  Alias For "LoadLibraryA;Ansi"
function long FreeLibrary( long hinstance ) LIBRARY "KERNEL32"
FUNCTION ulong LoadBitmap(ulong hInstance, long lpBitmapName) LIBRARY "user32.dll" ALIAS FOR "LoadBitmapA"

FUNCTION ulong CreateDC(string lpDriverName,string lpDeviceName, string lpOutput, long lpInitData) LIBRARY "gdi32.dll" ALIAS FOR "CreateDCA;Ansi"
FUNCTION ulong CreateCompatibleDC(ulong hdc) LIBRARY "gdi32.dll"
FUNCTION ulong GetPixel(ulong hdc,ulong ix,ulong iy) LIBRARY "gdi32.dll"
FUNCTION ulong LoadImage(ulong hInst,long lpsz,ulong un1,ulong n1,ulong n2,ulong un2) LIBRARY "user32.dll" ALIAS FOR "LoadImageA"
//if it's use in other version ,please modify by hand.
FUNCTION long FN_ResGetIconID(string lpLibFileName) LIBRARY "PBVM90.dll" alias for "FN_ResGetIconID;Ansi" 
FUNCTION long FN_ResGetBitmapID(string lpLibFileName) LIBRARY "PBVM90.dll" alias for "FN_ResGetBitmapID;Ansi" 
FUNCTION string FN_ResGetBitmapName(long lpLibFileName) LIBRARY "PBVM90.dll" alias for "FN_ResGetBitmapName;Ansi" 
Function Long ImageList_Create (Long MinCx , Long MinCy , Long flags ,Long cInitial ,Long cGrow) Library "PBVM90.dll" ALIAS FOR "PBImageList_Create"
Function Long ImageList_AddMasked(Long hImageList ,Long hbmImage , Long crMask ) Library "PBVM90.dll" ALIAS FOR "PBImageList_AddMasked"
Function Long ImageList_GetIcon(Long hImageList , Long ImgIndex , Long hbmMask ) Library "PBVM90.dll" ALIAS FOR "PBImageList_GetIcon"
Function Long ImageList_Destroy(Long hImageList) Library "PBVM90.dll" ALIAS FOR "PBImageList_Destroy"
//ImageList 函数
//Function Long ImageList_Create (Long MinCx , Long MinCy , Long flags ,Long cInitial ,Long cGrow) Library "COMCTL32.DLL"
//Function Long ImageList_AddMasked(Long hImageList ,Long hbmImage , Long crMask ) Library "COMCTL32.DLL"
//Function Long ImageList_GetIcon(Long hImageList , Long ImgIndex , Long hbmMask ) Library "COMCTL32.DLL"

FUNCTION ulong RoundRect(ulong hdc,ulong X1,ulong Y1,ulong X2,ulong Y2,ulong X3,ulong Y3) LIBRARY "gdi32.dll"
FUNCTION ulong GetStockObject(ulong nIndex) LIBRARY "gdi32.dll"

function ulong SetCapture(ulong hwnd) library "user32.dll"
function boolean ReleaseCapture() library "user32.dll"
function ulong GetCapture() library "user32.dll"

//获得整个窗口的范围矩形，窗口的边框、标题栏、滚动条及菜单等都在这个矩形内 
FUNCTION ulong GetWindowRect(ulong hwnd,ref tagRECT lpRect) LIBRARY "user32.dll" alias for "GetWindowRect;Ansi"
//移动矩形
FUNCTION ulong OffsetRect(ref tagrect lpRect,ulong xs,ulong ys) LIBRARY "user32.dll" alias for "OffsetRect;Ansi"
FUNCTION ulong GetClientRect(ulong hwnd,ref tagRECT lpRect) LIBRARY "user32.dll" alias for "GetClientRect;Ansi"
FUNCTION ulong DefWindowProc(ulong hwnd,ulong wMsg,ulong wParam,ulong lParam) LIBRARY "user32.dll" ALIAS FOR "DefWindowProcA"
FUNCTION ulong DrawFocusRect(ulong hdc,ref tagRECT lpRect) LIBRARY "user32.dll" alias for "DrawFocusRect;Ansi"

Function long CreateCompatibleBitmap ( long hdc, long nwidth, long nheight ) library "gdi32"
Function uLong BitBlt(uLong hDestDC,uLong xx,uLong yy,uLong nWidth,uLong nHeight,uLong hSrcDC,uLong xSrc,uLong ySrc,uLong dwRop) LIBRARY "gdi32.dll"
Function uLong GetObjectBitmap( uLong  hgdiobj, int  cbBuffer, ref us_bitmap bm ) library "gdi32.dll" alias for "GetObjectA;Ansi"
FUNCTION ulong FrameRect(ulong hdc,ref tagRECT lpRect,ulong hBrush) LIBRARY "user32.dll" alias for "FrameRect;Ansi"
 

end prototypes

type variables
//public:
string normalPicName 
protected:
integer PicSize //customize pic's size

String mMovePicName 
String BKBMPName
//if PIC_Align is floating!,the button does't have Edge.
ToolBarAlignment PIC_Align = Alignatleft! 
//if ParentColor = true,then COLOR_BK_OUT = ParentWindow's BackColor
boolean ParentColor = false
boolean Border = true
long COLOR_BK_OUT = 14215660
long COLOR_BK_IN = 8900092
long COLOR_BK_DOWN = 12965593

long BorderColor = rgb(0,0,0)
long FontColor =rgb(0,0,0) 
long DisableFontColor=rgb(128,128,128)
long MoveinFontColor =rgb(0,0,0)
//TOOLTIP type,no suitable enumerate type ,so use rowfocusind
rowfocusind TOOLTIPTYPE = focusrect!
boolean HaveFocusRect=true 
private:
ulong h_bmpBk,h_normalPic,h_movePic
boolean ib_captured = false
boolean ib_pushDown = false
integer ii_picsize  
long il_BKDC =- 1
integer ii_BKW
integer ii_BKH
tagRect winRect

Private uo_tooltip ToolTip	//显示提示信息的用户对象实例

//-------------------------------------
//CONSTANT var
//SetBkMode
CONSTANT ulong  TRANSPARENT = 1
//DrawEdge
CONSTANT uLong BDR_RAISEDOUTER = 1
CONSTANT uLong BDR_SUNKENOUTER = 2
CONSTANT uLong BDR_RAISEDINNER = 4
CONSTANT uLong BDR_SUNKENINNER = 8
CONSTANT uLong BDR_OUTER =  3
CONSTANT uLong BDR_INNER =  12

CONSTANT uLong BF_LEFT = 1
CONSTANT uLong BF_RIGHT = 4
CONSTANT uLong BF_TOP = 2
CONSTANT uLong BF_BOTTOM = 8
CONSTANT uLong BF_RECT = 1
//GetSysColor GetSysColorBrush
CONSTANT uLong COLOR_BACKGROUND=1 
CONSTANT uLong COLOR_HIGHLIGHT= 13
CONSTANT uLong COLOR_HIGHLIGHTTEXT  =  14
CONSTANT uLong COLOR_MENU = 4
CONSTANT uLong COLOR_WINDOW = 5 
CONSTANT uLong COLOR_MENUTEXT = 7
CONSTANT uLong COLOR_WINDOWTEXT = 8
CONSTANT uLong COLOR_ACTIVECAPTION = 2
CONSTANT uLong COLOR_BTNFACE = 15
CONSTANT uLong COLOR_INFOBK = 24
Constant integer COLOR_BTNHIGHLIGHT = 20
Constant integer COLOR_BTNSHADOW = 16

//LoadImage
CONSTANT uLong IMAGE_BITMAP = 0
CONSTANT uLong IMAGE_ICON = 1
CONSTANT uLong IMAGE_CURSOR = 2
CONSTANT uLong IMAGE_ENHMETAFILE = 3

CONSTANT uLong LR_DEFAULTCOLOR = 0
CONSTANT uLong LR_MONOCHROME = 1
CONSTANT uLong LR_COLOR = 2
CONSTANT uLong LR_COPYRETURNORG = 4
CONSTANT uLong LR_COPYDELETEORG = 8
CONSTANT uLong LR_LOADFROMFILE = 16
CONSTANT uLong LR_LOADTRANSPARENT = 32
CONSTANT uLong LR_DEFAULTSIZE = 64
CONSTANT uLong LR_VGACOLOR = 128
CONSTANT uLong LR_LOADMAP3DCOLORS = 4096
CONSTANT uLong LR_CREATEDIBSECTION = 8192
CONSTANT uLong LR_COPYFROMRESOURCE = 16384
CONSTANT uLong LR_SHARED = 32768
//CreatePen
CONSTANT uLong PS_SOLID = 0


//DrawText
CONSTANT uLong DT_LEFT = 0
CONSTANT uLong DT_CENTER = 1
CONSTANT uLong DT_RIGHT = 2
CONSTANT uLong DT_VCENTER = 4
CONSTANT uLong DT_BOTTOM = 8
CONSTANT uLong DT_SINGLELINE = 32
CONSTANT uLong DT_CALCRECT =  1024
CONSTANT uLong DT_WORDBREAK = 16 

Constant integer DST_ICON = 3
Constant integer DST_BITMAP =4
Constant integer DSS_NORMAL =0
Constant integer DSS_DISABLED = 32
Constant integer DSS_MONO =  128
Constant integer  DSS_UNION = 16

Constant int SM_CYMENU = 15
Constant int SM_CXMENUCHECK = 71

//use in GetStockObject
Constant int WHITE_BRUSH=0
Constant int GRAY_BRUSH =1
Constant int LTGRAY_BRUSH=2
Constant int  DKGRAY_BRUSH=3
Constant int  BLACK_BRUSH=4
Constant int  NULL_BRUSH=5
Constant int  WHITE_PEN=6
Constant int  BLACK_PEN=7
Constant int  NULL_PEN=8
Constant int  OEM_FIXED_FONT=10
Constant int  ANSI_FIXED_FONT=11
Constant int  ANSI_VAR_FONT=12

Constant int  SYSTEM_FONT=13

Constant int WM_GETFONT = 49
//bitblt
constant long SRCCOPY = 13369376
constant long NOTSRCCOPY = 3342344
constant long SRCAND = 8913094
constant long SRCINVERT = 6684742
end variables

forward prototypes
private function integer of_getpicsize ()
private subroutine of_getrect (toolbaralignment aa_align, ref tagrect ast_cbrect, ref tagrect ast_picrect, ref tagrect ast_textrect, integer ai_picsize)
protected function long of_getstockicon (string as_stockbitmap, ref integer ai_imagetype)
public function window of_getparentwin ()
protected subroutine of_drawborder (long hdc, tagrect ast_rect)
protected function long of_geticonhandle (string as_iconname, integer ai_size)
protected function long of_getmaskcolor (long al_bitmaphandle)
private subroutine of_loadbkbmp (string as_bmp)
private subroutine of_drawbmpbk (long hdc, tagrect ast_rect)
protected subroutine of_destroyrc ()
public subroutine of_paint ()
public subroutine of_init_draw ()
public subroutine of_drawbk (long hdc, ref tagrect ast_rect)
end prototypes

event ue_paint;of_paint()

//validates the client area within a rectangle by removing the rectangle 
//from the update region of the specified window
ValidateRect(handle(this),winRect) 
return 1
end event

event ue_lbuttondown;//draw pushdown state
ib_pushDown = true 
this.setFocus()
of_paint()
//Don't processes this message
return 1
end event

event ue_mousemove;if (XPos<0 or YPos<0) or (XPos>=this.Width or YPos>this.Height) then //not in region
	IF ib_Captured THEN //release mouse
		ib_Captured = false
		ib_PushDown= false
		ReleaseCapture()
		//draw normal state
		of_paint() 
	END IF
elseif GetCapture()<>handle(this) THEN 	
	SetCapture(handle(this))
	ib_Captured = true
	if KeyDown(KeyLeftButton!) THEN		
		ib_PushDown= true
	end if
	//draw movein state
	of_paint()
end if

//registe hint
if TOOLTIPTYPE =focusrect! then 
	ToolTip.TIPSTYLE = ToolTip.STYLE_NORMAL
	ToolTip.ControlRegister(this,this.tag,0)
	ToolTip.Showtip(this)
elseif TOOLTIPTYPE =Hand! then
	ToolTip.TIPSTYLE = ToolTip.STYLE_BALLOONTIP
	ToolTip.ControlRegister(this,this.tag,0)
	ToolTip.Showtip(this)
end if
end event

event ue_lbuttonup;//draw pushup(normal) state

if ib_pushDown then postevent("clicked") //as lbuttondown event was't processed
ib_pushDown = false
ib_captured = false
ReleaseCapture()

of_paint()

end event

event ue_size;of_destroyrc()
of_init_draw()
end event

event ue_dbclk;//Don't processes this message
//In order to remove default draw
return 1
end event

event ue_keyup;
ib_pushdown = false
IF Key = KeySpaceBar! OR key= KeyEnter! THEN
   post of_paint()
end if

end event

event ue_keydown;IF Keydown( KeySpaceBar!) OR keydown(KeyEnter!) THEN
		IF NOT ib_pushdown THEN
			ib_pushdown = true
			post of_paint()
	   elseif (Keydown( KeySpaceBar!) and Key <> KeySpaceBar!) or( Key <> KeyEnter! and keydown(KeyEnter!)) then	
			 // KeySpaceBar is down and press other key
			 post of_paint()
		end if
	END IF


end event

event ue_enable;post of_paint()
end event

event ue_textchange();//====================================================================
// Function: uo_imflatbutton for ue_textchange()
//--------------------------------------------------------------------
// Description:按钮txt改变时触发 
//--------------------------------------------------------------------
// Arguments:(None)
//--------------------------------------------------------------------
// Returns:  
//--------------------------------------------------------------------
// Author:	yyx		Date: 2003.10.28
//--------------------------------------------------------------------
// Modify History: 
//	
//====================================================================

this.of_paint()
end event

private function integer of_getpicsize ();integer li_refer,li_cbwidth,li_cbHeight

li_cbwidth = UnitsToPixels ( width, XUnitsToPixels!)
li_cbHeight = UnitsToPixels ( height, YUnitsToPixels!)
if PicSize > 0 then 
	if li_cbwidth<PicSize then width = PixelsToUnits (PicSize, XPixelsToUnits!) +2
	if li_cbHeight<PicSize then height = PixelsToUnits(PicSize, XPixelsToUnits!) +2
	return PicSize 
end if
//if PicSize = 0 then calculate the pic's size
li_refer = min(li_cbwidth,li_cbHeight) *0.9 
li_refer = li_refer /8 
li_refer = li_refer*8
return li_refer


end function

private subroutine of_getrect (toolbaralignment aa_align, ref tagrect ast_cbrect, ref tagrect ast_picrect, ref tagrect ast_textrect, integer ai_picsize);//get the two rects that pic and text to occupy 

ast_textrect = ast_cbrect
ast_picrect = ast_cbrect
choose case aa_align
	case  alignatbottom!
        ast_textrect.bottom = ast_cbrect.bottom - ai_picsize -1
		  ast_picrect.top = ast_cbrect.bottom - ai_picsize
	case  alignatleft!
		  ast_textrect.left = ast_cbrect.left + ai_picsize +1
		  ast_picrect.right = ast_cbrect.left + ai_picsize
	case  alignatright!
		  ast_textrect.right = ast_cbrect.right - ai_picsize -1
		  ast_picrect.left = ast_cbrect.right - ai_picsize
	case  alignattop! 
        ast_textrect.top = ast_cbrect.top + ai_picsize +1
		  ast_picrect.bottom = ast_cbrect.top + ai_picsize
	case  floating! 
		 //no conduct
end choose



end subroutine

protected function long of_getstockicon (string as_stockbitmap, ref integer ai_imagetype);//////////////////////////////////////////////////////////////////////////////
//
//	函数名称:      Of_getstockicon
//	访问级别:  		Public
//	参数:          as_stockbitmap             stock位图名称
//                ai_imagetype               位图 or 图标标示 
//	返回值:        Long
//	功能描述:      得到系统stock位图相应的标识符
//////////////////////////////////////////////////////////////////////////////

Long  ll_Identifier

//检查参数
If IsNull(as_StockBitmap) Or as_StockBitmap="" Then Return -1

 ll_Identifier = FN_ResGetBitmapID(as_stockbitmap)
 
 if ll_Identifier<=0 then 
 	ll_Identifier = FN_ResGetIconID(as_stockbitmap)
	ai_imagetype= IMAGE_ICON
 else  //位图 
	ai_imagetype = IMAGE_BITMAP
 end if
 return ll_Identifier

end function

public function window of_getparentwin ();powerobject lpo_tmp
window	lw_tmp

lpo_tmp = this.getparent()

DO	While	lpo_tmp.typeof() <> Window!
	lpo_tmp=	lpo_tmp.getparent()
LOOP	

lw_tmp = lpo_tmp

return lw_tmp
end function

protected subroutine of_drawborder (long hdc, tagrect ast_rect);long hNewBrush
	
if ib_pushDown  then // Draw pressed state
	if PIC_Align<>floating! then
	  DrawEdge(hdc,ast_rect, BDR_SUNKENOUTER, BF_LEFT+BF_RIGHT+BF_TOP+BF_BOTTOM) 
   end if
elseif ib_Captured then // Draw mousemove state

	if PIC_Align<>floating! then
   	DrawEdge(hdc,ast_rect, BDR_RAISEDINNER, BF_LEFT+BF_RIGHT+BF_TOP+BF_BOTTOM)
   end if
else	// Draw normal state
	if Border then
		hnewBrush = CreateSolidBrush(BorderColor)
   	FrameRect(hdc,ast_rect,hnewBrush)
		deleteObject(hnewBrush)
	end if
end if	

end subroutine

protected function long of_geticonhandle (string as_iconname, integer ai_size);//====================================================================
// Function: of_geticonhandle()
//--------------------------------------------------------------------
// Description: Get the handle of icon from the toolbaritemname of the menu
//--------------------------------------------------------------------
// Arguments: 
//		value	string	as_iconname		
//--------------------------------------------------------------------
// Returns:  long  IconHandle
//--------------------------------------------------------------------
// Author:	Dale Zheng		Date: 2003.05.07
//--------------------------------------------------------------------
// Modify History: 
//	
//====================================================================

Long ll_BitmapHandle , ll_IconHandle , ll_Library , ll_Index=-1
Environment env
Integer rtn,li_imagetype
String ls_File
Long ll_identier
long ll_hIml

if pos(lower(as_iconname),'.ico')>0 then 
	ll_IconHandle= LoadImage(0,as_iconname,IMAGE_ICON,ai_size,ai_size,LR_LOADFROMFILE )
elseif pos(lower(as_iconname),'.bmp')>0 then 	
	ll_BitmapHandle =LoadImage(0,as_iconname,IMAGE_BITMAP,ai_size,ai_size,LR_LOADFROMFILE + LR_DEFAULTSIZE)

	ll_hIml = ImageList_Create (ai_size , ai_size , 33, 4, 4)
 	ll_Index = ImageList_AddMasked(ll_hIml , ll_BitmapHandle ,  Of_GetMaskColor(ll_BitmapHandle))
 	ll_IconHandle= ImageList_GetIcon( ll_hIml , ll_Index , 1)
  	//DeleteObject(ll_hIml)
	ImageList_Destroy(ll_hIml)
	DeleteObject(ll_BitmapHandle)
elseif pos(as_iconname,'!')>0 then 
	rtn = GetEnvironment(env)
   IF rtn <> 1 THEN
		MessageBox("TIP","Get the value is wrrong in the PowerBuilder Environment.",StopSign!,Ok!)
		Return -1
	End If
		
	ls_File = "PBVM" + String(env.PBMajorRevision) + "0.DLL"
	ll_Library = LoadLibrary(ls_File)
	ll_identier = Of_GetStockicon(as_iconname,li_imagetype)
	If ll_identier >0 and li_imagetype = IMAGE_BITMAP Then
	  		ll_BitmapHandle =  LoadBitmap ( ll_Library , ll_identier)
     		ll_hIml = ImageList_Create (16 , 16 , 33, 4, 4)
			ll_Index = ImageList_AddMasked(ll_hIml , ll_BitmapHandle ,  Of_GetMaskColor(ll_BitmapHandle))
			ll_IconHandle= ImageList_GetIcon( ll_hIml , ll_Index , 1)
 			//DeleteObject(ll_hIml)
			ImageList_Destroy(ll_hIml)
			DeleteObject(ll_BitmapHandle)
	elseIf ll_identier >0 and li_imagetype = IMAGE_ICON Then	
			ll_IconHandle= LoadImage(ll_Library,ll_identier,IMAGE_ICON,0,0, LR_DEFAULTSIZE )
	Else
			ll_index = -1
	End If
		
	FreeLibrary ( ll_Library )
else
	return 0
end if

return ll_IconHandle

end function

protected function long of_getmaskcolor (long al_bitmaphandle);//////////////////////////////////////////////////////////////////////////////
//	函数名称:      Of_GetMaskColor
//	访问级别:  		Public
//	参数:          al_BitmapHandle          位图句柄
//	返回值:        Long
//	功能描述:      获得指定位图文件的Mask颜色，返回值为Mask颜色
// 补充说明:
//////////////////////////////////////////////////////////////////////////////
//	修改记录：2001-11-15
// 编码：何锐
////////////////////////////////////////////////////////////////////////////
String ls_Null
Long ll_Hdc,ll_HDCTemp,ll_HbmpTemp
Long ll_MaskColor = -1

ll_HDC = CreateDC("DISPLAY",ls_Null,ls_Null,0)

If ll_HDC<>0 Then
   //创建一个与指定设备兼容的内存上下文环境
   ll_HDCTemp = CreateCompatibleDC(ll_HDC)
   If ll_HDCTemp<>0 Then
      //选择一个对象到指定的设备上下文环境
      ll_HbmpTemp = SelectObject(ll_HDCTemp, al_BitMapHandle)
		//获得位图坐标(0,0)颜色值
      ll_MaskColor = GetPixel(ll_HDCTemp,0,0)

      SelectObject(ll_HDCTemp,ll_HbmpTemp)

		DeleteDC(ll_HDCTemp)
   End If
	
   DeleteDC(ll_HDC)
End If

Return ll_MaskColor
end function

private subroutine of_loadbkbmp (string as_bmp);ulong ll_bmp,ll_new,ll_old,ll_rgb
us_bitmap lus_bmp
long ll_inst,ll_windc
SetNull(ll_inst)

//载入图片
ll_bmp = LoadImage(ll_inst,as_bmp,0,0,0,16)
if ll_bmp = 0 then 
	il_BKDC = -1
	return
end if

GetObjectBitmap( ll_bmp, 28, lus_bmp )
ii_BKW= lus_bmp.bmwidth
ii_BKH= lus_bmp.bmheight

ll_windc = getdc(handle(this))
il_BKDC = CreateCompatibleDC(ll_windc)

ll_old = SelectObject(il_BKDC,ll_bmp)
//释放资源
fillrect (ll_windc,winrect,ll_bmp)
Deleteobject(ll_bmp)
ReleaseDC(handle(this),ll_windc)
end subroutine

private subroutine of_drawbmpbk (long hdc, tagrect ast_rect);//if the button's background is a BMP ,then process this function.

//the BMP's size had better be more larger then the button	

	int li_cbwidth,li_cbHeight,li_W,li_H,li_srcx,li_srcy
	li_cbwidth =  ast_rect.right - ast_rect.left
	li_cbHeight =  ast_rect.bottom - ast_rect.top
	li_W = min(li_cbwidth,ii_BKW)
	li_H = min(li_cbHeight,ii_BKH)
	if ib_pushDown then
		li_srcx=1
	end if
	BitBlt(HDC, 0, 0,li_W,li_H, il_BKDC,li_srcx, 0, SrcCopy)
	
	li_cbwidth =li_cbwidth - ii_BKW -li_srcx
	int i 
	DO WHILE li_cbwidth > 0 //button's width is more longer then the loaded bmp's
		i++
		BitBlt(HDC, (ii_BKW - li_srcx)*i,0, ii_BKW - li_srcx,li_H, il_BKDC,li_srcx, 0, SrcCopy)
		li_cbwidth= li_cbwidth - ii_BKW - li_srcx 
   LOOP
	i=0
	li_cbHeight =li_cbHeight - ii_BKH -1
	DO WHILE li_cbHeight > 0 //button's Height is more longer then the loaded bmp's
		i++
		BitBlt(HDC, 0, (ii_BKH -1)* i ,ast_rect.right - ast_rect.left,ii_BKH,HDC,0, 1, SrcCopy)
		li_cbHeight= li_cbHeight - ii_BKH -1
   LOOP	
end subroutine

protected subroutine of_destroyrc ();destroyicon(h_normalPic)
destroyicon(h_MovePic)
if il_BKDC<> - 1 then 
	deleteDC(il_BKDC)
	il_BKDC= - 1
end if
end subroutine

public subroutine of_paint (); 
long hdc
tagRect picRect,textRect
int li_x,li_y
long ll_hFont,ll_oldFont,ll_oldFcolor


hdc = getdc(handle(this))

if h_normalPic>0 then
 of_getrect(PIC_Align,winRect,picRect,textRect,ii_picsize)
else
 textRect = winRect
end if

if PIC_Align = alignatleft! then
	li_y = (picRect.bottom - picRect.top - ii_picsize) /2
	li_x = li_y
elseif  PIC_Align = alignatright! then
	li_y = (picRect.bottom - picRect.top - ii_picsize) /2
	li_x = picRect.left - li_y 	
elseif PIC_Align = alignattop! then
	li_x = (picRect.right - picRect.left - ii_picsize) /2	
   li_y = 4//li_x 
elseif PIC_Align = alignatbottom! then
	li_x = (picRect.right - picRect.left - ii_picsize) /2	
   li_y = picRect.Top - li_x 	
end if	

if ib_pushDown then
	li_y ++
	li_x ++
	offsetRect(textRect,1,1)
end if	
if il_BKDC <> - 1 then
	of_drawBMPbk(hdc,winRect)
else
	of_drawbk(hdc,winRect)	
end if
of_drawBorder(hdc,winRect)

if  (ib_captured or ib_pushDown) and h_MovePic >0 then //mouse movein state
	DrawIconEx(hdc,li_x,li_y,h_MovePic,ii_picsize,ii_picsize,0,0,DST_ICON )
elseif enabled and h_normalPic>0 then //normal state
	DrawIconEx(hdc,li_x,li_y,h_normalPic,ii_picsize,ii_picsize,0,0,DST_ICON )
elseif not enabled then //disable state
	DrawState(hdc, 0, 0, h_normalPic, 0, li_x, li_y, 0 , 0, DST_ICON + DSS_DISABLED)
end if

SetBkMode(hdc,TRANSPARENT)
// Get the font in use on the Button
ll_hFont = Send(Handle(this), WM_GETFONT, 0, 0)
// Select it into the device context
ll_oldFont = SelectObject(Hdc, ll_hFont)
if  ib_captured then
	ll_oldFcolor = SetTextColor(hdc,MoveinFontColor)
else
	if this.Enabled THEN
		ll_oldFcolor = SetTextColor(hdc,FontColor)
	else
		ll_oldFcolor = SetTextColor(hdc,DisableFontColor)
	end if
end if
DrawText(hdc,text,- 1,textRect,DT_CENTER+DT_VCENTER+DT_SINGLELINE)
SetTextColor(hdc,ll_oldFcolor)
SelectObject(Hdc, ll_oldFont)

if getfocus()=this and HaveFocusRect then //draw FocusRect
	
	tagrect FocusRect
	//get FocusRect's size
	FocusRect.left  = winRect.left +4
   FocusRect.top = winRect.top +4
   FocusRect.right = winRect.right -4
	FocusRect.bottom = winRect.bottom -4
	
	DrawFocusRect(hdc,FocusRect)
end if


releaseDC(handle(this),hdc)


end subroutine

public subroutine of_init_draw ();//initiate some instance vars
if ParentColor  then COLOR_BK_OUT = of_getParentWin().backColor
ii_picsize = of_getpicsize()
if normalPicName<>"" then h_normalPic = of_geticonhandle(normalPicName,ii_picsize)
if mMovePicName<>"" then h_MovePic = of_geticonhandle(mMovePicName,ii_picsize)
GetClientRect(handle(this),winRect) 

if BKBMPName<>"" then
  of_loadbkbmp(BKBMPName)
end if

end subroutine

public subroutine of_drawbk (long hdc, ref tagrect ast_rect);long hNewBrush,hnewPen,holdPen,holdBrush
	
if ib_pushDown  then // Draw pressed state
	if PIC_Align=floating! then
	  hNewBrush =CreateSolidBrush(COLOR_BK_OUT)
   else
	  hNewBrush =CreateSolidBrush(COLOR_BK_DOWN)
   end if
	FillRect(hdc,ast_rect,hNewBrush)
	DeleteObject(hNewBrush)	
elseif ib_Captured then // Draw mousemove state

   if PIC_Align=floating! then
	  hNewBrush =CreateSolidBrush(COLOR_BK_OUT)
   else
	  hNewBrush =CreateSolidBrush(COLOR_BK_IN)
   end if
	FillRect(hdc,ast_rect,hNewBrush)
	DeleteObject(hNewBrush)	
else	// Draw normal state
	hNewBrush =CreateSolidBrush(COLOR_BK_OUT)
	FillRect(hdc,ast_rect,hNewBrush)
	DeleteObject(hNewBrush)
end if	

end subroutine

event constructor;//----------------------------------------------------------------*
//	UserObject:		uo_imflatbutton									      *
//	Description:	flatbutton (ONLY WINDOW MENU SUPPORTED)			*
// known bugs: 
//      if one or more buttons are checked "default" or "Cancel" properties,
//      as the button use default CommandButtonWnd to try to refresh,
//      maybe can see default appearance.
//----------------------------------------------------------------*
//	Author(s):	         Date 				Remark							*
//	bluetlck		         2003.05.20     Develop							*
//	rjh				      2003.05.20		fixed a few bugs				*								
//																						*
//----------------------------------------------------------------*



constant long BS_OWNERDRAW = 11
constant long GWL_STYLE = -16
//constant long  BS_GROUPBOX = 7
constant long BS_PUSHBUTTON = 0
//BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP

//constant long WS_CHILD =1073741824
//constant long WS_TABSTOP = 65536
//constant long WS_VISIBLE = 268435456
//nStyle =BS_PUSHBUTTON + WS_CHILD + WS_VISIBLE + WS_TABSTOP
long nStyle
//将按钮设置为OWNERDRAW button
nStyle = GetWindowuLong(handle(this), GWL_STYLE)
nStyle = nStyle + BS_OWNERDRAW 
SetWindowuLong(handle(this), GWL_STYLE, nStyle)
//载入图标
of_init_draw()

end event

on uo_imflatbutton.create
end on

on uo_imflatbutton.destroy
end on

event destructor;of_destroyRC()  //释放图标资源
end event

event getfocus;post of_paint()

end event

