在触摸设备上做软件开发很多时候需要界面实现拖拽效果,近期的电话软件开发中设计到了列表控件(gtktreeview实现),比如通讯录,铃音列表等,控件完成后也为其增加了拖拽效果,仅仅实现了y轴的拖拽效果,总结如下:
创建全局标识:
//是否处于拖拽状态
gboolean DRAG = FALSE;
gint drag_last_y;
创建好列表控件后为其连接信号:
g_signal_connect(G_OBJECT(pTreeView), "motion_notify_event",
G_CALLBACK(mouse_motion_cb), NULL);
g_signal_connect(G_OBJECT(pTreeView), "button_press_event",
G_CALLBACK(mouse_press_cb), NULL);
g_signal_connect(G_OBJECT(pTreeView), "button_release_event",
G_CALLBACK(mouse_release_cb), NULL);
实现对应的回调函数:
static gint mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
{
if(DRAG)
{
gint tx, ty;
//获取当前binwindow(0,0)位置 对应的tree coordinates中的坐标
gtk_tree_view_convert_bin_window_to_tree_coords(widget
0,
0,
&tx,
&ty);
//拖拽过程中在y轴的位移
gint moved = bevent->y - drag_last_y;
drag_last_y = bevent->y;
gtk_tree_view_scroll_to_point(pList->pTreeView, -1, ty - moved);
}
return FALSE;
}
static gint mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
{
DRAG = TRUE;
drag_last_y = bevent->y;
return FALSE;
}
static gint mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
{
DRAG = FALSE;
return FALSE;
}
阅读(1306) | 评论(0) | 转发(0) |