HTMLayout滚动条behavior源码示例

1.核心API:get_scroll_info() 和 set_scroll_pos()

       该效果的显示主要是控制内外区域的协调显示问题.多说无益看代码

2.相关代码.h文件

                 

#include "..\behaviors\behavior_aux.h"

/***************************************************
垂直滚动条自动滚动
示例:
<div Backandforth style="behavior:UDB_V_Autoscroller;width:100px;height:200px; border:1px solid red;margin:*;overflow:hidden;padding:5px;">
       <option style="margin-top:5px;border:1px solid gray;height:20px;">1</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">2</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">3</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">4</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">5</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">6</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">7</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">8</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">9</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">10</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">11</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">12</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">13</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">14</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">15</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">16</option>
       <option style="margin-top:5px;border:1px solid gray;height:20px;">17</option>
</div>

属性:Backandforth 这个属性表示上下弹性展示内部元素,如果没有这个属性则一一循环展示内部元素

***************************************************/

namespace htmlayout 
{
	struct UDB_V_Autoscroller: public behavior
	{
		UDB_V_Autoscroller(const char* name = "UDB_V_Autoscroller");
		virtual void attached  (HELEMENT he );
		virtual void detached  (HELEMENT he );

		virtual BOOL on_mouse  (HELEMENT he, HELEMENT target, UINT event_type, POINT pt, UINT mouseButtons, UINT keyboardStates );
		virtual BOOL on_timer  (HELEMENT he );
	};

	UDB_V_Autoscroller     UDB_V_Autoscroller_instance;	
}

3.cpp文件

                 

#include "stdafx.h"
#include <time.h>
#include "UDB_V_Autoscroller.h"
namespace htmlayout 
{
		UDB_V_Autoscroller::UDB_V_Autoscroller(const char* name ): behavior(HANDLE_TIMER|HANDLE_MOUSE, name) {}
		void UDB_V_Autoscroller::attached  (HELEMENT he ) 
		{ 
			HTMLayoutSetTimer( he, 30); 
		} 

		void UDB_V_Autoscroller::detached  (HELEMENT he ) 
		{ 
			HTMLayoutSetTimer( he, 0 ); 
		} 

		BOOL UDB_V_Autoscroller::on_mouse(HELEMENT he, HELEMENT target, UINT event_type, POINT pt, UINT mouseButtons, UINT keyboardStates )
		{
			if( event_type == MOUSE_ENTER )
				HTMLayoutSetTimer( he, 0 );

			if( event_type == MOUSE_LEAVE)
				HTMLayoutSetTimer( he, 30 );

			return 0;
		}


		BOOL UDB_V_Autoscroller::on_timer  (HELEMENT he ) 
		{ 
			POINT scroll_pos;//滚动条当前位置
			RECT  view_rect; //滚动条当前区域
			SIZE  content_size;//滚动条最大区域

			dom::element el = he;
			el.get_scroll_info(scroll_pos, view_rect, content_size);

			const wchar_t* cw = el.get_attribute("Backandforth");

			if(NULL != cw)
			{
				static bool bDown = 0;

				if(content_size.cy - view_rect.bottom < scroll_pos.y)
					bDown=0;
				else if(scroll_pos.y==0)
					bDown=1;

				if(!bDown)	scroll_pos.y -= 2;
				else
					scroll_pos.y += 2;

				el.set_scroll_pos(scroll_pos, false);
			}
			else
			{
				if(scroll_pos.y > content_size.cy)
					scroll_pos.y = 0 - view_rect.bottom;		

				scroll_pos.y += 2;
				el.set_scroll_pos(scroll_pos, false);
			}

			return 1; 
		}

} // htmlayout namespace

剩下的 事情就是自己加进工程自个看咯

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值