?
Solved

Get client area without borders

Posted on 2009-05-13
19
Medium Priority
?
1,768 Views
Last Modified: 2013-12-14
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
Comment
Question by:List244
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
  • +2
19 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24376269
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 24376308
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 24376314
Sorry abel - you were faster ...
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 19

Expert Comment

by:alb66
ID: 24376464
It should be

HDC hDC = ::GetDC( hWnd );
COLORREF cref = ::GetPixel( hDC, 10, 10 );
0
 
LVL 8

Author Comment

by:List244
ID: 24377241
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
 
LVL 39

Expert Comment

by:abel
ID: 24377385
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 24377615
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
 
LVL 8

Author Comment

by:List244
ID: 24384534
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
 
LVL 39

Expert Comment

by:abel
ID: 24384589
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
 
LVL 8

Author Comment

by:List244
ID: 24384791
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
 
LVL 8

Author Comment

by:List244
ID: 24391584
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
 
LVL 39

Expert Comment

by:abel
ID: 24394007
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
 
LVL 39

Expert Comment

by:abel
ID: 24394017
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
 
LVL 8

Author Comment

by:List244
ID: 24395092
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
 
LVL 39

Expert Comment

by:abel
ID: 24396724
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
 
LVL 8

Author Comment

by:List244
ID: 24396893
Yes.
0
 
LVL 8

Author Comment

by:List244
ID: 24432889
Anyone have ideas?
0
 
LVL 8

Author Comment

by:List244
ID: 24454116
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
 
LVL 39

Expert Comment

by:abel
ID: 24454250
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question