gtk3开发
依赖环境
# 依赖 gtk3-devel
# libgtk-3-dev
cd build
cmake ..
make
ldd ./main |grep gtk
# libgtk-3.so.0 => /lib64/libgtk-3.so.0 (0x00007fef71c96000)
# 或者
gcc `pkg-config --cflags gtk+-3.0` -o hello main.c `pkg-config --libs gtk+-3.0`
# 支持高分屏
GDK_SCALE=2 GDK_DPI_SCALE=0.5 ./main
CMakeLists.txt
# Set the name and the supported language of the project
project(main-world C)
# Set the minimum version of cmake required to build this project
cmake_minimum_required(VERSION 2.8)
# Use the package PkgConfig to detect GTK+ headers/library files
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
# Add other flags to the compiler
add_definitions(${GTK3_CFLAGS_OTHER})
# Add an executable compiled from main.c
add_executable(main main.c)
# Link the target to the GTK+ libraries
target_link_libraries(main ${GTK3_LIBRARIES})
# 安装
install(FILES main DESTINATION /app/bin/)
hello bar
#include <gtk/gtk.h>
static void print_hello(GtkWidget *widget, gpointer data)
{
g_print("Hello World\n");
}
// 执行
void run_exec()
{
// xrandr --output VNC-0 --brightness 0.5
const gchar *command = "ls -l"; // 要执行的外部命令
// 执行外部命令
gchar *output = NULL;
GError *error = NULL;
gboolean result = g_spawn_command_line_sync(command, &output, NULL, NULL, &error);
if (result)
{
g_print("命令执行成功!\n");
g_print("输出结果:\n");
g_print("%s\n", output);
g_free(output);
}
else
{
g_print("命令执行失败:%s\n", error->message);
g_error_free(error);
}
}
// 滑动条的回调函数
static void on_slider_changed(GtkRange *range, gpointer user_data)
{
gdouble value = gtk_range_get_value(range);
g_print("滑动条的值:%f\n", value);
run_exec();
}
static void activate(GtkApplication *app, gpointer user_data)
{
GtkWidget *window;
GtkWidget *button;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Hello World ! Example");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 20);
// ! 创建滑动条
GtkWidget *slider = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, 0, 100, 1);
gtk_scale_set_value_pos(GTK_SCALE(slider), GTK_POS_TOP);
gtk_widget_set_hexpand(slider, TRUE);
gtk_scale_set_draw_value(GTK_SCALE(slider), TRUE);
// 连接滑动条的"value-changed"信号到回调函数
g_signal_connect(slider, "value-changed", G_CALLBACK(on_slider_changed), NULL);
// 将滑动条添加到主窗口
gtk_container_add(GTK_CONTAINER(window), slider);
gtk_widget_show_all(window);
}
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}