SetWindowPos API - Backward resizing? - windows

I'm using Windows's SetWindowPos API to try to 'crop' a part of a window so a user will only be able to see the RIGHT part of it.
Problem is, I can only 'crop' it, starting from the top left corner, and I would rather cropping a different part of the window which does not 'contain' the top left part.
As seen on the image below, what i'm trying to do is reverse the 'resizing' mechanism in such way that the window will 'hide' the Label1, and only show the Button1.
It is possible that I am not using the correct API for the job, If so, i'd love to be pointed toward the right one.

You need to move your controls on the Form for that to work. The window has a position and a size, which is all the window manager cares about. What is visible inside is your application's responsibility.
Your screenshot looks very VB6-ish. Do note that newer technologies, e.g. Windows Forms or WPF offer better support for what you are trying to do here.

If you have a window handle for the control, then you can place it outside its parent. Pass to SetWindowPos a negative value for the X coordinate.
Of course, the fact that the control cannot be seen does not mean it cannot be interacted with. The user can reach it with the keyboard if it accepts focus. So perhaps a better idea would be to hide and/or disable it. Use ShowWindow and EnableWindow to do that.
Then again, it's quite plausible that you won't be able to get a window handle. Many frameworks use controls that are not windowed. To interact with them you would need to use an automation framework such as UI Automation. Only you can know whether or not the target application supports automation.

Related

What is the best way to get all ui elements of a window?

I want to get a list of all UI elements of a window, In a way I can recreate them on another PC? Currently I am focused on windows but a multi-platform answer will be greatly appreciated. Thanks! (What I mean below)
There are a couple of ways to go.
Option 1: If the window is a traditional one where the UI elements are child windows (rather than windowless controls), then you can use GetWindow or EnumChildWindows to iterate through the UI elements in a given window. You then would have to examine attributes of those windows, like the window procedure, window styles, and window text, to build a list like you're suggesting. You could probably make this work for all the standard controls. The problem is making it general enough to handle everything. Lots of applications have custom control types that your code wouldn't be prepared to recognize, or it might lay them out in ways that aren't obvious. You also won't be able to deal with windows that use windowless controls.
Options 2: You can use UI Automation to walk the tree of UI controls. This is how accessibility technologies like screen readers interact with applications.

Minimize-in-place - Do I need a custom framework

I want to create a system wide minimize-in-place feature that occurs when double-clicking the title bar of any visible window in layer 0.
It seems that this would be a really simple feature to re-implement... When a title-bar is double-clicked, just draw the title bar only. That's it. The problem is implimenting it in all applications. I think it requires writting a custom framework to override the behavior in AppKit? Maybe NSApplication, NSWindow or NSView?
How can I recreate minimize-in-place?
Is a framework my only choice? If I create a framework, can I replace the behavior of minimize in 3rd party apps?
Which framework do I need to override in order to intercept and recreate the default behavior of the minimize button?
More about minimize-in-place:
I am familiar with WindowShade by Unsanity, this is exactly what I want to create. Supposedly unsanity is working on a Lion version, but their track record is bismal. Minimize-in-place was a system feature way back in the days of OS 7 or 8. I have tried other utilities that try to replace this feature, and there aren't any that do minimize-in-place at a core system level like it needs to be done. Please don't offer utitlity suggestions, I am going to build my own.
I have built an Application that recreates minimize-in-place, but it's not good enough.
My Application semi-successfully recreates minimize-in-place by putting "placeholder" windows (belonging to my app) in place of the 3rd party windows when they get minimized to dock. When my window (title bar only) gets double-clicked, I close my window and restore the real window from the dock.
My custom app works perfectly, but there is a lot of application switching going on. I have optimized the switching between apps to be nearly seamless, but the fact remains that there is application switching going on every time a window title bar is double-clicked. The result of application switching is that menu bars switch back and forth, pallets of 3rd party apps hide themselves when my app takes focus, and the list goes on.
So, although I've built a concept app, this method isn't going to work as I'd like it to. Minimize-in-place needs to be implemented using some other method than building an Application, and I need help understanding how to do it.
What I know think I need to do. Suggestions and assistance welcome.
I think I need to write a custom framework that replaces AppKit? This seems overwhelming even though I only need a super-tiny portion of the code to be overridden? i.e. the core _minimize function whatever that may be.
When a title-bar of 3rd party window is double-clicked, just clip to the title bar and let the rest of the system function as normal. On un-minimize (double click 2nd time), set clip back to full window.
Simple right?
Thanks for any assistance/suggestions,
Chris

Constraining window positions in other applications in Cocoa?

