Mac changing the background color of a Window in an application

vnraju

macrumors newbie
Original poster
Jun 27, 2007
25
0
hi all,
is it possible to change the background color of a window?
if it is possible how it is?
any suggestion is appreciated..
 

Eraserhead

macrumors G4
Nov 3, 2005
11,124
10,447
UK
You can change the background of a view by making a custom subclass of NSView and implementing this method:

Code:
- (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

macrumors 603
Jan 6, 2004
5,858
1,726
Western US
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

macrumors newbie
Original poster
Jun 27, 2007
25
0
yaaa its my own app...
im trying to change using the code
[myWindow setbackgroungColor:[NSColor blackColor]];
but it....doesn't work....[/COLOR]
 

HiRez

macrumors 603
Jan 6, 2004
5,858
1,726
Western US
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

macrumors newbie
Original poster
Jun 27, 2007
25
0
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.....
 

kainjow

Moderator emeritus
Jun 15, 2000
7,745
3
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

macrumors 68000
Jul 19, 2002
1,621
6
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.