본문 바로가기
Programming/Window Programming

[MFC] Picture Control Background Color

by BadaGreen_Kim 2019. 7. 3.

 

 

이것때문에 몇번을 고생했는지 모른다.  MFC Dialog를 이용할때 프로그램이 시작될때 OnPaint 부분에 초기값을 설정해놓으면 Color를 변경할수 있다.

 

참고로 여러 색상을 이용하기 위해 color.h 헤더파일을 만들어서 색상을 미리 정의 하였다.

 

color.h

#define RED        RGB(127,  0,  0)
#define GREEN      RGB(  0,127,  0)
#define BLUE       RGB(  0,  0,127)
#define LIGHTRED   RGB(255,  0,  0)
#define LIGHTGREEN RGB(  0,255,  0)
#define LIGHTBLUE  RGB(  0,  0,255)
#define BLACK      RGB(  0,  0,  0)
#define WHITE      RGB(255,255,255)
#define GRAY       RGB(192,192,192)

 

 

Dlg.cpp

 

void CMFCVIEWERDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 그리기를 위한 디바이스 컨텍스트입니다.

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 클라이언트 사각형에서 아이콘을 가운데에 맞춥니다.
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 아이콘을 그립니다.
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{

		CDialogEx::OnPaint();

		/*CDC* p = m_View.GetWindowDC();

		p->SetBkColor(BLACK_BRUSH);
		on
		p->Rectangle(200, 100, 300, 200);*/

		CRect rect;
		GetClientRect(&rect);
		CBrush myBrush(LIGHTRED); // dialog background color <- 요기 바꾸면 됨.

		CDC* pDC = m_View.GetWindowDC();
		CBrush *pOld = pDC->SelectObject(&myBrush);
		BOOL bRes = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
		pDC->SelectObject(pOld); // restore old brush
	

		
	}
}
<

 

결과.