Click to See Complete Forum and Search --> : Detecting Screen Resolution


Greenfish
July 15th, 2005, 01:19 PM
Hello, I'm new here, but I've been programming for a couple of years now. Frequently I do a Google search for pieces of code that I need for programs that I write, and frequently CodeGuruForums have been helpful! So I figure I could start asking directly, since apparently the question hadn't been recently answered.

I need a way to detect the resolution of the screen that my program is running on (800x600,1024x768, etc.).

It would be all I need to complete this need little program that I'm writing: I'll post it (or email or something), if you can help me finish it.

Smasher/Devourer
July 15th, 2005, 02:33 PM
Call GetSystemMetrics() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getsystemmetrics.asp) with SM_CXSCREEN and SM_CYSCREEN to obtain the width and height of the screen in pixels, respectively.

Greenfish
July 15th, 2005, 04:01 PM
Thanks!
I had seen that before, but I don't think I was doing it correctly. The MSDN website made it clear, and for whatever reason it works now.
I'll attach a compiled copy of my program when I finish it.

NoHero
July 15th, 2005, 05:58 PM
Thanks!
I had seen that before, but I don't think I was doing it correctly. The MSDN website made it clear, and for whatever reason it works now.
I'll attach a compiled copy of my program when I finish it.

You don't need/have to attach a compiled copy of your program. Posting the solution is more helpful, because anyone else who expierience the same problem in future can get help from this thread.

Greenfish
July 15th, 2005, 07:10 PM
Well, the simple solution was just that:

int cx = GetSystemMetrics(SM_CXSCREEN)/2;
int cy = GetSystemMetrics(SM_CXSCREEN)/2;

The rest of my code, is a little sloppy (I'm used to a different type of C++ compiler) and doesn't have much to do with my question - other than using those variables for the center of the screen. Posting the compiled copy was a sort of reward (for whoever wanted it), to see what they helped create. But if they have the source code, then they can view it or compile it, so I'll just post the source code.

Marc G
July 16th, 2005, 05:31 AM
Well, the simple solution was just that:

int cx = GetSystemMetrics(SM_CXSCREEN)/2;
int cy = GetSystemMetrics(SM_CXSCREEN)/2;

I think you mean:
int cx = GetSystemMetrics(SM_CXSCREEN)/2;
int cy = GetSystemMetrics(SM_CYSCREEN)/2;


NOTE: The above calls will return the size of the primary display on your system. If you need the dimensions of the virtual screen (combining all your monitors), you can use SM_CXVIRTUALSCREEN and SM_CYVIRTUALSCREEN.