在桌面上显示形状窗口/图像的多个实例?

时间:2013-05-07 19:54:26

标签: ruby wxruby

我正试图在不同位置的桌面上多次显示图像,但我不知道如何去做。现在我正在尝试Wxruby,但我想知道是否还有另一种方法可以做到这一点我不知道。

到目前为止,我能够在一个位置显示一个图像,wxruby几乎来自其中一个样本:

require 'wx'
include Wx

class Warning < Wx::App
  include Wx
  def on_init(x = 300, y = 300)
    @frame = Frame.new( nil, -1, "Application", Point.new(x,y), Size.new(50,50), FRAME_SHAPED|SIMPLE_BORDER|FRAME_NO_TASKBAR|STAY_ON_TOP)
    @frame.show
    @has_shape = false
    @delta = [0,0]

    evt_paint {on_paint}

    shape = File.join( 'pathToImage' )
    @bmp = Bitmap.new( Image.new(shape) )
    @frame.set_client_size(@bmp.width, @bmp.height)

    if PLATFORM == 'WXGTK'
      # wxGTK requires that the window be created before you can
      # set its shape, so delay the call to SetWindowShape until
      # this event.
      evt_window_create { set_window_shape }
    else
      # On wxMSW and wxMac the window has already been created, so go for it.
      set_window_shape
    end

    #@frame.paint { | dc | dc.draw_bitmap(@bmp, 0, 0, true) }
  end

  def set_window_shape
    r = Region.new(@bmp)
    @has_shape = @frame.set_shape(r)
  end

  def on_paint
    @frame.paint { | dc | dc.draw_bitmap(@bmp, 0, 0, true) }
  end
end

app = Warning.new
app.main_loop

1 个答案:

答案 0 :(得分:0)

我终于明白了! 这个ruby脚本使用wx gem来显示整形窗口并使用一些窗口东西来获得当前的屏幕分辨率(我假设主监视器)。

它获得分辨率,然后是要在桌面上显示的图像的宽度/高度。关于图像的一个注意事项,似乎当wx显示图像时(我不知道它是否仅适用于成形窗口)图像的任何部分是纯黑色都不显示,而是你看到窗口后面的内容

希望以后拯救某人有些麻烦,或者如果不是哦。

require 'wx'
require 'dl/import'
require 'dl/struct'
include Wx

class Warning < Wx::Frame
  include Wx
    @@x = 0
    @@y = 0
  def setXY(x = 0, y = 0)
    @@x = x
    @@y = y
  end
  def initialize(parent, bmp)
    super(nil, -1, '', pos = [@@x,@@y], size = DEFAULT_SIZE, style = FRAME_SHAPED|SIMPLE_BORDER|FRAME_NO_TASKBAR|STAY_ON_TOP)
    @has_shape = false
    @delta = [0,0]
    @bmp = bmp

    evt_paint {on_paint}
    self.set_client_size(@bmp.width, @bmp.height)

    if PLATFORM == 'WXGTK'
      # wxGTK requires that the window be created before you can
      # set its shape, so delay the call to SetWindowShape until
      # this event.
      evt_window_create { set_window_shape }
    else
      # On wxMSW and wxMac the window has already been created, so go for it.
      set_window_shape
    end
    self.paint { | dc | dc.draw_bitmap(@bmp, 0, 0, true) }
  end

  def set_window_shape
    r = Region.new(@bmp)
    @has_shape = self.set_shape(r)
  end

  def on_paint
    self.paint { | dc | dc.draw_bitmap(@bmp, 0, 0, true) }
  end
end # of Warning

class WarnTest < Wx::App
  def on_init
    @frame = Frame.new()
    warnings = []
    resolution = getScreenResolution()
    shape = File.join( 'image you want' )
    @bmp = Bitmap.new( Image.new(shape) )
    imageWidth = @bmp.get_width
    imageHeight = @bmp.get_height

    imagesAcross = resolution[0] / imageWidth
    imagesDown = resolution[1] / imageHeight

    j = 0
    (1..imagesDown).each do |y|
      i = 0
      (1..imagesAcross).each do |x|
        warnings << [(imageWidth * i), (imageHeight * j)]
        i = i + 1
      end
      j = j + 1
    end

    warnings << [resolution[0], resolution[1]]

    # Create a new window for each xy in warnings array
    warnings.each do |x|
      win = Warning.new(self, @bmp)
      win.setXY(x[0], x[1])
      win.show(true)
      win.update
      sleep 0.01
    end
  end # of on_init

  def getScreenResolution
    sM_CXSCREEN =   0
    sM_CYSCREEN =   1
    user32 = DL.dlopen("user32")

    get_system_metrics = user32['GetSystemMetrics', 'ILI']
    x, tmp = get_system_metrics.call(sM_CXSCREEN,0)
    y, tmp = get_system_metrics.call(sM_CYSCREEN,0)
    res = [x, y]
    return res
  end # of getScreenResolution
end # of WarnTest

app = WarnTest.new
app.main_loop