红联Linux门户
Linux帮助

公布自写流量悬浮窗代码,非常简单,几十行。

发布时间:2012-04-12 18:11:29来源:红联作者:东方巽雷
[i=s] 本帖最后由 东方巽雷 于 2012-8-30 20:02 编辑 [/i]

使用gtk+-3.0和glade3编写。

经过修改,更加精简。
直接贴源码吧
#include
#include
#include
#include
#include

GtkWidget *window,*table,*tv1,*tv2,*tv3,*tv4;
GtkTextBuffer *tb1,*tb2,*tb3,*tb4;
gchar *getup,*getdown;


void itoa(int num,char buffer[]){
gint i=0,j=0;
gchar tmp[20]={'\0'};
if(num==0){
buffer[0]='0';
buffer[1]='\0';
return;
}
for(;num>0;num/=10)
tmp[j++]='0'+num%10;
for(j--;j>=0;j--)
buffer[i++]=tmp[j];
buffer[i]='\0';
return;

}
//获取下载总字节
long get_down_byte(){
gchar buffer[20];
FILE *fp;

fp=popen(getdown,"r");
fgets(buffer,sizeof(buffer),fp);
pclose(fp);

return atol(buffer);
}
//获取上传总字节
long get_up_byte(){
gchar buffer[20];
FILE *fp;

fp=popen(getup,"r");
fgets(buffer,sizeof(buffer),fp);
pclose(fp);
return atol(buffer);
}



//使得悬浮窗响应鼠标事件
gboolean on_textview_button_press_event(GtkWidget* widget,GdkEventButton* event,
GdkWindowEdge edge){
if (event->type == GDK_BUTTON_PRESS)
{
//判断按下的是否是鼠标左键。
if (event->button == 1){
//使得去除窗口装饰的窗口可以拖曳
gtk_window_begin_move_drag(GTK_WINDOW(gtk_widget_get_toplevel(widget)),
event->button,event->x_root,event->y_root,event->time); }
//判断按下的是否是鼠标右键。
else if(event->button == 3){
gtk_main_quit();
}
}
return FALSE;
}

//实时更新流量显示,隔秒读取一次总传输字节,相减即为网速。
void refresh_data(){

gint u1,u2,d1,d2;

gchar dbuffer[20]={'\0'},ubuffer[20]={'\0'};


while(1){

d1=get_down_byte()/1024;
u1=get_up_byte()/1024;

sleep(1);

d2=get_down_byte()/1024;
u2=get_up_byte()/1024;

itoa(d2-d1,dbuffer);
itoa(u2-u1,ubuffer);

strcat(dbuffer,"KB/s");
strcat(ubuffer,"KB/s");

gdk_threads_enter();
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv4)),dbuffer,-1);
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv2)),ubuffer,-1);
gdk_threads_leave();
}
}


