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 1024x768 you want to load CTempView::IDD and for 800x600 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.



Comments

  • amazing

    Posted by Karsten Schmidt on 11/19/2012 05:18am

    thats exactly what I have searched, and it still works on vs 2010 14years later, great job!

    Reply
  • That is what I was looking

    Posted by Macky on 01/15/2005 10:47am

    Thanks

    Reply
  • I looking for This...Cool(No contents)

    Posted by Legacy on 04/23/2002 12:00am

    Originally posted by: Grassh

    ..

    Reply
  • How to have the controls laid out more neatly?

    Posted by Legacy on 07/02/2001 12:00am

    Originally posted by: Muralia Dhar

    Can anyone please tell me how to make a view class (derivd from CFormView perhaps) that looks more organized, like an HTML form? normally the dialog box template that the cformview uses is just put there on the left side. how to make it "centered" and "spread out"? basically i want to have my controls laid out more neatly. Please help!!!
    Thanks...

    Reply
  • Please check the comments

    Posted by Legacy on 12/08/2000 12:00am

    Originally posted by: J.L. TRYOEN

    the comment : use a function call in the initializer instead of a global. - Joe Shapiro (1998/12/10)
    is not more valid

    Reply
  • Re: Bitmaps in menu are not created at startup when a document is not first loaded in MDI Frame

    Posted by Legacy on 04/26/1999 12:00am

    Originally posted by: Brent Corkum

    You must no be adding the icons to your default menu. You have to add the icons to BOTH the mainframe menu and the default menu (see the example). You'll notice that if you add this code to the example that comes with the class that you do not get a document and you get the bitmaps in the default menu.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • When it comes to desktops – physical or virtual – it's all about the applications. Cloud-hosted virtual desktops are growing fast because you get local data center-class security and 24x7 access with the complete personalization and flexibility of your own desktop. Organizations make five common mistakes when it comes to planning and implementing their application management strategy. This eBook tells you what they are and how to avoid them, and offers real-life case studies on customers who didn't let …

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds