当前位置: 首页 > 工具软件 > GDK > 使用案例 >

GDK获得各种scale factor

杨乐意
2023-12-01

这些factor跟设置中的并无关系,而且都是整数,感觉象是放大镜的作用。

  • 源码
 
#include <gtk/gtk.h>
#include <gdk/gdk.h>

#define  FIRST_LABEL "first label"
 
int main(int argc, char *argv[]) 
{ 
	gtk_init(NULL, NULL);
 
	GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);

	GtkWidget *vbox = gtk_vbox_new(TRUE, 10);
	gtk_container_add(GTK_CONTAINER(window), vbox);

	GtkWidget *label_one = gtk_label_new(FIRST_LABEL);
	gtk_container_add(GTK_CONTAINER(vbox), label_one);

	gtk_widget_show_all(window);

	int scale_factor = 0;
	scale_factor = gtk_widget_get_scale_factor (GTK_WIDGET (window));
	printf("gtk_widget_get_scale_factor=%d\n", scale_factor);

	GdkDisplay* display = gdk_display_get_default();
	printf("display=0x%08X\n", display);

	GdkScreen* screen = gdk_screen_get_default();
	printf("screen=0x%08X\n", screen);
	scale_factor = gdk_screen_get_monitor_scale_factor(screen, 0);
	printf("gdk_screen_get_monitor_scale_factor=%d\n", scale_factor);

	GdkMonitor* monitor = gdk_display_get_primary_monitor(display);
	printf("monitor=0x%08X\n", monitor);
	scale_factor = gdk_monitor_get_scale_factor(monitor);
	printf("gdk_monitor_get_scale_factor=%d\n", scale_factor);

	gtk_main();
 
    return 0; 
} 
  • 编译
#!/bin/bash

COMPILE_INCLUDE=${COMPILE_INCLUDE}" -I/usr/include/gtk-3.0"

OUTPUT_FILE=test
if [ -f ${OUTPUT_FILE} ]; then
    rm ${OUTPUT_FILE}
fi

echo ${COMPILE_INCLUDE_LINK}

gcc \
    -o ${OUTPUT_FILE} \
    test.c \
    ${COMPILE_INCLUDE} \
    ${COMPILE_LINK} \
    `pkg-config --cflags --libs gtk+-3.0 gtk+-x11-3.0 gdk-3.0 gdk-x11-3.0`

 类似资料: