0

按下按钮时,移动鼠标,不要拖动窗口。

按下按钮移动时,不要移动窗口

代码下载。http://code.google.com/p/kacperwangbuttontab/downloads/detail?name=buttonTab.zip&can=2&q=#makechanges

按钮添加一个类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];
}

在此处输入图像描述

4

1 回答 1

0

我仍然不确定您想要什么,但是如果您希望在单击并拖动按钮时不拖动窗口,请在 ButtonStyle 中的 mouseDown 方法中添加一行 [super mouseDown:theEvent];班级似乎做到了。

-(void)mouseDown:(NSEvent *)theEvent{
    switch (self.tag) {
        case 2:
            [self setImage:[NSImage imageNamed:@"closeDown.png"]];
            [super mouseDown:theEvent];
            break;
        default:
            break;
    }
}
于 2012-05-06T04:59:01.510 回答