Sign in to follow this  
AlsoPenCover

Finding the upper-left corner of the window - C#

Recommended Posts

The reason I want to do this is because I want to find the position of the mouse relative to the window. The only way I've been able to do it is using Form.Location.X, but that gives me the upper-left corner of the screen, underneath whatever border is there (usually the Windows default). It seems strange that if I draw to the coordinates (0, 0) I draw to the first pixel in the upper-left corner that is visible, which is not the same as Form.Location.X. Is there anyway to get that pixel, the first one visible in the upper-left corner, regardless of whatever border is there? I've tried so many different ways, yet I can't figure it out. I'm almost certain that it's possible though, because I don't see why they would let you. I can do this in Java fairly easily: I just use JFrame.getInsets().left, and that gives me the left margin of the border that Windows put on the window for you. If that helps at all, for those of you who know both Java and C#.

Share this post


Link to post
Share on other sites
ClientRectangle.Location always gives me (0, 0), it says so in the description in the MSDN. The only thing I can use that for is the width and height of the window not including the border, which I already know.

EDIT: I think I'll just rephrase it, because I'm probably just saying too much without actually saying what I want to: Is there anyway to find out how big the border is around any given window, WITHOUT actually counting out the pixels? Is there any built-in function in C# that gives me those values?

Share this post


Link to post
Share on other sites
Quote:
ClientRectangle.Location always gives me (0, 0), it says so in the description in the MSDN. The only thing I can use that for is the width and height of the window not including the border, which I already know.

Calling myForm.PointToScreen(Point.Empty) will give you the upper left location of the client area of the window, relative to the upper left corner of the screen.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike.Popoloski
Quote:
ClientRectangle.Location always gives me (0, 0), it says so in the description in the MSDN. The only thing I can use that for is the width and height of the window not including the border, which I already know.

Calling myForm.PointToScreen(Point.Empty) will give you the upper left location of the client area of the window, relative to the upper left corner of the screen.


Wow, thank you so much. I'm surprised I missed that method, I must have gone through every method in the Form class half a dozen times, and I guess I just kept ignoring that one.

On a side note, I wonder why that's the only way to do it. That method seems like something they just threw in there (just based on the fact that there are no overloads for it - something like that usually has at least one additional overload with 2 ints as the parameters), thinking that they didn't really need it. You'd think it would be important, because when you draw to the window, it automatically adjusts itself for the offsets.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this