How to return the handle of a window when we click on it, without any DLL injection? - c++

For one of my projects, I need to create a function that will return a handle to a window when the user click on it (any window displayed on screen, and anywhere inside that window).
I know it is possible to use a global hook, but I think there must be a more simple way of doing that, without using any DLL injection.
In fact, I could intercept the left mouse click or intercept when a window is activated.
Can I use one of those 2 solutions without any DLL injection?

You could use a LowLevelMouseProc hook to intercept the click, and then use WindowFromPoint to determine the window. (I haven't actually tried this.)

Call SetCapture. When you do that, all subsequent mouse events will go to your own window. When you get a click event, call ReleaseCapture, and then WindowFromPoint to find out what window resides at the point where you got the click event. The coordinates you get in the click event will be relative to the window you passed to SetCapture, to remember to convert them to screen coordinates first. Use ClientToScreen.

Related

In C++ , how can I get the user to specify a point on the screen (X,Y) out of the app window?

I must find the coordinates of a point which is out of the application window.
I intend to have a button "Specify Point" and when the user clicks this button, their next mouseclick will be registered by the program as this desired point. The problem is I don't know how to implement this....Any ideas ?
You can call SetCapture to direct mouse clicks from anywhere on the desktop to your window. Call ReleaseCapture after the click to return to normal.
If the user clicks on another application you will receive the click but the other application will also be activated, which you probably don't want. To avoid this, an alternative approach is to overlay the entire desktop with an always-on-top very nearly fully transparent window (a fully transparent window won't get clicks). Transparent windows are known as layered windows; use the WS_EX_LAYERED extended style to create one.

What Function can I use to get the handle of a button?

the situation is as follows:
I have the handle for a window (which i got with the function FindWindowEx() ) and that window has 3 buttons. I would like to know how I can get the handle for 1 of the 3 buttons. I mean I know ppl can use spy++, but I am sure there have to be functions that can do it for me, so my questions are:
Question::
What function or functions can I use to get the handle of a button of a window (already knowing the handle for the window)?
Question2::
How do I get the Button's ID???
You can loop through children of that window with EnumChildWindow.
It lets you define a callback function to be called for every child of the window.
In the callback, you can check if the current child is the button you need.
For example, if you dinamically want to press an OK button, you can check if the window text equals OK. You can get the window text with GetWindowText.
If you know the position in the parent window you can use ChildWindowFromPoint.
For this to work you will probably have to check the windows status first (maximazed or current width-height) to be sure the button is right where you want.
An easy way may also be getting his position relative to another child of the window that's easier to retrieve. For example, if the button is always to the right of a text-box control, you can get coordinates of the text-box and use ChildWindowFromPoint passing those coordinates with something added in the x direction.

Mouse click on a background window without moving mouse c++

I need to know if there is a code in c++ that allows to simulate a click, on a background window (if that's impossible, a foreground window will do fine), without moving the mouse.
I also need to click on specific coordinates, and drag an item to some other position (always without moving the mouse).
Example:
my mouse pointer is at (500,700),
but i need to left click at (100,150),
and drag to (700,300).
I need to be able to move my mouse pointer without affecting the program,
and the program must run correctly without moving, or locking, the mouse pointer.
If this action is impossible in c++, a VB code will be apreciated.
From a C++ Windows application, you can call Windows API directly by first finding the window using FindWindow that will give you the window handle you want, then finding the area within this window that you want to click. You can use API like GetWindowRect for this.
Finally, you can send this window, or an area within it, mouse messages using mouse_event function to cause the mouse to move, click, drag and let go at a new location.

Place a window (borderless) on top (z-index) of another window (borderless) such that the one on top trails the one on the bottom

The concept of DWM window manipulation/Window Styles is a bit new to me and I am experimenting with a few new situations. I would be obliged if someone could help point me in the right direction.
Also, how do you keep the 2nd window exactly behind the 1st instead of another window from another application in between them?
You need process the WM_WINDOWPOSCHANGING message on one of the windows and use SetWindowPos to make sure you keep the z-order. You can either use SetWindowPos to send the window below a message that it should precede the current window, or call it from the second window to the first one with the HWND_TOP argument when the z-order changes.

can't change mfc controls order

I have a dialog box with a list box,slider and a button.
I tried to change the background color but I couldn't managed to change that, so i thought that if I add a "picture control" as a bitmap and put it in the background i will succed, but now the problem is that the "picture control" is on top of all the controls.
I tried to change the the tab control with Ctrl+d but it didn't change anything.
I also tried to use SetWindowPos to top or buttom but also it didn't change anything.
I noticed that if I click in the location of the button it's brought to the front as I want.
Is there any way to "click" all the controls at the begining? do i miss something in order to bring the control to the top?
If you need to change the background colour of the dialog box, you need to handle the WM_CTLCOLORDLG message and return the handle to a brush (if the brush is not a stock object, make sure you delete the brush after the dialog box is closed) -- or, you can process the WM_ERASEBKGND message and erase the background yourself.
I tried to change the the tab control with Ctrl+D but it didn't change anything. I also tried to use SetWindowPos to top or buttom but also it didn't change anything.
Ctrl+D does get you in reordering mode, however there is a more reliable way of checking. The dialog template is in text form in .RC file, where you can review the order of control with text editor and sort lines manually the way you wish. This will be the order of control creation and tab order as well. Sometimes it's even easier to reorder controls this way.
More to that, when your application is running, Spy++ SDK tool can enumerate windows and again it will give you window order for checking.
SetWindowPos with proper arguments changes Z-order of controls on runtime as well.