Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1793
  • Last Modified:

Get client area without borders

I am in need of getting an applications client-area without borders. Since the borders in different Windows versions change, borders are causing me problems.

I have a static pixel location, let's say 10,10. I want to be able to monitor this pixel's color. My problem is, with borders, the pixel is not always at 10,10. What if the application has no border? What if the windows scheme creates larger or smaller borders?

Is there a way to get pixels with 0,0 always starting at the top-left of the client area rather than of the window area?
0
List244
Asked:
List244
  • 8
  • 7
  • 2
  • +2
1 Solution
 
abelCommented:
Isn't what you are after the function GetClientRect? It retrieves a RECT structure without the borders. And the (0, 0) position is always inside the border for any painting functions.
0
 
ZoppoCommented:
Hi List244,

I'm not sure if I understand - the rect returned by GetClientRect() always returns the rectangle of the window's client area with 0, 0 as the top/left point.

So I guess the problem is how you monitor the pixel at 10, 10 - IMO you need a DC for the client area of the window - this is returned by GetDC().

Maybe you should post some code so we could see what the problem is ...

ZOPPO
0
 
ZoppoCommented:
Sorry abel - you were faster ...
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
alb66Commented:
It should be

HDC hDC = ::GetDC( hWnd );
COLORREF cref = ::GetPixel( hDC, 10, 10 );
0
 
List244Author Commented:
I am basically looking for some sort of translate.. Firstly, I have a color-picker. This color-picker grabs a color off the screen and gets a pixel position. This is how I find the static position (10,10). How this color picker works is it uses GetWindowRect and gets the DC of desktop. It then gets the current mouse coords and subtracts the left/top from the rect. This gives me the current position in the window of the pixel under the mouse.

However, when I go back to check this position (10,10), it is no longer returning the same position since getpixel is ignoring borders and the previous method of subtracting the rect is not.
0
 
abelCommented:
It is easier to use GetPixel and GetWindowDC on the desktop window, because GetPixel is free of the current window and the desktop window is borderless you don't have offset problems. Then they'll work. I saw that technique in this code: http://www.codeguru.com/vb/gen/vb_graphics/article.php/c2357
0
 
itsmeandnobodyelseCommented:
I don't know whether I understand your problem correctly but let me try:

Assuming the (10, 10) are relative coords to the window's client area and you need the absolute screen coords out of this, do

   POINT pt = { 10, 10};
   ClientToScreen(hwndClient,  &pt);

If the (10, 10) are screen coordinates use

   POINT pt = { 10, 10};
   ScreenToClient(hwndClient,  &pt);

to make it client coords.



0
 
List244Author Commented:
I am assuming that my problem is borders, but I will explain my problem:

I use GetDC on the desktop. I then get the current window (WindowFromPoint) and subtract the top-left. This gives me my pixel location in the current window.

I then take what I get from that to another application. It again uses GetDC on the desktop. Then, it adds the top-left of the window to the static (10,10), or whatever the pixels may be. But, when I do this , I am getting two different pixels.

I need to be able to use one program to grab a pixel and it's location, and I need to take that to my other program which is needs to find that pixel and monitor it.
0
 
abelCommented:
Remove the part where you do a WindowFromPoint. The GetPixel works fine with only the desktop DC, regardless the windows on top of it. In fact, you should not need any offset calculations at all.
0
 
List244Author Commented:
Well, the reason I am doing that is to get the coord in the app for my other application. Because if I just get the point... I don't know that when my other app runs that it will be in the same spot.

Should I replace that with ScreenToClient on the WindowFromPoint? Rather than subtracting top-left?
0
 
List244Author Commented:
Here is an update with more specifics:

ScreenToClient(WindowFromPoint(*Mouse),Mouse); //Mouse is an LPPOINT

That is how the original X,Y are grabbed(489,469)

Then the following is done:

POINT Pos;
Pos.x = 489;
Pos.y = 469;
ClientToScreen(WinHandle,&Pos); //WinHandle is a known handle to the window in question

Now I get: 1184,673.

I use SetCursorPos(Pos.x,Pos.y)

Now, it sets the cursor to the wrong position. It is not the position I wanted it at. Also, when I return to the color picker, it tells me that the position it set it to is:

(487,438)


Maybe now that I have included the code someone can help me out.
0
 
abelCommented:
I'd love to help you out, but something tells me that I'm not certain about what you are doing. Let me try to put it in my words, and perhaps you can tell me whether I am correct or not.

  1. Get the color under the mouse pointer, regardless under what window or process
  2. Run another program, which monitors this pixel
  3. If the window where the pixel was found, moves, the relative position should be used of that window.
I'm not sure about step 3. If you want any static position relative to the top-left of the screen there should be no problem at all, because that's just absolute. But since you seem to have problems, I assume that step 3 is important. Can you confirm / elaborate?
0
 
abelCommented:
Btw: I have no idea what SetCursorPos does in this thread, is that some other requirement, moving the mouse pointer? Let's focus on the monitoring bit first... ;)
0
 
List244Author Commented:
Abel, SetCursorPos is a test. It is my way of seeing what the program is doing. It is not working, I know this, so I added that to see where the program is trying to monitor vs. where I wanted it to. And in doing so I found that it is actually a bit off from the pixel I wanted it to monitor.

Abel, not quite. Here is the best way I can explain it:

Say you have a 3rd party app which has a circle in the corner of the app. This circle changes colors depending on the status. This color is a code which has meaning. Let's say red is an error, black is a slow-down, and green means it is running good. So, I make a color-picker, this application's sole purpose is so that I can find the position of this circle, or at least, a pixel in the circle. I take this coord, and I take it to my REAL application, the one I am developing. And I use this application to monitor that circle... if GetPixel returns Red, it notes the error, black, it notes the slow-down, green, it does nothing.

Now, my problem is the pixel I am getting from the picker is not matching up with the pixel the real application is grabbing.

I've tried to fix this most recently by using ScreenToClient and ClientToScreen. I got the pixel with the color picker by getting the general cursor pos and using ScreenToClient. This gave me the pixel at 489,469. I took that number to the real app and did ClientToScreen. This failed to grab the correct pixel. So to test it, I set the cursor position. It set it a bit off from where I wanted.

So my problem is.. how do I get the cursor position relative to a window, and then save that so that I can get that position relative to that window at any time?
0
 
abelCommented:
Ok, that makes sense. Am I correct in saying that the circle is relative to a certain windows topleft corner? In other words, you will need the window position containing that circle and find the pixel relative to that top left corner. And monitor that.
0
 
List244Author Commented:
Yes.
0
 
List244Author Commented:
Anyone have ideas?
0
 
List244Author Commented:
I got it working:

I was using:

GetCursorPos
ScreenToClient(WindowFromPoint,Pos)
ClientToScreen(WindowFromPoint,Pos)

It was not working correctly. I changed it to:

GetCursorPos
WIN = WindowFromPoint
ScreenToClient(WIN,Pos)
ClientToScreen(WIN,Pos)

This fixed it. Apparently somehow, even though the application was not changing, it was not liking the recall to getting the HWND.
0
 
abelCommented:
Funny, we were working on it together, but I was a bit busy so it went on slowly... But I have a nice color picker now which can monitor any pixel on the screen ;)

Quite odd that the change you show, actually makes a difference. Glad it eventually works though.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 7
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now