Loading a FormView based on the Screen Resolution | CodeGuru

Loading a FormView based on the Screen Resolution

–> When you generate a formview based application or generally when you are using formview derived class, you will find this piece of code. CTempView::CTempView() : CFormView(CTempView::IDD) { //{{AFX_DATA_INIT(CTempView) //}}AFX_DATA_INIT } Now depending upon the resolution you want to load the resource i.e. for 1024×768 you want to load CTempView::IDD and for 800×600 you want […]

Written By
CodeGuru Staff
CodeGuru Staff
Dec 9, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

–>

When you generate a formview based application or generally when you are using formview derived class, you will find this piece of code.

CTempView::CTempView()
 : CFormView(CTempView::IDD)
{
 //{{AFX_DATA_INIT(CTempView)
 //}}AFX_DATA_INIT
}

Now depending upon the resolution you want to load the resource i.e. for 1024×768 you want to load CTempView::IDD and for 800×600 you want to load CTempView::IDD1. If you try to write the code for selecting the resource directly in the constructor i.e. something like this

CTempView::CTempView()
 {
    /*logic to find the resolution, bResol will be true if it is 1024x768*
    if(bResol)
        id = CTempView::IDD;
   else
        id = CTempView::IDD1;

  // Call the formview costructor here.
  CFormView(id);

 //{{AFX_DATA_INIT(CTempView)
 //}}AFX_DATA_INIT

}
!-- end the block of source code -->

The Compiler will crib and it will not compile.

The solution is to have a global object which gets constructed before the formview and assign a global variable with the appropriate resource id, so that it can be used in the FormView constructor. The code looks something like this.

static int g_ID;
static struct _stDummy
{
 _stDummy()
 {
  /* logic to find the resolution *
  int x = GetSystemMetrics(SM_CXSCREEN);
  if(x == 1024)
   g_ID = CTempView::IDD;
  else /* for 800x600 and any other resolution*
   g_ID = CTempView::IDD1;
 }
}g_Dummy; /* dummy object *

IMPLEMENT_DYNCREATE(CTempView, CFormView)
CTempView::CTempView() :CFormView(g_ID)
{
 //{{AFX_DATA_INIT(CTempView)
 //}}AFX_DATA_INIT
}
!-- end the block of source code -->

Now the g_Dummy object gets constructed first and your logic for selecting the resource id is done here and the id is stored in g_ID which is later being used in the formview constructor. Now the compiler works happily and so does the code.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.