I'm relatively new to Cocoa development. I'm developing on Snow Leopard. I have a scenario in mind and I'm curious if the API supports it. I've not been able to find anything about it, so I assume its not possible. But I'm happy to be proved wrong.
Suppose I write an application that lives at the top of the screen just below the menu bar. The window floats, so it is always on top. This can be annoying, so one thing I had in mind was constraining other windows (especially those in other applications) so that they are below my application. Is there something in the API that would allow me to do that? To specify a maximum origin for all windows (except mine)?
Thanks.
There's no real supported way to do this. You could achieve something similar using the Accessibility API. You'd have to watch for window-moved notifications and shove the window back into the visibile area.
I don't think you can do that, you see, one of the cool things of a window's based UI is that you can stack Windows on top of others, in a desktop metafore.
Now, what you can do is a always on top window, using NSScreenSaverWindowLevel as the NSWindow level. One nice trick you can do is [window setLevel: NSScreenSaverWindowLevel+1], and it set's your window above the screensaver level.

How does a Windows non-native user interface work?

Through experience I have found that the native windows forms/components don’t like to be changed. I know using Delphi or Visual Studio you are given native windows components to populate a form or window with and then you attach code on events that these components may do (onClick for example).
However, how do all of these programs like Word or google’s Chrome browser alter the standard windows’ window? I thought it was somehow protected?
Chrome seems to have tabs actually on the window’s frame?
I know you can also get toolkits like Swing and QT that have their own controls/components to populate a form. How do these work? (How does the operating system/computer know what a non-native button should act like? For example; Chrome's back and forward buttons, they're not native components?).
I can understand how OpenGL/DirectX window would work because you’re telling the computer exactly what to draw with polygons/quads.
I hope this question is clear!
Windows does not protect GUI elements. Windows and controls can be subclassed to handle various drawing operations in a custom way. For example, windows may override and reimplement the handling of the WM_NCPAINT message to draw a custom titlebar and frame:
http://msdn.microsoft.com/en-us/library/dd145212(VS.85).aspx
Some Windows controls have an "owner-draw" mode. If you use this, you get to draw the control (or at least vital parts of the control), while Windows takes care of responding to user input in the standard way.
Swing ant QT draw their own widgets at a low level using basic primitives, but they also have theme engines which can mimic the native controls.
Qt moved to native controls a while back. As for how swing does it, it gets a basic window from the OS. Then much like Opengl\Directx it does all of the drawing with in that window. As for where to position things that is what the layout managers do. Each manager has a layout style horizontal, vertical, grid, components it has to draw and a section of window it is expected to fill. From there it does some pretty easy math to allocate its space to its controls.
There's no magic: non native controls are simply drawn on a blank window. Or, instead of being drawn they may be represented as one of several bitmaps based on state (ie: a button may be represented as a .png for the normal state, another .png for the pressed state, etc)

Any possibility to get a notification if another application receives a scroll event?

I'm developing an application in Cocoa which allows users to draw on any given window in OS X. The drawings move along with the corresponding window when dragged on screen. To complete this tie between drawings and the windows (and their contents) beneath, I'd like to catch scrolling events from the window in order to react on the positioning/visibility of the drawings.
An example:
The user opens Safari and browses the web. On a specific website s/he draws a circle around a link and takes hand written notes (this is all considered a drawing, input by a pen tablet). Afterwards s/he moves the window, the drawings are also being moved so that they remain on top of the link on the website. Then s/he begins to scroll the website and the location of the link changes (moves up until it's outside of the viewport).
Now I'd like to catch that event and also move the layer with the drawings to keep them on top of the link. When the link is no longer visible, I'd turn off the visibility of the drawing and turn it back on when scrolling brings the link back on to the viewport.
I know this is a quite tricky assignment and being able to intercept such events from another application might as well be considered an OS security flaw but maybe someone out there is good enough a coder to give me a hint... :)
The Cocoa Accessibility classes may be helpful but until now I haven't found the solution.
Thanks for your help.
Oh, and if that's not tricky to you, maybe you can tell me how to get notified when Safari switches Tabs ;)
kkthxbai
I'm not sure if you can monitor scroll events. However, it's a lot easier if you just monitor the position of the link with the Accessibility API.
Just hold a reference to that link and constantly poll it for its position, if the position changes, you know what to do.
You could also try using AXObserverAddNotification, but as far as I am aware, there is no notification you can monitor for position changes.
If you haven't discovered it already, the Accessibility Inspector can help you a lot with identifying things that you can get using the Accessibility API and pfiddlesoft's UI Browser lets you register for notifications.

Resources