PDA

View Full Version : changing the background color of a Window in an application




vnraju
Aug 10, 2007, 08:57 AM
hi all,
is it possible to change the background color of a window?
if it is possible how it is?
any suggestion is appreciated..



pianoman
Aug 10, 2007, 10:12 AM
EDIT: since it's his own code, this post was irrelevant.

Eraserhead
Aug 10, 2007, 11:19 AM
You can change the background of a view by making a custom subclass of NSView and implementing this method:


- (void)drawRect:(NSRect)rect {
// Drawing code here.
NSColor *white=[NSColor whiteColor];
[white set];
NSRectFill([self bounds]);
}


This code just makes the view white, but another colour is easy to achieve by changing "whiteColor" to the color of your choosing.

HiRez
Aug 10, 2007, 05:04 PM
You never said whether you meant your OWN application, or ANY application. And if it's your own, what are you using to code it?

vnraju
Aug 10, 2007, 11:57 PM
yaaa its my own app...
im trying to change using the code
[myWindow setbackgroungColor:[NSColor blackColor]];
but it....doesn't work....[/COLOR]

HiRez
Aug 11, 2007, 02:26 AM
1. Do you get an error, or just nothing happens?

2. Can you post the actual code you're using to do it? In the example you give, the call is misspelled (but probably just a typo here?)

3. Are you sure that myWindow is a valid, initialized window? For example, is it connected to a window in IB? Are you trying to call setBackgroundColor: in a method that gets called before awakeFromNib:? If you are, the window might be invalid or not receive messages yet.

vnraju
Aug 11, 2007, 03:14 AM
1. Do you get an error, or just nothing happens?

2. Can you post the actual code you're using to do it? In the example you give, the call is misspelled (but probably just a typo here?)

3. Are you sure that myWindow is a valid, initialized window? For example, is it connected to a window in IB? Are you trying to call setBackgroundColor: in a method that gets called before awakeFromNib:? If you are, the window might be invalid or not receive messages yet.

no it doesn't raise any error..
i made a mistake in posting...to this forum...

and that method will be called after te awakefromnib method.....

Eraserhead
Aug 11, 2007, 03:45 AM
no it doesn't raise any error..
i made a mistake in posting...to this forum...

See my post (#2) for how to do it.

vnraju
Aug 11, 2007, 07:44 AM
See my post (#2) for how to do it.

i didn;t found............dat......

kainjow
Aug 12, 2007, 12:39 AM
It looks like the problem is related to HiRez's #3 item. You could try NSLog-ing your myWindow variable on awakeFromNib, and see if it shows up as a valid object. If not, then there's something wrong with your nib.

Fukui
Aug 12, 2007, 07:32 PM
yaaa its my own app...
im trying to change using the code
[myWindow setbackgroungColor:[NSColor blackColor]];
but it....doesn't work....[/COLOR]

Change your window style to textured. Does that work now?
Also, make sure to tell the [window display] after setting the color.