Get which process/window is actually visible in C# - c#

I have a list of all open windows, the ones in the windows taskbar. What I want to do is to remove the ones that are not visible from the list.
What I mean is, if a window is hidden by another one, it should not be in my list.
I've been searching google for the past two days without success...
Any ideas?
PS: I can get the location and size of each process, so getting the last time a process was used/idle time would work too...

You need to use regions, what's still visible of the window can be a complex set of rectangles. Start with the window you want to check, GetWindowRect and CreateRectRegion to create a region for the window rectangle.
Call GetWindow() in a loop, passing GW_HWNDPREV. Which returns the previous window in the Z-order so it could overlap. GetWindowRect() to get its rectangle, CreateRectRegion() and combine that with the original window region using CombineRgn() with RGN_DIFF.
When GetWindow returns NULL you've found all possible overlapping windows. Use GetRgnBox to check if there's anything left of the region. If there is, at least one part of the window wasn't overlapped.

Related

How do you keep a ToolStripSplitButton within the bounds of its container

I am developing a windows forms application with C#
My main form has a toolstrip with a ToolStripSplitButton which is aligned to the right. When the split button is clicked, its menu displays over the edge of the form, unless, the form is too close to the far right hand side of the screen.
I have played with various properties to try and solve this, but without much luck!
Is there anything (maybe even a DllImport), that can force the button's menu to always fold in and towards the form, and never exceed the forms bounds?
As worked out in the comments on the OP, if you don't use images in the sub-items of the ToolStripSplitButton, this is an easy solution:
1) Set the RightToLeft property of the ToolStripSplitButton itself to Yes
2) Set the RightToLeft property of all sub-items to No to align the text correctly.

How to resize a window with a certain key-combo + mouse move? (like Alt+F8 in Linux) Not click and drag

In a C# winform app, I'm trying to implement some of Linux window manager features.
I already did a lot of stuff, but right now, I'm trying to do the re-sizing part, where you press a certain combo at a window, then move your mouse to the right/left, the width of the window increases to the right/left, move it diagonally, the window will increase in both directions.
Now I already know how to register the hotkeys, get/set mouse cursor pos, move a window, etc. All in winapi.
The thing that I can't seem to get that much, is how can I increase the window's, say width, in one direction? if I normally increase it, it will span in both directions. I gave this a thought, and I think that: the window will increase in both directions, but if we want to just increase it's width to the right, we keep re-positioning/moving it at it's start point, so that the left side of it will remain fixed, will the right one will span.
What is the right and easy way to do this? Am I right about my guess?
And please note that I don't just want this to work only in my form, but on any window.
Any C++/C#/Winapi code would help.
Any ideas/thoughts of some sort would also be great.
Thanks a lot.
Any Windows window:
Alt+Space - brings up the standard Windows menu
S - selects Size
Arrow keys (Or Mouse) - Resizes the window
Enter - commits the operation

Get a window by coordinates

I have a point in screen coordinates and I need to find which window it "falls" onto. I.e. find the window (hWND) that is foremost of all windows containing the point. Pinvoke and WinApi is fine...
PS. I actually need to detect, which window is my mouse cursor currently "over", to put it another way.
Use WindowFromPoint.

User controls do not properly fit on the screen

My application has several controls. Like in one screen has TreeView on left side, GridView with paging in the middle and 4 buttons at right side. The controls properly appear when the form is in a maximized state, but if I minimize it the controls do not properly fit on the screen.
I tried with different different tricks like table layout.. in dat I added a panel, etc...
But I could not solve the problem.
How can I create such type of screens which fits independently of size of my window?
Thanks
I had the same problem a while ago. In my case i had a Button and a ListView within a GroupBox that was inside a SplitContainer, which was within a UserControl on a TabControl. I wanted the button on the top right located and anchored and the ListView took the space leftover, so i couldn't dock it. Instead it was anchored in all four places.
In my case my button and the listview are worked and behaved correctly in the designer, but in my running app the button was positioned to far on the right and the ListView size had also a too much width.
For a first bugfix i did some iterations about positioning the button in the designer a little more to the left, check it running mode, re-align the button in designer from the impression i had in the last run.
So i got it to work and started over with some other thing i had to do in my app. After a while i got a new feature request and needed another button within this messy thing. This time i did a complete rebuild of the gui elements on a new usercontrol, just to see if the problem reappears. To my amazement this gui mock just behaved as expected.
Within my code i didn't anything about changing the location, size, anchors, docks, etc. So the problem had to be within the InitializeComponents() code created by the designer.
I started with a diff of both versions and couldn't see any big differences (there were a lot of them, but only minors like size, location or name). So i started to put code from my crazy usercontrol into the mock and running the mock in my app. After several copies the problem also appears in my mock, so i got the root cause of the problem.
What do you think, which property leaded to the crazy behaviour?
It was the MinimumSize of my SplitContainer!
So to get really the root cause of your problem you should start over with an empty UserControl (or Form) and just place all the elements on it with the desired behaviour (size, location, anchor, dock).
Nothing more!
Then test if this mock behaves the way you want and if not, post this code here and tell us what you expected to see.

How can I write my own ContextMenu? C#

I feel quite limited by the default ContextMenuStrip, as it only can contain buttons, and no Controls.
I was wondering that for a long time, and I already tried it, using forms, but it never really worked out.
I already have I idea on how to set the whole thing up, with events and items. The only problem I have is the paint method.
When you open a ContextMenu (ContextMenuStrip) you can set its position on the mouse cursor, and it will be there, even if that means that it goes beyond the active form. (So I can't use the Controls Class as inheritance, as they can only draw themself as a part of a form.
Now I thought to use the Form Class as a base for my ContextMenu, but those where placed on the screen randomly.
So what I actually need is a class (or something similar) that can draw itself, without problems, and can be placed accurately on the screen.
Any hint would be nice, thanks.
Greg the Mad
Your first statement is false -- you can have a TextBox or a ComboBox in a ContextMenuStrip.
MSDN ToolStripComboBox
MSDN ToolStripTextBox
From the designer there is a small drop-down arrow when your mouse is in the "Type Here" box (sometimes hard to click) that will allow you to change the type.
If you are looking to allow for any type of control to be displayed in a top down fashion inside of a container to be positionable... you could always make a custom control using FlowLayoutPanel. With it's properties FlowDirection=TopDown and WrapContents=False to keep a vertical approach. This will handle your "menu" basics and your new control can expose whichever events you wish from each Control. You will have to handle the logic of showing the panel and positioning with it's Location property as well.
I forgot to address the issue with drawing outside of the parent form. Notice that ContextMenus are smart and when they reach a boundary of their parent they draw away from it. You should logically be able to draw in the correct direction (Up/Down or Left/Right) from any right mouse click. Per your attempt with a Form, set StartPosition=Manual then prior to calling Show() or ShowDialog() set it's Location property respective to the X and Y parameters provided in the event args of MouseClick.

Categories

Resources