Gtk无法全屏或无屏幕检测到窗口

时间:2017-08-11 11:46:13

标签: c gtk

这是我项目的一部分......我的全屏功能有问题:

#include "header.h"

gboolean  my_gtk_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
  GdkWindow *window = gtk_widget_get_window(widget);

  GdkWindowState state = gdk_window_get_state(GDK_WINDOW(window));

  switch(event->keyval) {
  case GDK_KEY_F11:
    if(state == GDK_WINDOW_STATE_FULLSCREEN) {
      gtk_window_unfullscreen(GTK_WINDOW(widget));
    }
    else {
      gtk_window_fullscreen(GTK_WINDOW(widget));
    }

    printf("win_state %d\n", state);
    printf("gdk_state %d\n", GDK_WINDOW_STATE_FULLSCREEN);

    break;
  case GDK_KEY_Escape:
    destroy(NULL, NULL);
  break;
  }
}

int main(int argc, char **argv)
{
  GtkWidget *win;
  char *s = "In progress";

  gtk_init(&argc, &argv);

  win = win_principal(win, s, 600, 400, 10);

  g_signal_connect(G_OBJECT(win), "key-press-event", G_CALLBACK(my_gtk_key_press_event), NULL);

  g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(destroy), NULL);

  gtk_widget_show_all(win);

  gtk_main();

  return 0;
}

当处于未完成屏幕时,它会打印128,而在全屏显示它会打印144 并且GDK_WINDOW_STATE_FULLSCREEN的值等于16 ... 我怎么解决这个问题?或者有什么问题?

1 个答案:

答案 0 :(得分:1)

GDK_WINDOW_STATE_FULLSCREEN是一个位标志,因此,您无法直接比较状态,而是执行按位操作。

变化:

if (state == GDK_WINDOW_STATE_FULLSCREEN)

要:

if (state & GDK_WINDOW_STATE_FULLSCREEN)

这是您的代码,没有依赖项:

#include <gtk/gtk.h>

gboolean  my_gtk_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
  GdkWindow *window = gtk_widget_get_window(widget);

  GdkWindowState state = gdk_window_get_state(GDK_WINDOW(window));

  switch(event->keyval) {
  case GDK_KEY_F11:
    if (state & GDK_WINDOW_STATE_FULLSCREEN) {
      gtk_window_unfullscreen(GTK_WINDOW(widget));
    }
    else {
      gtk_window_fullscreen(GTK_WINDOW(widget));
    }
    break;
  case GDK_KEY_Escape:
    gtk_main_quit ();
  break;
  }
}

int main(int argc, char **argv)
{
  GtkWidget *win;
  char *s = "In progress";

  gtk_init(&argc, &argv);

  //win = win_principal(win, s, 600, 400, 10);
  win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW(win), 640, 400);

  g_signal_connect(G_OBJECT(win), "key-press-event", G_CALLBACK(my_gtk_key_press_event), win);

  g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(win);

  gtk_main();

  return 0;
}

编译:

gcc -o window window.c `pkg-config --cflags --libs gtk+-3.0`

窗口将使用F11切换全屏,然后使用Esc。

关闭