Custom Window Class for View Window | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 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

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.

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.