Custom Window Class for View Window
Posted
by Tyler Bindon
on August 6th, 1998
#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:00amOriginally 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