MFC Background color

bodhisatwa bhattacharya 21 Reputation points
2023-01-09T20:13:23.133+00:00

hi,

I have a question about the background color of MFC.

My code is as follows


Mainfrm.h


class C_MainFrame : public CFrameWnd
{

public:
C_MainFrame(); // Declear Constructor

};


Mainfrm.cpp


include "MFCAppRun.h"

include "MainFrm.h"

// CMainFrame

//Define Constructor

C_MainFrame::C_MainFrame() {

    HBRUSH hredBrush = ::CreateSolidBrush(RGB(255, 200, 20));   
    CString MyWindowClass = AfxRegisterWndClass(  
                          CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW ,  
                          AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING),  
                           hredBrush,                                                                        //"unable to read memory".  
                          AfxGetApp()->LoadStandardIcon(IDI_EXCLAMATION));  

    Create(MyWindowClass, _T("Main Window A"), WS_OVERLAPPEDWINDOW);  
     ::DeleteObject(hredBrush);    

}

when I use hredBrush as parameter of AfxRegisterWndClass,It shows me "unable to read memory".

but when i use ::CreateSolidBrush(RGB(255, 200, 20)) as parameter of AfxRegisterWndClass it works fine.

I means refer the below code:

CString MyWindowClass = AfxRegisterWndClass(
CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW ,
AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING),
::CreateSolidBrush(RGB(255, 200, 20)), // work fine
AfxGetApp()->LoadStandardIcon(IDI_EXCLAMATION));

    Create(MyWindowClass, _T("Main Window A"), WS_OVERLAPPEDWINDOW);  

Can Anyone please help me to understand why this is happening?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,411 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,520 questions
{count} votes

Accepted answer
  1. SM 416 Reputation points
    2023-02-02T21:11:15.6266667+00:00

    As per documentation ,
    "The system automatically deletes class background brushes when the class is unregistered by using UnregisterClass. An application should not delete these brushes.".

    Remove the call to ::DeleteObject(hredBrush);.

    -SM

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful