CGCaptureAllDisplaysWithOptions(kCGCaptureNoFill) still fills the screens with black - osx-mavericks

I'm programming a cocoa app which uses scripting bridge to interact with menus in a specific frontmost application, but I don't want the user to see all the gibberish that scripting bridge is doing.
So I want to freeze the image of all monitors during that phase and I'm trying to use
CGCaptureAllDisplaysWithOptions(kCGCaptureNoFill);
which supposedly do that until i call the
CGReleaseAllDisplays();
But it won't, all the screens turn black.
I have tried the solution here "CGDisplayCaptureWithOptions (kCGDirectMainDisplay, kCGCaptureNoFill) still fills the screen with black" to capture the screen one by one but stills they turn black.
Someone know why? Is there some workaround?
I also tried a more complex approach, taking screenshots of every screen before capture, using CGDisplayCreateImage. But another problem arouse when i try to draw them directly to the screen context. I got the context using CGDisplayGetDrawingContext and call the CGContextDrawImage to draw the images for each one of the screens.
With only one or mirrored screens works fine but with extended screens only works on my main screen, the other show a sub image of the screenshot.
For example:
Main Screen:
CGDisplayBounds returns {{0, 0}, {1280, 800}}
CGRect rect1 = {{0, 0}, {CGImageGetWidth(screenshot1), CGImageGetHeight(screenshot1)}};
CGContextDrawImage(context1, rect1, screenshot1); //<- works fine
Other Screen
CGDisplayBounds returns {{1280, 0}, {1920, 1080}}
CGRect rect2 = {{0, 0}, {CGImageGetWidth(screenshot2), CGImageGetHeight(screenshot2)}};
CGContextDrawImage(context2, rect2, screenshot2); //<-Shows a sub image of my screenshot with origin at (1280, 0) and frame {{1280, 0}, {1280 - 1920, 1080}}
Sub image that appears on the monitor:
http://i.stack.imgur.com/zoUWe.jpg
Original Screenshot:
http://i.stack.imgur.com/KHjN6.jpg
Any suggestions?

Related

Maximize Window to Have Client Area Just Fill Screen Issue (Windows 10, Win32 DLL)

First, if this isn't a great way to do this, PLEASE suggest better. I can't seem to find an exact solution to what must be a common desire (see post title).
My approach was to trap the WM_PAINT message and check whether the window position is maximized. If it is, then run this code:
SetWindowLong(hCDGWnd, GWL_STYLE, 0); // remove all styling
SetWindowPos(hCDGWnd, HWND_TOP, 0, 0, 1280, 720, SWP_SHOWWINDOW); // full screen is 1280 x 720
StretchDIBits(hdc, 0, 0, 1280, 720, CDG_XOFFSET, CDG_YOFFSET, CDG_RENDER_WIDTH, CDG_RENDER_HEIGHT,
bmBits, (LPBITMAPINFO)&bmInfo, DIB_RGB_COLORS, SRCCOPY);
What I want to see is my BITMAP stretched to fill the entire client area, which should just fill the entire screen. Actually it is a section taken out of a larger bitmap. It seems like the window is in fact full screen, but the bitmap is its normal size and not stretched. Curiously, when I was fiddling with the window positioning stuff I had all kinds of attempts leaving the styling but trying to position the title bar and frame offscreen (see below) - the bitmap was appearing stretched just fine during those close but failed attempts. Now I've got the window right, suddenly the bitmap no longer stretches. Is there something about removing the styling that would screw up the StretchDIBits function?
Also, when I attempt leaving the style in place, and use AdjustWindowRect() to have my client size be fullscreen, it returns {-3, -26, 1283, 723 } which makes sense - 3 pixel border plus 23 more for title bar on top. But, just to explore things, when I don't even test for maximized state, and just make the window have x = -3, y = -26, cx = 1286, cy = 749, then almost everything is fine except the bottom of the window is shy of fullscreen by about 4 pixels. When I make the window height much bigger - say 760 - IT STAYS THE SAME HEIGHT!? I so confused. If try this maneuver only when maximized, it seems like windows ignores my attempts to have the title bar off the top of the screen.

Flicker-free expansion (resize) of a window to the left

Say you have a form that you can expand to the left to show additional controls:
Collapsed:
Expanded:
The simplest way to achieve this in Delphi is to use alRight as the primary anchor for all controls (instead of alLeft) and then simply adjust the width and X coordinate of the form. Either you can set the Width and Left properties individually, or you can use a function that sets them simultaneously, like
if FCollapsed then
SetWindowPos(Handle, 0, Left - Width, Top, 2 * Width, Height, 0)
else
SetWindowPos(Handle, 0, Left + Width div 2, Top, Width div 2, Height, 0)
The problem is that there is quite noticeable flickering in the always-visible part of the form (in this example, the buttons) while expanding or collapsing. Try it yourself!
It is possible for the operating system to resize the form to the left without any flickering at all -- just grab the left edge of the form using the mouse and drag the mouse to the left or right -- but I am unable to find any function in the Windows API that exposes this kind of resizing.
I have tried to use several different Windows API functions to resize and reposition the form, tried their various parameters (for instance, the SWP_* flags), tried LockWindowUpdate, WM_SETREDRAW, TForm.DoubleBuffered etc. to no avail. I also examined the possibility to use the WM_SYSCOMMAND SC_SIZE approach.
I am not yet sure if the problem lies at the OS level or the VCL level.
Any suggestions?
Edit: I am very surprised to see that this Q received close votes. Let me try to clarify:
Create a new VCL forms application.
Add a few buttons to the right side of the main form and a memo to the left. Set Anchors to [alTop, alRight] on all controls. On the OnClick handler of the buttons, add the following code:
if FCollapsed then
SetWindowPos(Handle, 0, Left - Width, Top, 2 * Width, Height, 0)
else
SetWindowPos(Handle, 0, Left + Width div 2, Top, Width div 2, Height, 0);
FCollapsed := not FCollapsed;
where FCollapsed is private boolean field of the form (initialized to false).
Now, click the buttons repeatedly. (Or give one of them keyboard focus and hold the Enter key for a few seconds.) You will probably notice that the region with the buttons on your monitor will not display a perfect still image, but will flicker. In addition, you might actually see 'ghosts' of the buttons to the left of the actual column of buttons.
I am unable to capture this millisecond flickering using screen capture, so instead I used a digital camera to record my screen:
https://privat.rejbrand.se/VCLFormExpandFlicker.mp4
In this video clip, it is apparent that the column of buttons isn't a static image on the screen; instead, for a few milliseconds each time the form is resized, this region is something else than it should be. It is equally apparent that there is a 'ghost' column of buttons to the left.
My question is if there is any reasonably simple way to get rid of these visual artefacts (that at least to me are very visible even if you expand/collapse the form a single time).
On my Windows 10/Delphi 10.1 computer at work, the form is resized in a perfect manner when I drag its left-most edge using the mouse: the unaffected client area of the form is perfectly static on the monitor. However, on my Windows 7/Delphi 2009 PC at home, I do see that there is a lot of repositioning going on when I do this.
I can provide some insight about why you see ghost images of the other half of your UI and possibly a way to stop it. The ghost image indicates that someone is copying your client area pixels (and copying them to the wrong place, always flush-left in your window) before you have a chance to redraw them with the correct pixels.
There are likely two different, overlapping sources of these ghost pixels.
The first layer applies to all Windows OSes and comes from a BitBlt inside SetWindowPos. You can get rid of that BitBlt in several ways. You can create your own custom implementation of WM_NCCALCSIZE to tell Windows to blit nothing (or to blit one pixel on top of itself), or alternately you can intercept WM_WINDOWPOSCHANGING (first passing it onto DefWindowProc) and set WINDOWPOS.flags |= SWP_NOCOPYBITS, which disables the BitBlt inside the internal call to SetWindowPos() that Windows makes during window resizing. This has the same eventual effect of skipping the BitBlt.
However, Windows 8/10 aero adds another, more troublesome layer. Apps now draw into an offscreen buffer which is then composited by the new, evil DWM.exe window manager. And it turns out DWM.exe will sometimes do its own BitBlt type operation on top of the one already done by the legacy XP/Vista/7 code. And stopping DWM from doing its blit is much harder; so far I have not seen any complete solutions.
For sample code that will break through the XP/Vista/7 layer and at least improve the performance of the 8/10 layer, please see:
How to smooth ugly jitter/flicker/jumping when resizing windows, especially dragging left/top border (Win 7-10; bg, bitblt and DWM)?
Since you have multiple child windows, the situation is even a little more complicated. The BitBlt type operations I mentioned above happen on your whole top-level window as a whole (they treat the window as one set of pixels regardless of how many windows are underneath, and regardless of CLIPCHILDREN). But you need to have windows move atomically so that on the next redraw they are all positioned correctly. You may find BeginDeferWindowPos/DeferWindowPos/EndDeferWindowPos useful for that (but only go there if the above tricks do not work).

