【问题标题】:cocoa:When the button is pressed,Move the mouse,Do not drag the windowcocoa:按下按钮时,移动鼠标,不要拖动窗口
【发布时间】:2024-01-10 18:43:02
【问题描述】:

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

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

代码下载。 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];
}

【问题讨论】:

  • 点击按钮移动窗口不动。我移动窗口。
  • 只有当 mouseDown 在按钮上时才移动窗口?或者当鼠标在窗口中的任意位置时?
  • 代码下载。当按钮移动时,不要移动窗口
  • 如果您有可以为您翻译的朋友,请这样做,因为您当前形式的帖子是零意义的。
  • 对不起,我不想在按下按钮移动时拖动窗口,ButtonTab示例,我更新了。我们该怎么办。明白了吗?

标签: objective-c macos cocoa osx-lion


【解决方案1】:

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

-(void)mouseDown:(NSEvent *)theEvent{
    switch (self.tag) {
        case 2:
            [self setImage:[NSImage imageNamed:@"closeDown.png"]];
            [super mouseDown:theEvent];
            break;
        default:
            break;
    }
}

【讨论】:

  • 按按钮将无法设置图片。
  • 当我按下按钮它确实改变了图片
  • 你设置了背景,你会发现按钮没有改变背景。我下载了更新的文件,设置背景给你测试。
  • 我们在这里谈论的是哪个应用程序? ButtonHover 还是 ButtonTab?上次下载我得到了ButtonHover,当前链接下载的是ButtonTab。
  • 对不起,我不能再帮忙了,因为我听不懂你的英语。