The tale about windows and their appearances

A lot of users want to buy products with unusual interface. It may contains some specific widgets, but you can surprise many users if you'll develope your custom window. For instance, window without toolbar, or which has interesting form ( not rectangle ).

I' tell you how create the following window:
1) The first step, which you should do is creating new Cocoa application project in Xcode. This project is created with one window. The name of my project is "Custom Window"


2) Now we should create to classes: CustomView and MainWindow. CustomWindow is inherited from NSView and MainWindow from NSWindow, indeed.CustomView class override only one method from NSView

- ( void ) drawRect: ( NSRect )rect;In this method you can create different forms for your future content. By the way, in this tutorial we draw simple figure with NSBezierPath class. For it, we create method in CustomView class
- ( NSBezierPath * ) RoundedRect: ( NSRect ) rect withRadius: ( float ) radius
{
NSBezierPath *path = [ NSBezierPath bezierPath ];[ path moveToPoint: NSMakePoint( rect.origin.x + radius + 1.0, rect.origin.y ) ];
[ path appendBezierPathWithArcFromPoint: NSMakePoint( rect.origin.x, rect.origin.y )
toPoint: NSMakePoint( rect.origin.x, rect.origin.y + radius ) radius: radius * 1.25 ];
[ path lineToPoint: NSMakePoint( rect.origin.x, rect.origin.y + rect.size.height - radius - 2.0 ) ];

[ path appendBezierPathWithArcFromPoint: NSMakePoint( rect.origin.x, rect.origin.y + rect.size.height )
toPoint: NSMakePoint( rect.origin.x + radius, rect.origin.y + rect.size.height )
radius: radius * 1.25 ];
[ path lineToPoint: NSMakePoint( rect.origin.x + rect.size.width - radius - 2.0, rect.origin.y + rect.size.height ) ];
[ path appendBezierPathWithArcFromPoint: NSMakePoint( rect.origin.x + rect.size.width, rect.origin.y + rect.size.height )
toPoint: NSMakePoint( rect.origin.x + rect.size.width, rect.origin.y + rect.size.height - radius )
radius: radius * 1.25 ];

[ path lineToPoint: NSMakePoint( rect.origin.x + rect.size.width, rect.origin.y + radius + 2.0 ) ];

[ path appendBezierPathWithArcFromPoint: NSMakePoint( rect.origin.x + rect.size.width, rect.origin.y ) toPoint: NSMakePoint( rect.origin.x + rect.size.width - radius, rect.origin.y )
radius: radius * 1.25 ];

[ path closePath ];

return path;
}
And now our overriding method drawRect will be

- ( void ) drawRect: ( NSRect )rect{

[ [ NSColor colorWithDeviceRed: 1 green: 1 blue: 1 alpha: 0.9 ] setFill ];

[[ RoundedRect: NSMakeRect( 0, 0, 313, 282 ) withRadius: 10 ] fill ];
}
3) So, we made our new custom view for Window. by the way, it's neccessary to hide standard window frame of our Window. For this we create MainWindow class and override some methods from NSWindow class ( which is superclass for our MainWindow )

- ( id ) initWithContentRect: ( NSRect) contentRect
styleMask: ( unsigned int ) aStyle
backing: ( NSBackingStoreType ) bufferingType
defer: ( BOOL ) flag{ self = [ super initWithContentRect: contentRect styleMask: aStyle & ( ~NSTitledWindowMask )
backing: bufferingType
defer: flag ];

if( self == nil )
return nil;
[ self setHasShadow: YES ];
[ self setMovableByWindowBackground: YES ]; [ self setBackgroundColor: [ NSColor colorWithDeviceRed: 1 green: 1 blue: 1 alpha: 0.0 ] ];
[ self setOpaque: NO ];

return self;
}
- ( id ) initWithContentRect: ( NSRect ) contentRect styleMask: ( unsigned int ) aStyle
backing: ( NSBackingStoreType ) bufferingType
defer: ( BOOL ) flag screen: ( NSScreen* ) screen
{
self = [ super initWithContentRect: contentRect styleMask: aStyle & ( ~NSTitledWindowMask )
backing: bufferingType
defer: flag
screen: screen ];
if( self == nil ) return nil;
[ self setHasShadow: YES ];
[ self setMovableByWindowBackground: YES ];

[ self setBackgroundColor: [ NSColor colorWithDeviceRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.0 ] ];
[ self setOpaque: NO ];
return self;}
- ( BOOL ) canBecomeKeyWindow
{
return YES;}- ( BOOL ) canBecomeMainWindow
{
return YES;
}

4) Our classes is ready. and now we should work with nib file. Double click on MainMenu.nib, and it will be opened in Interface Builder application. In which we should define class indentity for Window ( MainWindow class ) and it's ContentView ( CustomView class ).


5) Finally, don't forget create AppController class!!

THE END

Комментариев нет:

Отправить комментарий