Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5991785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:26:15+00:00 2026-05-22T23:26:15+00:00

I am making an app that is a standalone menu item and the basis

  • 0

I am making an app that is a standalone menu item and the basis for the code is sample code I found on a website. The sample code uses a number as the menu icon, but I want to change it to an image.

I want it to be like other apps where it shows icon.png when not clicked and icon-active.png when clicked.

The current code is this:

- (void)drawRect:(NSRect)rect {
// Draw background if appropriate.
if (clicked) {
    [[NSColor selectedMenuItemColor] set];
    NSRectFill(rect);
}

// Draw some text, just to show how it's done.
NSString *text = @"3"; // whatever you want

NSColor *textColor = [NSColor controlTextColor];
if (clicked) {
    textColor = [NSColor selectedMenuItemTextColor];
}

NSFont *msgFont = [NSFont menuBarFontOfSize:15.0];
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
[paraStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
[paraStyle setAlignment:NSCenterTextAlignment];
[paraStyle setLineBreakMode:NSLineBreakByTruncatingTail];
NSMutableDictionary *msgAttrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 msgFont, NSFontAttributeName,
                                 textColor, NSForegroundColorAttributeName,
                                 paraStyle, NSParagraphStyleAttributeName,
                                 nil];
[paraStyle release];

NSSize msgSize = [text sizeWithAttributes:msgAttrs];
NSRect msgRect = NSMakeRect(0, 0, msgSize.width, msgSize.height);
msgRect.origin.x = ([self frame].size.width - msgSize.width) / 2.0;
msgRect.origin.y = ([self frame].size.height - msgSize.height) / 2.0;

[text drawInRect:msgRect withAttributes:msgAttrs];
}

Also, I found a post describing a method on how to do this, but it did not work for me. The url to that is this: http://mattgemmell.com/2008/03/04/using-maattachedwindow-with-an-nsstatusitem/comment-page-1#comment-46501.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:26:16+00:00Added an answer on May 22, 2026 at 11:26 pm

    Use an NSImage and draw it where desired. For example:

    NSString *name = clicked? @"icon-active" : @"icon";
    NSImage *image = [NSImage imageNamed:name];
    NSPoint p = [self bounds].origin;
    [image drawAtPoint:p fromRect:NSZeroRect
             operation:NSCompositeSourceOver fraction:1.0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an app that accesses Amazon S3. But should I put the
I am making an app that uses a lot of fairly big image files.
I am making an app that uses a class I will call Player the
I'm making android app that tells you the operator's name of your contact's number.
I am making an iphone app that uses two fingers to place and scale
So I'm making an Android 2.2 app that uses GLSurfaceView. My question is, since
I am making an app that scrolls information on the desktop. The window is
I'm making another app's window topmost to ensure that a click in my app
I'm making a WinForms app with a ListView set to detail so that several
I'm writing an app for WM that handles incoming SMS events. I tried making

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.