How do I get my Cocoa app to draw exactly the color specified by my designer in Sketch?

My designer has specified a color to draw. When I try to draw that color in a Cocoa app, I get a resulting color that’s visibly different from the reference image as displayed by Sketch.app.
I made a small Cocoa app that draws a custom view. Here’s the interesting part of the code. Note that I am initializing the color in SRGB space.
class View: NSView {
override func drawRect(dirtyRect: NSRect) {
let components : [CGFloat] = [156.0/255.0, 0, 254.0/255.0, 1]
let color = NSColor.init(SRGBRed: components[0], green: components[1], blue: components[2], alpha: components[3])
color.setFill()
NSRectFill(self.bounds)
}
}
Here’s what it draws. (Nevermind the part about the cursor. And I removed the window shadow so it would be easier to review this side by side with other windows.)
And here’s the Sketch file portion:
Putting it all together, here’s a side by side of the Sketch file and the custom view, as well as Xscope loupe displaying the color value under the mouse cursor.
When hovering over Sketch file, I see this:
When hovering over my custom view, I see this:
You can see that the color value of the pixel under the black mouse cursor as read by Xscope is significantly different. The colors also look significantly different on my Retina Macbook Pro display, though interestingly, not so different in the captured screenshot PNG.
HOWEVER: so far, this was all done with default display settings and color profile “Color LCD” (the hardware is Retina Macbook Pro with its built-in display). When I manually change the display profile to “sRGB IEC61966-2.1” in OSX Settings app, and then sample the colors again with Xscope, you can see these sampled values:
And when sampling the custom view:
Most interestingly, you can see that the values sampled by Xscope on my custom view exactly match the specified values, and the color is also visually correct. But of course, I can’t make my users change their display profile.
My question: how do I make my custom view color exactly match the color in Sketch (both for visual inspection and when sampled with the Xscope loupe) with the default Color LCD display profile?
Just worked through this issue myself. Here's my process. Just tested on a Retina Macbook Pro.
Open Sketch.
Open Digital Color Meter (installed on OSX)
Switch to 'Display in Generic RGB'
In menu, ensure that 'View -> Display Values -> As Decimal`
Mouse over your color of the artwork in sketch and note the values (e.g. 0, 150, 200)
Use that value in Cocoa:
-(void)drawRect:(NSRect)dirtyRect {
[[NSColor colorWithCalibratedRed:0/255.0 green:150/255.0 blue:200/255.0 alpha:1] set];
NSRectFill(self.bounds);
}
This should work, as 'Generic RGB' is a device independent space equivalent to the 'calibrated' color space in Cocoa.

Matlab figure window transitions (screen transitions - GUI design)

