MFC Background color

asked 2023-01-09T20:13:23.133+00:00
bodhisatwa bhattacharya 1 Reputation point

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.
1,890 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.
2,617 questions
{count} votes

2 answers

Sort by: Most helpful
  1. answered 2023-01-12T03:37:48.4066667+00:00
    bodhisatwa bhattacharya 1 Reputation point

    Hi,

    Can anyone help me to solve out this problem. I think DavidLowndes-6766 not understand the problem. Still, I get same message ""unable to read memory". And background color is not changed.

    Thanks


  2. answered 2023-01-12T10:45:42.6666667+00:00
    bodhisatwa bhattacharya 1 Reputation point

    Hi Viorel,

    Thanks for Text.

    If I define

    HBRUSH hredBrush = ::CreateSolidBrush(RGB(255, 200, 20));

    then

    HBRUSH hredBrush = ::CreateSolidBrush(RGB(255, 200, 20));   
        CString MyWindowClass = AfxRegisterWndClass(  
                              CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW ,  
                              AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING),  
                               hredBrush,                                                                          
                              AfxGetApp()->LoadStandardIcon(IDI_EXCLAMATION));  
    
        Create(MyWindowClass, _T("Main Window A"), WS_OVERLAPPEDWINDOW);  
         ::DeleteObject(hredBrush); 
    

    hredBrush is not able to change background color ,due to <Unable to read memory>

    If I put only "::CreateSolidBrush(RGB(255, 200, 20))", insert of hredBrush in my code is work fine.

    Can You Please tell me why this is happening?