Draw over screen with Quartz

2019-06-07 12:30发布

问题:

I'm trying to work out what the best way to draw over the top of all other items on the screen on OS X. I don't want to impede the user's ability to interact with their applications, but want to 'annotate' them. I want to be able to draw up to 20 different annotations. The top half of this screenshot from Gizmodo happens to nicely show the kind of thing I want to do. http://gizmodo.com/assets/resources/2006/07/04%20Safari.jpg (sorry, I'm too new to post it as an image)

The questions I think I need to answer are:

  1. Should I create a single window for each drawing and draw to that? If so, how do I minimise overhead?
  2. What kind of window or other context should I use given that I don't want any window decoration?

I don't think I want the overhead of creating 20 windows, but I also don't know that I want to create a full-screen, invisible window that contains my context (I presume a subclassed NSView), because I fear that will a) cause problems interacting with what's below and b) break the niceties of only redrawing when necessary (my actual drawing will likely only cover 10% of the screen)

I've not worked with Quartz2d before, so I just can't get my head around how to get the 'right' context to draw on from the documentation. Any help would be appreciated.

Thanks,

Who

回答1:

You can only draw into a window. You can make a transparent, borderless window but it will need to be at the front.

You could set it's level to something like NSPopUpMenuWindowLevel to make it draw above other windows (this will be very annoying to users) but clicking on it will:

a) activate and bring to the front your app

b) Prevent the app underneath receiving mouse events

Is that what you want?