[b]此分栏列表会从指定文本文件中读取数据显示出来
文本文件中的内容是这样[/b]
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
文本文件中的内容是这样[/b]
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
张三:男人:哈哈:哈哈
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_LINE_SIZE 400
int main(int argc,char *argv[])
{
GtkWidget *window;
GtkWidget *clist;
GtkWidget *image;
gint i;
FILE *fp;
gchar *line;
gchar *token;
gchar *row[4];
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_signal_connect(GTK_OBJECT(window),"delete_event",GTK_SIGNAL_FUNC(gtk_main_quit),NULL);
gtk_widget_set_size_request(window,500,500);
gtk_container_set_border_width(GTK_CONTAINER(window),1);
clist=gtk_clist_new(4);
gtk_clist_set_column_width(GTK_CLIST(clist),0,100);
gtk_clist_set_column_width(GTK_CLIST(clist),1,100);
gtk_clist_set_column_width(GTK_CLIST(clist),2,100);
gtk_clist_set_column_width(GTK_CLIST(clist),3,100);
gtk_clist_set_column_justification(GTK_CLIST(clist),3,GTK_JUSTIFY_CENTER);
gtk_clist_set_row_height(GTK_CLIST(clist),28);
gtk_clist_set_column_title(GTK_CLIST(clist),0,"姓名");
gtk_clist_set_column_title(GTK_CLIST(clist),1,"性别");
gtk_clist_set_column_title(GTK_CLIST(clist),2,"年龄");
gtk_clist_set_column_title(GTK_CLIST(clist),3,"年1龄");
//gtk_clist_column_title_passive(GTK_CLIST(clist),1);/*设置编号为1的列标题为不活动的列标题*/
//image=gtk_image_new_from_file("1"); /*创建图片构件,大家可以把参数改为自己的图片文件名*/
//gtk_clist_set_column_widget(GTK_CLIST(clist),0,image);/*设置标题按钮构件*/
gtk_clist_column_titles_show(GTK_CLIST(clist));
line = (char*)malloc( MAX_LINE_SIZE * sizeof(char) );
if((fp = fopen("users.txt", "r"))==NULL)
{
printf("can not open file %s !\n", "file.txt");
exit(-1);
}
while( fgets(line, MAX_LINE_SIZE, fp) != NULL )
{
i=0;
memset(row,0,sizeof(row));
row[i] = strtok(line,":");
while((token = strtok(NULL, ":")) != NULL)
{
i++;
row[i] = token;
}
gtk_clist_append(GTK_CLIST(clist),row);
}
fclose(fp);
gtk_container_add(GTK_CONTAINER(window),clist);
gtk_widget_show(clist);
gtk_widget_show(window);
gtk_main();
}