按下按钮时,移动鼠标,不要拖动窗口。
按下按钮移动时,不要移动窗口
按钮添加一个类ButtonStyle。按下鼠标改变背景图像。
-(void)mouseDown:(NSEvent *)theEvent{
[self setImage:[NSImage imageNamed:@"closeDown.png"]];
}
这是拖动窗口的代码
- (void)mouseDown:(NSEvent *)theEvent
{
NSRect windowFrame = [self frame];
initialLocation = [NSEvent mouseLocation];
initialLocation.x -= windowFrame.origin.x;
initialLocation.y -= windowFrame.origin.y;
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint currentLocation;
NSPoint newOrigin;
NSRect screenFrame = [[NSScreen mainScreen] frame];
NSRect windowFrame = [self frame];
currentLocation = [NSEvent mouseLocation];
newOrigin.x = currentLocation.x - initialLocation.x;
newOrigin.y = currentLocation.y - initialLocation.y;
if( (newOrigin.y+windowFrame.size.height) > (screenFrame.origin.y+screenFrame.size.height) ){
newOrigin.y=screenFrame.origin.y + (screenFrame.size.height-windowFrame.size.height);
}
[self setFrameOrigin:newOrigin];
}