GTK—Linux Info信息
文件名Info.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <gtk-2.0/gtk/gtk.h>
using namespace std;
int return_i = -1;
static void PASS (GtkWidget *widget,
gpointer data)
{
g_print ("Test PASS\n");
return_i = 0;
}
static void FAIL (GtkWidget *widget,
gpointer data)
{
g_print ("Test FAIL\n");
return_i = 1;
}
static gboolean on_delete_event (GtkWidget *widget,
GdkEvent *event,
gpointer data)
{
g_print ("Test Close\n");
return_i = 2;
return TRUE;
}
int main (int argc, char *argv[])
{
system("sudo dmidecode -t bios | grep -i Version | awk -F \'\t\' \'{print $NF}\'>bios");
system("sudo dmidecode -t bios | grep -i Date| awk -F \'\t\' \'{print $NF}\'>>bios");
system("grep -i \"model name\" /proc/cpuinfo | awk -F \':\' \'{print $NF}\'>cpu");
system("lsmem | grep -i Total>mem");
system("lsblk -o NAME,SIZE,TYPE,HOTPLUG | grep -i disk>hdd");
system("");
ifstream biosfile("./bios", ios::in);
if(!biosfile){
cout << "./bios" << " file opening failed" << endl;
return 1;
}
string biostemp((istreambuf_iterator<char>(biosfile)), (istreambuf_iterator<char>()));
biosfile.close();
gchar *bios;
bios = (char*)biostemp.data();
ifstream cpufile("./cpu", ios::in);
if(!cpufile){
cout << "./cpu" << " file opening failed" << endl;
return 1;
}
string cputemp((istreambuf_iterator<char>(cpufile)), (istreambuf_iterator<char>()));
cpufile.close();
gchar *cpu;
cpu = (char*)cputemp.data();
ifstream memfile("./mem", ios::in);
if(!memfile){
cout << "./mem" << " file opening failed" << endl;
return 1;
}
string memtemp((istreambuf_iterator<char>(memfile)), (istreambuf_iterator<char>()));
memfile.close();
gchar *mem;
mem = (char*)memtemp.data();
ifstream hddfile("./hdd", ios::in);
if(!hddfile){
cout << "./hdd" << " file opening failed" << endl;
return 1;
}
string hddtemp((istreambuf_iterator<char>(hddfile)), (istreambuf_iterator<char>()));
hddfile.close();
gchar *hdd;
hdd = (char*)hddtemp.data();
GtkWidget *window;
GtkWidget *pass_button,*fail_button;
GtkWidget *hbox1,*hbox2,*hbox3,*hbox4;
GtkWidget *frame1,*frame2,*frame3,*frame4;
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *biosView,*cpuView,*memView,*hddView;
GtkTextBuffer *biosbuf,*cpubuf,*membuf,*hddbuf;
GtkWidget *scrolledbios,*scrolledcpu,*scrolledmem,*scrolledhdd;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "iNet Tools");
gtk_window_set_default_size(GTK_WINDOW(window),500,600);
gtk_window_set_position(GTK_WINDOW (window), GTK_WIN_POS_CENTER);
g_signal_connect (window, "delete-event", G_CALLBACK (on_delete_event), NULL);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
scrolledbios = gtk_scrolled_window_new(NULL, NULL);
scrolledcpu = gtk_scrolled_window_new(NULL, NULL);
scrolledmem = gtk_scrolled_window_new(NULL, NULL);
scrolledhdd = gtk_scrolled_window_new(NULL, NULL);
frame1 = gtk_frame_new ("BIOS Info:");
frame2 = gtk_frame_new ("CPU Info:");
frame3 = gtk_frame_new ("MEM Info:");
frame4 = gtk_frame_new ("HDD Info:");
vbox = gtk_vbox_new(FALSE, 5);
hbox = gtk_hbox_new(TRUE, 5);
hbox1 = gtk_hbox_new(TRUE, 5);
hbox2 = gtk_hbox_new(TRUE, 5);
hbox3 = gtk_hbox_new(TRUE, 5);
hbox4 = gtk_hbox_new(TRUE, 5);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_container_add (GTK_CONTAINER (vbox), frame1);
gtk_container_add (GTK_CONTAINER (vbox), frame2);
gtk_container_add (GTK_CONTAINER (vbox), frame3);
gtk_container_add (GTK_CONTAINER (vbox), frame4);
gtk_container_add (GTK_CONTAINER (vbox), hbox);
gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
gtk_container_add (GTK_CONTAINER (frame1), hbox1);
gtk_container_set_border_width (GTK_CONTAINER (hbox1), 10);
gtk_container_add (GTK_CONTAINER (frame2), hbox2);
gtk_container_set_border_width (GTK_CONTAINER (hbox2), 10);
gtk_container_add (GTK_CONTAINER (frame3), hbox3);
gtk_container_set_border_width (GTK_CONTAINER (hbox3), 10);
gtk_container_add (GTK_CONTAINER (frame4), hbox4);
gtk_container_set_border_width (GTK_CONTAINER (hbox4), 10);
biosView = gtk_text_view_new();
biosbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW (biosView));
gtk_text_buffer_set_text (biosbuf, bios,-1);
gtk_container_add(GTK_CONTAINER(scrolledbios), biosView);
gtk_container_add(GTK_CONTAINER(hbox1),scrolledbios);
cpuView = gtk_text_view_new();
cpubuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW (cpuView));
gtk_text_buffer_set_text (cpubuf, cpu,-1);
gtk_container_add(GTK_CONTAINER(scrolledcpu), cpuView);
gtk_container_add(GTK_CONTAINER(hbox2),scrolledcpu);
memView = gtk_text_view_new();
membuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW (memView));
gtk_text_buffer_set_text (membuf, mem,-1);
gtk_container_add(GTK_CONTAINER(scrolledmem), memView);
gtk_container_add(GTK_CONTAINER(hbox3),scrolledmem);
hddView = gtk_text_view_new();
hddbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW (hddView));
gtk_text_buffer_set_text (hddbuf, hdd,-1);
gtk_container_add(GTK_CONTAINER(scrolledhdd), hddView);
gtk_container_add(GTK_CONTAINER(hbox4),scrolledhdd);
pass_button = gtk_button_new_from_stock ("PASS");
gtk_box_pack_start(GTK_BOX(hbox), pass_button, FALSE, FALSE, 10);
g_signal_connect (pass_button, "clicked", G_CALLBACK (PASS), (gpointer)1);
fail_button = gtk_button_new_from_stock ("FAIL");
gtk_box_pack_start(GTK_BOX(hbox), fail_button, FALSE, FALSE, 10);
g_signal_connect (fail_button, "clicked", G_CALLBACK (FAIL), (gpointer)2);
g_signal_connect_swapped (pass_button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
g_signal_connect_swapped (fail_button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
gtk_widget_show_all (window);
gtk_main ();
return return_i;
}
编译命令:g++ -o Info.exe Info.cpp `pkg-config --cflags --libs gtk+-2.0`
执行结果: