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

剩下的 事情就是自己加進工程自個看咯

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章