2012-02-16 40 views
5

我正在使用goocanvas在屏幕上顯示圖形的GTK +應用程序。我有問題想出一個實現拖動滾動的好方法。拖動GTK +應用程序滾動

目前應用程序保存用戶點擊的座標,然後在「motion-notify」信號回調中,將goo_canvas_scroll_to()添加到新的位置。問題在於繪圖速度有點慢,並且每一個像素都被鼠標移動,我得到的回調被調用一次。拖動圖形時,這會使繪圖滯後。

有沒有一種很好的方式來做拖動滾動,所以它會顯得更加流暢,我可以跳過一些重繪?

回答

4

當用戶按下鼠標按鈕時,我可以通過啓動一個5ms定時器來得到類似這樣的工作。在計時器中,我檢查鼠標的位置,並決定滾動的方式,包括更快地滾動距離邊緣越近。結果是非常順利的滾動,至少這是我記得的。在這裏它的膽量,它的gtkmm/C++,但你應該能夠得到它的要點:

static const int HOT_AREA = 24; 

// convert distance into scroll size. The larger the 
// value, the faster the scrolling. 
static int accel_fn(int dist) { 
    if (dist > HOT_AREA) 
     dist = HOT_AREA; 
    int dif = dist/(HOT_AREA/4); 
    if (dif <= 0) dif = 1; 
    return dif; 
} 


bool scrollerAddin::on_timeout() { 
    int ptr_x, ptr_y; 
    o_scroller->get_pointer(ptr_x, ptr_y); 

    int vp_width = o_scroller->get_width(); 
    int vp_height = o_scroller->get_height(); 

    if (o_scroller->get_hscrollbar_visible()) 
     vp_height -= o_scroller->get_hscrollbar()->get_height(); 
    if (o_scroller->get_vscrollbar_visible()) 
     vp_width -= o_scroller->get_vscrollbar()->get_width(); 

    if (ptr_x < HOT_AREA) 
     scroll_left(accel_fn(HOT_AREA-ptr_x)); 
    else if (ptr_x > vp_width - HOT_AREA) 
     scroll_right(accel_fn(ptr_x - (vp_width - HOT_AREA))); 
    if (ptr_y < HOT_AREA) 
     scroll_up(accel_fn(HOT_AREA - ptr_y)); 
    else if (ptr_y > vp_height - HOT_AREA) 
     scroll_down(accel_fn(ptr_y - (vp_height - HOT_AREA))); 

    return true; 
} 

的滾動功能僅通過參數調整適當的調整對象。

+0

在定時器回調中做它是個好主意。這正是我需要的,謝謝! – 2012-02-17 13:11:22