int main(int argc,char *argv[])
{
if(argc!=2){
g_printf("\nUsage: %s \n",argv[0]);return 1;
}
getup = g_strconcat("cat /proc/net/dev|grep ",argv[1],"|awk \'{print $10}\'",NULL);
getdown = g_strconcat("cat /proc/net/dev|grep ",argv[1],"|awk \'{print $2}\'",NULL) ;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_focus_on_map(GTK_WINDOW(window),FALSE);
gtk_window_set_accept_focus(GTK_WINDOW(window),FALSE);
gtk_window_set_opacity(GTK_WINDOW(window),0.6);
gtk_window_set_skip_taskbar_hint(GTK_WINDOW(window),TRUE);
//使窗口自适应字符长度
gtk_window_set_resizable(GTK_WINDOW(window),FALSE);
//去掉窗口装饰
gtk_window_set_decorated(GTK_WINDOW(window),FALSE);
//保持窗口最前
gtk_window_set_keep_above(GTK_WINDOW(window),TRUE);
//gtk_window_set_default_size(window,60,35);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
table = gtk_table_new( 2,2,TRUE);
//gtk_table_set_row_spacings(GTK_TABLE(table),2);
//gtk_table_set_col_spacings(GTK_TABLE(table),2);
tv1 = gtk_text_view_new_with_buffer(GTK_TEXT_BUFFER(tb1));
tv2 = gtk_text_view_new_with_buffer(GTK_TEXT_BUFFER(tb2));
tv3 = gtk_text_view_new_with_buffer(GTK_TEXT_BUFFER(tb3));
tv4 = gtk_text_view_new_with_buffer(GTK_TEXT_BUFFER(tb4));
gtk_text_view_set_editable(GTK_TEXT_VIEW(tv1),FALSE);
gtk_text_view_set_editable(GTK_TEXT_VIEW(tv2),FALSE);
gtk_text_view_set_editable(GTK_TEXT_VIEW(tv3),FALSE);
gtk_text_view_set_editable(GTK_TEXT_VIEW(tv4),FALSE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(tv1),FALSE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(tv2),FALSE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(tv3),FALSE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(tv4),FALSE);
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv1)),"上传:",-1);
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv2)),"0 KB/s",-1);
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv3)),"下载:",-1);
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv4)),"0 KB/s",-1);
gtk_table_attach_defaults(GTK_TABLE(table),tv1,0,1,0,1);
gtk_table_attach_defaults(GTK_TABLE(table),tv2,1,2,0,1);
gtk_table_attach_defaults(GTK_TABLE(table),tv3,0,1,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),tv4,1,2,1,2);
gtk_table_set_homogeneous(GTK_TABLE(table),FALSE);
gtk_container_add(GTK_CONTAINER(window),table);
gtk_widget_show_all(window);
g_signal_connect(G_OBJECT(tv1),"button-press-event",G_CALLBACK(on_textview_button_press_event),NULL);
g_signal_connect(G_OBJECT(tv2),"button-press-event",G_CALLBACK(on_textview_button_press_event),NULL);
g_signal_connect(G_OBJECT(tv3),"button-press-event",G_CALLBACK(on_textview_button_press_event),NULL);
g_signal_connect(G_OBJECT(tv4),"button-press-event",G_CALLBACK(on_textview_button_press_event),NULL);

if(!g_thread_supported())
g_thread_init(NULL);
gdk_threads_init();
g_thread_create((GThreadFunc)refresh_data,NULL,FALSE,NULL);
gdk_threads_enter();
gtk_main();
gdk_threads_leave();

return 0;
}
文章评论

共有 19 条评论

  1. ZXSCHNLFAHQ2676 于 2016-03-23 21:50:58发表:

    支持

  2. ZXSCHNLFAHQ2676 于 2016-03-23 21:50:41发表:

    支持

  3. 黑色 于 2015-12-09 10:54:31发表:

    卧槽,多久以前的了,下下来研究一下

  4. lyriezheng 于 2015-03-04 11:07:51发表:

    程序参数是什么?

  5. tianzhang 于 2015-02-14 13:47:27发表:

    顶下

  6. jcx631678546 于 2015-02-12 23:26:02发表:

    诶 但是为什么有2个不同的呢?

  7. jcx631678546 于 2015-02-12 23:25:17发表:

    寻找了很多的东西了,才找到这个的`` 谢楼主

  8. xuchen70 于 2014-10-12 01:01:48发表:

    回复有金币吗?

  9. stokley 于 2014-07-29 16:09:40发表:

    支持。。。。。。。。。。

  10. dalin 于 2014-05-20 10:31:43发表:

    怎么下载不了啊

  11. feng201310 于 2014-05-19 14:34:54发表:

    支持

  12. zengyi8203 于 2014-05-19 10:23:04发表:

    good资料

  13. alexwilbur 于 2014-05-05 11:05:34发表:

    积分现金不够,哥们能给发个到邮箱不,zengyi8203@hotmail.com

  14. 秦合六一 于 2014-01-14 22:23:46发表:

    对楼主所在的领域不太熟悉

  15. 283656429 于 2013-10-16 15:54:35发表:

    试试好用不

  16. jeffkuang1 于 2013-10-14 15:47:01发表:

    顶一下。

  17. xiaoweiboke 于 2013-05-19 21:06:07发表:

    谢谢了

  18. hml1006 于 2012-04-18 10:17:34发表:

    对gui编程很不熟

  19. glsow 于 2012-04-13 08:51:04发表:

    友情支持