问题
Trying to add a background to a curses window. I have these two properties
I've found these two methods:
bkgd(ch)
and
bkgdset(ch)
However, I can't implement them:
win1.new(10,10,10,10)
win1.box('|','-')
win1.bkg(COLOR_RED)
This fills win1 with a load of diamonds! Very insteresting effect but not what I wanted. I want a red background.
Colors work perfectly in my terminal.
回答1:
It's been a while, but maybe my examples are still useful:
It is the same "diamonds" for me when using
window.bkgd(COLOR_RED)
This seems to appear, because the bkgd
method takes a char and prints it to all free spaces of the window (see old doc).
However, then you can use a color pair with the wanted background color and apply it to all screen positions before writing oher stuff.
Here is how I solved it:
require 'curses'
init_screen
start_color
init_pair(COLOR_RED, COLOR_WHITE, COLOR_RED)
window = Curses::Window.new(0, 0, 0, 0)
window.attron(color_pair(COLOR_RED)) do
lines.times do |line|
window.setpos(line, 0)
window << ' ' * cols
end
end
来源:https://stackoverflow.com/questions/19438195/set-a-windows-background-color-in-ruby-curses