My idea is building a logo image (similar to Matlab's startup one) which shows before my gui is appeared. I have the following code (it was obtained modifying the solution given by How do I make a splash screen for my MATLAB GUI application?) for it:
% create a figure1 that is not visible yet, and has minimal titlebar properties
fh = figure('Visible','off','MenuBar','none','NumberTitle','off',...
'DockControls','off');
% put an axes in it
ah = axes('Parent',fh,'Visible','on');
% put the image in it
ih = imshow('SIS.gif','Parent',ah);
% set the figure1 size to be just big enough for the image, and centered at
% the center of the screen
imxpos = get(ih,'XData');
imypos = get(ih,'YData');
set(ah,'Unit','Normalized','Position',[0,0,1,1]);
figpos = get(fh,'Position');
figpos(3:4) = [imxpos(2) imypos(2)];
set(fh,'Position',figpos);
movegui(fh,'center')
% make the figure1 visible
set(fh,'Visible','on');
pause(3);
close(fh);
I have two problems:
Number 1: I want the logo image's figure windows has no borders, no title and no taskbars. I have tried the WindowAPI but it does not work because I call it after the above code and because of the visibility of the window is off, then the handle of it is off too.
Number 2:I want that, when the logo image is disappeared, it is showed the gui window maximized. Where is the problem? The screen transition between the logo image's window and the gui window is not smoothed. I have tried to use a lot of Matlab´s applications that I found in Matlab Central's File Exchange (WindowAPI, Maxfig, Maximize, SetFigTransparency,...) without success. I realized that the problem is the visibility of my gui (I set off until all elements are created and then I change it to on). Because of the off visibility cause the handlevisibility is off too, the previous mentioned applications has no effects on the figure window that I want to maximize.
After observating the startup of Matlab, I have noticed that after the logo is showed, it appears a fullscreen image followed by the normal fullscreen of the program. So I have tried to create an maximized fullscreen windows that appears after the logo's windows is closed. However, now the problem is the transition between this last and the gui window. If I set the visibility of the gui window on and then I maximize it, during an instant it can be seen that transition that bothers me. I do not know what to do. I also think that if I could avoid that the guide window is currentfigure when I change its visibility, perhaps I would achieve it. Other solution it might be a timer that hold the white window as currentfigure while the guide window is behind changing its visibility but I do not know how to do. Thank you for your attention. Cheers.
As I show in this answer, derived from this MathWorks newsgroup thread, you can create a window without a title, borders, etc., using Java. Here's a modification of the code from my other answer to create a splash window centered on the screen:
img = imread('peppers.png'); %# A sample image to display
jimg = im2java(img);
frame = javax.swing.JFrame;
frame.setUndecorated(true);
icon = javax.swing.ImageIcon(jimg);
label = javax.swing.JLabel(icon);
frame.getContentPane.add(label);
frame.pack;
imgSize = size(img);
frame.setSize(imgSize(2),imgSize(1));
screenSize = get(0,'ScreenSize'); %# Get the screen size from the root object
frame.setLocation((screenSize(3)-imgSize(2))/2,... %# Center on the screen
(screenSize(4)-imgSize(1))/2);
frame.show; %# You can hide it again with frame.hide
I would try this to create your splash screen and see if it also helps with the problems transitioning to the next GUI window.
After a long research work, I have found a reasonable answer which was nearest to that I thought. If you type in "splash screen" in the File Exchange browser, you have some interesting applications designed specifically to it. I have chosen splash.m. With respect to the smooth transition, I have used these programs: WindowAPI and maximize.
The written code looks like this:
logoh = splash('logo.jpg'); %# Appear the logo image
fh = figure('Visible','on','Name','MyGUI','Position',[-1000,-1000,...
1000,500],'Menu','none','Toolbar','none','NumberTitle','off');
%# Put the figure window outside the screen (see Position property) because
%# its visibility is on
WindowAPI(fh,'Alpha',0); %# Make the figure window invisible
...
movegui(fh,'center'); %# Move the figure window to center
maximize(fh);% Maximize it
WindowAPI(fh,'Alpha',1); %# Make the figure window visible totally
pause(2); %# time during which the logo image is exposed
splash(logoh,'off'); %# Disappear the logo image

Mathematica: Unable to zoom in/out using the mouse on a 3D graphics after Rotate[]

I made 3D graphics, and using the known method of zooming, which is to hold the Ctrl and now slide the mouse up and down to zoom in and out as described here
http://reference.wolfram.com/mathematica/howto/RotateZoomAndPanGraphics.html
This works ok.
But now I issue the command Rotate[g,90 Degree], and try to zoom on the generate plot (in the new output cell). But the zoom no longer works on that new rotated image.
I see it blinking black each time I slide the mouse, but it does not zoom in nor out.
Here is the command
g=Graphics3D[ Cuboid[{-.1,-.1,-.1},{.1,.1,.1}],
AxesOrigin->{0,0,0},
PlotRange->{{-1,1},{-1,1},{-1,1}},
Axes->True,
AxesLabel->{"X","Y","Z"},
ViewPoint->Front,
Ticks->None]
now zoom works ok. Now type
Rotate[g,90 Degree]
Now try zoom on the result of the above command. It does not work.
version 8.0.1, windows 7
thanks
You are right, there is a bug in the interface.
After a few tries, pressing Ctrl and the mouse buttons, I was able to get a weird display:
And the zooming works (although inconsistently), but ... moving the mouse left to right!
I think this is either an abusive or unanticipated use of the Rotate command, depending on your perspective.
Rotate creates a RotationBox wrapper that instructs the FrontEnd (I believe) to rotate it's contents. When you apply this to an object with its own rotation controls, you have conflicting methods.
Generally speaking, Rotate should not be used on dynamic elements. Consider this modified example from the documentation:
DynamicModule[{p1 = {0, 0}, p2 = {1, 0}, p3 = {0, 1}},
{Framed#
Graphics[Polygon[{Dynamic[p1], Dynamic[p2], Dynamic[p3]}],
PlotRange -> 1],
Column[
{Slider2D[Dynamic[p1], {-1, 1}],
Slider2D[Dynamic[p2], {-1, 1}]~Rotate~(Pi/2),
Slider2D[Dynamic[p3], {-1, 1}]}]}]
Notice the strange behavior of the middle slider (try it), and also notice that its appearance is wrong. The latter is further indication that this use is noncanonical.

Resources