将NSProgressIndicator添加到dock图标 [英] Adding an NSProgressIndicator to the dock icon

查看:180
本文介绍了将NSProgressIndicator添加到dock图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,应在dock图标中显示一个进度条。目前我有这个,但它不工作:

I'm creating an application which should show a progress bar in the dock icon. Currently I have this, but it's not working:

  NSProgressIndicator *progressIndicator = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0.0f, 0.0f, 10.0f, 20.0f)];
  [progressIndicator setStyle:NSProgressIndicatorBarStyle];
  [progressIndicator setIndeterminate:NO];
  [[[[NSApplication sharedApplication] dockTile] contentView] addSubview:progressIndicator];
  [progressIndicator release];

或者我必须自己在码头上画画吗?谁能帮助我?感谢。

Or must I draw it on the dock myself? Can anyone help me? Thanks.

推荐答案

在完成中,我不得不使用下面的代码,因为contentView为null:

In the finish I had to use the following code as the contentView was null:

    docTile = [[NSApplication sharedApplication] dockTile];
    NSImageView *iv = [[NSImageView alloc] init];
    [iv setImage:[[NSApplication sharedApplication] applicationIconImage]];
    [docTile setContentView:iv];

    progressIndicator = [[NSProgressIndicator alloc]
                                              initWithFrame:NSMakeRect(0.0f, 0.0f, docTile.size.width, 10.)];
    [progressIndicator setStyle:NSProgressIndicatorBarStyle];
    [progressIndicator setIndeterminate:NO];
    [iv addSubview:progressIndicator];

    [progressIndicator setBezeled:YES];
    [progressIndicator setMinValue:0];
    [progressIndicator setMaxValue:1];
    [progressIndicator release];

    [self setProgress:[NSNumber numberWithFloat:-1]];
}

- (void)setProgress:(NSNumber *)fraction {
    if ( [fraction doubleValue] >= 0 ) {
        [progressIndicator setDoubleValue:[fraction doubleValue]];
        [progressIndicator setHidden:NO];
    }
    else
        [progressIndicator setHidden:YES];
    [docTile display];
}

这篇关于将NSProgressIndicator添加到dock图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