c语言图形界面填充,「分享」C语言如何编写图形界面

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

贴吧内经常有人问C语言是不是只能用于字符终端界面开发,不能用于图形界面。大家也都有回答,需要其他的库。MFC,GTK,QT。

本人近期刚用GTK库加上纯C写成了第一个LINUX实用程序。现在与大家分享:

acbec432f0fe38f3ba8eaec10217c494.png

主界面程序gmaxtrix.c

#include

#include"client_socket.h"

#define MAX_BUFFER 1024

static GtkWidget *IP_entry;

static GtkWidget *port_entry;

static gint sockfd;

static gint tag;

gchar buffer[MAX_BUFFER];

void g_show_alarm_dialog(const gchar *message){

GtkWidget *dialog;

dialog = gtk_message_dialog_new(NULL,

GTK_DIALOG_DESTROY_WITH_PARENT,

GTK_MESSAGE_WARNING,

GTK_BUTTONS_OK,

"%s",message);

gtk_window_set_title(GTK_WINDOW(dialog),"警告");

gtk_dialog_run(GTK_DIALOG(dialog));

gtk_widget_destroy(dialog);

}

void g_client_send(GtkWidget *widget, gpointer textbuffer){

if (sockfd > 0){

client_send(sockfd, gtk_entry_buffer_get_text(GTK_ENTRY_BUFFER(textbuffer)), gtk_entry_buffer_get_bytes(GTK_ENTRY_BUFFER(textbuffer)));

}else{

g_show_alarm_dialog("未连接到中控主机");

}

}

void g_client_recv(gpointer data, gint source, GdkInputCondition condition){

gint count = client_recv(source, buffer, MAX_BUFFER);

buffer[count] = 0;

GtkTextIter iter;

gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(data),&iter);

gtk_text_buffer_insert(GTK_TEXT_BUFFER(data),&iter,buffer,-1);

}

void g_client_connect(GtkWidget *widget, gpointer date){

if (sockfd > 0){

client_close(sockfd);

gtk_button_set_label(GTK_BUTTON(widget),"连接");

gdk_input_remove(tag);

}else{

sockfd = client_connect(gtk_entry_get_text(GTK_ENTRY(IP_entry)),atoi(gtk_entry_get_text(GTK_ENTRY(port_entry))));

gtk_button_set_label(GTK_BUTTON(widget),"断开");

tag = gdk_input_add(sockfd, GDK_INPUT_READ, g_client_recv,date);

}

}

int main(int argc, char *argv[]){

gtk_init(&argc, &argv);

GtkWidget *window;

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit),NULL);

gtk_widget_set_size_request(window, 260, 250);

gtk_container_set_border_width(GTK_CONTAINER(window),5);

GtkWidget *vbox;

vbox = gtk_vbox_new(FALSE,1);

gtk_container_add(GTK_CONTAINER(window),vbox);

GtkWidget *hbox1;

hbox1 = gtk_hbox_new(FALSE,1);

gtk_box_pack_start(GTK_BOX(vbox), hbox1, FALSE, FALSE ,0);

GtkWidget *table = gtk_table_new(2,2,FALSE);

gtk_box_pack_start(GTK_BOX(hbox1), table, TRUE , TRUE , 0);

GtkWidget *IP_label = gtk_label_new("地址:");

gtk_table_attach_defaults(GTK_TABLE(table),IP_label,0,1,0,1);

IP_entry = gtk_entry_new();

gtk_table_attach_defaults(GTK_TABLE(table),IP_entry,1,2,0,1);

GtkWidget *port_label = gtk_label_new("端口:");

gtk_table_attach_defaults(GTK_TABLE(table),port_label,0,1,1,2);

port_entry = gtk_entry_new();

gtk_table_attach_defaults(GTK_TABLE(table),port_entry,1,2,1,2);

GtkWidget *connect_button = gtk_button_new_with_label("连接");

gtk_box_pack_start(GTK_BOX(hbox1), connect_button, TRUE, TRUE ,0);

GtkWidget *hseparator = gtk_hseparator_new();

gtk_box_pack_start(GTK_BOX(vbox), hseparator, FALSE, FALSE, 0);

GtkWidget *hbox2;

hbox2 = gtk_hbox_new(FALSE,1);

gtk_box_pack_start(GTK_BOX(vbox), hbox2, FALSE, FALSE ,0);

GtkWidget *send_entry = gtk_entry_new();

gtk_box_pack_start(GTK_BOX(hbox2), send_entry, TRUE, TRUE ,0);

GtkWidget *send_button = gtk_button_new_with_label("发送");

gtk_box_pack_start(GTK_BOX(hbox2), send_button, TRUE, TRUE ,0);

g_signal_connect(send_button, "clicked", G_CALLBACK(g_client_send), gtk_entry_get_buffer(GTK_ENTRY(send_entry)));

GtkWidget *recv_text = gtk_text_view_new();

gtk_box_pack_start(GTK_BOX(vbox), recv_text, TRUE, TRUE, 0);

g_signal_connect(connect_button, "clicked", G_CALLBACK(g_client_connect), gtk_text_view_get_buffer(GTK_TEXT_VIEW(recv_text)));

gtk_widget_show_all(window);

gtk_main();

return 0;

}

后台网络通迅模型接口声明client_socket.h

#ifndef CLIENT_SOCKET_H

#define CLIENT_SOCKET_H

int client_connect(const char *IP, const int port);

void client_close(int sockfd);

void client_send(int sockfd,const unsigned char *buffer, const size_t count);

size_t client_recv(int sockfd,unsigned char *buffer,size_t count);

#endif

后台网络通迅模型实现client_socket.c(采用POSIX接口,WIN下需改为win32API接口)

#include

#include

#include

#include

#include

#include

#include "client_socket.h"

int client_connect(const char *IP, const int port){

static struct sockaddr_in servaddr;

int sockfd = socket(AF_INET,SOCK_STREAM,0);

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

inet_pton(AF_INET, IP, &servaddr.sin_addr);

servaddr.sin_port = htons(port);

connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

return sockfd;

}

void client_close(int sockfd){

close(sockfd);

}

void client_send(int sockfd,const unsigned char *buffer, const size_t count){

send(sockfd,buffer,count,0);

}

size_t client_recv(int sockfd,unsigned char *buffer,size_t count){

return recv(sockfd,buffer,count,0);

}

编译脚本makefile

gmaxtrix: gmaxtrix.o client_socket.o

gcc gmaxtrix.o client_socket.o -o gmaxtrix `pkg-config --cflags --libs gtk+-2.0`

gmaxtrix.o: gmaxtrix.c client_socket.h

gcc -c gmaxtrix.c `pkg-config --cflags --libs gtk+-2.0`

client_socket.o: client_socket.h client_socket.c

gcc -c client_socket.c `pkg-config --cflags --libs gtk+-2.0`

clean:

@echo "cleanning project"

-rm gmaxtrix *.o

@echo "clean completed"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值