Paint background in mfc

I am experimenting with drawing a window background in C ++ using the MFC library. I am tasked with using this structure because I am working on an MFC application. I tried several different methods, but can't make it work. So I recently opened an empty project and just want to understand how to draw a background, but it does not work. Any help would be great. Here is my code ...

class CExerciseApp : public CWinApp
{   
     //a pointer to our window class object
     Basic_Window *bwnd; 

     BOOL InitInstance()
     {  
         bwnd = new Basic_Window();
         m_pMainWnd = bwnd;
         bwnd->ShowWindow(1);

         HWND hWnd = GetActiveWindow();

         CRect drawing_area;
         GetClientRect(hWnd, &drawing_area);

         CBrush newBrush;
         newBrush.CreateSolidBrush(RGB(255,255,255));

         CDC* dc = bwnd->GetDC();
         dc->FillRect(&drawing_area, &newBrush);
         bwnd->RedrawWindow();
         return TRUE;
    }    
};  
+2
source share
1 answer

From my own post fooobar.com/questions/1691633 / ... I can guarantee that I did this work. I used this approach to implement themes / skins in a commercial application.

You need to add the OnCtlColor method to the Basic_Window class. In the .h file, add the Basic_Window class:

const CBrush m_BackgroundBrush;

and

afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor);

.cpp

Basic_Window::Basic_Window()
: m_BackgroundBrush(RGB(255,255,255))
{
//...
}

HBRUSH Basic_Window::OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    if(some_exception)
        return __super::OnCtlColor( pDC, pWnd, nCtlColor);

    return (HBRUSH) m_BackgroundBrush.GetSafeHandle();
}

some_exception , , . , , nCtlColor.

ON_WM_CTLCOLOR() .

+1

Source: https://habr.com/ru/post/1691628/


All Articles