Custom Window Class for View Window

Often I have been coding away and realized that to get exactly the behavior I would like I would need to change the Window class for my View window. Upon looking into the Developer Studio help file, and perusing the help to AfxRegisterClass, I'd realize that this could be a bit more work than one would originally think. The most recent time I went to do this I came across Zafir Anjum's example "Titletip for individual cells" in the ListView section of this code repository. Looking at what he had done for his custom window classes I realized that if I could get the class info for the default window class of the MFC View class, it would be trivial to change the settings of said class and reregister it under a new name. This is what I came up with.
#define CUSTOM_CLASSNAME _T("YourCustomClassName")

BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{

   // modify window styles and such here
   cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);          
 
   // call base class PreCreateWindow to get the cs.lpszClass filled in with the MFC default class name
   if( !CView::PreCreateWindow(cs) )
     return 0;

   // Register the window class if it has not already been registered.
   
   WNDCLASS wndcls;
   HINSTANCE hInst = AfxGetInstanceHandle();

   if(!(::GetClassInfo(hInst, CUSTOM_CLASSNAME, &wndcls)))      // check if our class is registered
   {
     if(::GetClassInfo(hInst, cs.lpszClass, &wndcls))           // get default MFC class settings 
     {
	wndcls.lpszClassName = CUSTOM_CLASSNAME;                // set our class name

	wndcls.style |= CS_OWNDC;			        // change settings for your custom class
        wndcls.hbrBackground = NULL;

        if (!AfxRegisterClass(&wndcls))                         // register class
          AfxThrowResourceException();				// could not register class
     }
     else
       AfxThrowResourceException();				// default MFC class not registered
    }

    cs.lpszClass = CUSTOMVIEWCLASSNAME;                         // set our class name in CREATESTRUCT

    return 1;                                                   // we're all set
}

Date Posted: 6/24/98
Posted by: Pat Laplante.



Comments

  • Fantastic!

    Posted by Legacy on 07/30/2003 12:00am

    Originally posted by: Beau Jackson

    This is exactly what I was looking for - MFC dynamically generated window class names were giving me fits because I had to make a call to FindWindow() and you have to provide the class name.

    Thanks!

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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds