0

我试图在桌面上的不同位置多次显示图像,但我不知道该怎么做。现在我正在尝试 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
4

1 回答 1

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
于 2013-05-09T13:03:51.243 回答