Hi,

I have a form that has Width & Height (1000,1000).

Now if the users screen has for example this sizes (800,800).
Then I want to adjust the Forms Width & Height to 800,800 so it fits the screen.

So my question should be if it is possible to detect a monitors size in Width and Height ?

Thank you!

Recommended Answers

All 10 Replies

I use this for getting the window size in Windows. No idea how you get it on other operating systems.

int WINDOW_WIDTH, WINDOW_HEIGHT;
	HWND hDesktopWnd;
	HDC hDesktopDC;
	hDesktopWnd = GetDesktopWindow();
	hDesktopDC = GetDC(hDesktopWnd);

	WINDOW_WIDTH = GetDeviceCaps(hDesktopDC, HORZRES);
	WINDOW_HEIGHT = GetDeviceCaps(hDesktopDC, VERTRES);

	ReleaseDC( hDesktopWnd, hDesktopDC );

Yes, I use Win Forms.

Thank you for the links. I have managed to use this code wich seems to be correct.

So I set the width and height of the form to the same as the monitors width and height.
But when this code is running the Form is not centered, to Fill up the screen. Big parts of the Form is "outside" the screen view.

How is it possible to center the Form so it fills up the screen?

Screen^ screen = Screen::PrimaryScreen;
				int S_width = screen->Bounds.Width;
				int S_height = screen->Bounds.Height;	

				Form1::Width = S_width;
				Form1::Height = S_height;
				Form1::StartPosition = FormStartPosition::CenterScreen;

Not sure. Probably setting the Location to {0,0} (or as close to it if it needs room for the border).

I think this will do it, that if the users screen is less in size than the form, then maximizing the Form will work I think.
Thank you for help.

//If the Form is bigger than the users screen, then maximize the window:
			 Screen^ screen = Screen::PrimaryScreen;
			 int S_width = screen->Bounds.Width;
			 int S_height = screen->Bounds.Height;	

			 if( S_width > Form2::Width || S_height > Form2::Height )
			 {
				 Form2::WindowState = FormWindowState::Maximized;
			 }

Hmm.. The most common way to detect the monitors size in windows is

INT32 width = GetSystemMetrics(SM_CXSCREEN);
INT32 height = GetSystemMetrics(SM_CYSCREEN);

MyForm->Width = 1000;
MyForm->Height = 1000;

if(MyForm->Width > width)
     MyForm->Width = width;
if(MyForm->Height > Height)
     MyForm->Height = height;

MyForm->Top = 0;
MyForm->Left = 0;
MyForm->StartPosition = FormStartPosition::Manual;

Thank you ShadowScripter.. Perheps that is a more portable way to detect it.
I will perheps use that instead.

Just updated the last entry for a more practical approach.

Thanks.. I tried to only execute this code as a start but it does not
compile.

So I wonder what could be missing?

             INT32 width = GetSystemMetrics(SM_CXSCREEN);
             MessageBox::Show(width.ToString());

Well, it isn't a working script. It was just an example of how you could do it.

Here is a working script if you'd like to see it in action.

#include <windows.h>

#using <System.DLL>
#using <System.Windows.Forms.DLL>

using namespace System;
using namespace System::Windows::Forms;

namespace MyForm
{
	ref class MyForm : public Form
	{
	public:

		MyForm()
		{
			INT32 width = GetSystemMetrics(SM_CXSCREEN);
			INT32 height = GetSystemMetrics(SM_CYSCREEN);
			BOOL big = FALSE;

			this->Width = 1000;
			this->Height = 1000;

			if(this->Width > width || this->Height > Height)
			{
				this->Width = width;
				this->Height = height;
				this->WindowState = FormWindowState::Maximized;
				big = TRUE;
			}

			this->Top = 0;
			this->Left = 0;
			this->StartPosition = FormStartPosition::Manual;
			
			MessageBox::Show((big ? "Your screen is too small to display this window. Resizing and Maximizing." : "Your screen fits this window.")+"\n"+"Width: "+width.ToString()+"px\n Height:"+height.ToString()+"px");
		}
	};
}

int main()
{
	FreeConsole();
	Application::Run(gcnew MyForm::MyForm());
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.