gtkmm-更改窗口的最小大小 [英] Gtkmm - change minimum size of Window

查看:117
本文介绍了gtkmm-更改窗口的最小大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试更改Gtk :: Window的最小大小.应该可以将窗口缩小到比最大容器缩小到特定尺寸小的尺寸.

I try to change the minimum size of an Gtk::Window. It should be possible to shrink the window to a smaller size then the biggest container in it to a specific size.

我尝试了几种您可以在下面看到的方法.没有任何效果.最小值始终由图像大小定义.我做错了什么?

I try several approaches you can see below. Nothing shows any effect. The minimum always defined by the image size. What did I do wrong?

main.cpp

#include "MainWindow.h"
#include <gtkmm.h>

int main (int argc, char* argv[])
{
  Glib::RefPtr<Gtk::Application> app =
    Gtk::Application::create(argc, argv, "org.gtkmm.example");

  MainWindow mainWindow;
  return app->run(mainWindow);
}

MainWindow.h

#ifndef MAINWINDOW_H_INCLUDED
#define MAINWINDOW_H_INCLUDED

#include <gtkmm.h>
#include <gdkmm.h>

class MainWindow : public Gtk::Window
{
  public:
    MainWindow();
  private:
    Gtk::Image   m_Image;
};

#endif // MAINWINDOW_H_INCLUDED

MainWindow.cpp

#include "MainWindow.h"

#define APPROACH_05

MainWindow::MainWindow() :
  m_Image( "image.png" )
{
  this->set_border_width(0);

#ifdef APPROACH_01
  this->add(m_Image);
  m_Image.set_size_request(5,5);
#endif // APPROACH_01

#ifdef APPROACH_02
  this->add(m_Image);
  this->set_size_request(5,5);
#endif // APPROACH_02

#ifdef APPROACH_03
  this->add(m_Image);
  Gtk::Allocation allocation = m_Image.get_allocation();
  allocation.set_width(5);
  allocation.set_height(5);
  m_Image.set_allocation(allocation);
#endif // APPROACH_03

#ifdef APPROACH_04
  this->add(m_Image);
  Gtk::Allocation allocation = this->get_allocation();
  allocation.set_width(5);
  allocation.set_height(5);
  this->set_allocation(allocation);
#endif // APPROACH_04

#ifdef APPROACH_05
  this->add(m_Image);
  Gdk::Geometry geom = {
    .min_width   = 5,
    .min_height  = 5,
  };
  Gtk::Window::set_geometry_hints(*this,geom,Gdk::HINT_MIN_SIZE);
#endif // APPROACH_05

  this->show_all_children();
}

编译为:

g++ main.cpp MainWindow.cpp `pkg-config gtkmm-3.0 --cflags --libs` -o prog


@ptomato感谢您的回复.我已经尝试过这种方式:


@ptomato Thanks for your response. I have tried it this way:

#ifdef APPROACH_06
  this->add(m_ScrolledWindow);
  m_ScrolledWindow.set_border_width(0);
  m_ScrolledWindow.set_policy(Gtk::POLICY_NEVER,Gtk::POLICY_ALWAYS);
  m_ScrolledWindow.add(m_Image);
#endif // APPROACH_06

现在我可以垂直调整窗口的大小,但是可以看到垂直滚动条.如果像水平轴一样将策略设置为 POLICY_NEVER ,则窗口宽度将限制为图像宽度.另外,滑块的尺寸也限制了高度.

Now I can resize the window vertically, but I see the vertical scrollbar. If I set the policy to POLICY_NEVER like in the horizontal axis the window width is limited to the image width. Additionally the size of the slider limits the height too.

推荐答案

如果您希望将窗口缩小到小于其内部图像的大小,则需要将该图像放在 Gtk :: ScrolledWindow ,然后将其添加到窗口中.如果不进行滚动,那么当您将窗口缩小为小于图像时,图像将不知道要渲染图像的哪个部分.

If you want to be able to shrink the window down to a smaller size than the image inside it, then you need to put the image inside of a Gtk::ScrolledWindow before adding it to the window. Without scrolling, then the image wouldn't know what part of itself to render when you made the window smaller than the image.

这篇关于gtkmm-更改窗口的最小大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