상세 컨텐츠

본문 제목

MFC C++ 객체 색상 OnCtlColor를 이용한 변경

프로그래밍 관련/MFC

by AlrepondTech 2017. 9. 6. 15:11

본문

반응형

 

 

 

 

=================================

=================================

=================================

 

 

 

 

 

 
 
 
MFC에서 Dialog의 기본 배경색은 회색이므로.. 그리고 다른 컨트롤들도 배경색이 구리다.
 
바꾸는법
 
클래스 위저드실행해서 메시지탭에서
WM_CTLCOLOR의 메시지 맵을 추가 한다.
그러면 자동적으로 
 
 HBRUSH Cxxxxxx::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 가 생성됨.
 
거기에 코딩..
 HBRUSH Cxxxxxx::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
 
// TODO:  Change any attributes of the DC here
switch(nCtlColor){
case CTLCOLOR_DLG:   /// 다이얼로그 배경색을 white로.
{
        return (HBRUSH)GetStockObject(WHITE_BRUSH);
        break;
}
case CTLCOLOR_BTN :    // 버튼의 배경색을 투명으로...
{
        pDC->SetBkMode(TRANSPARENT);
                  return (HBRUSH)::GetStockObject(NULL_BRUSH);
        }
         case CTLCOLOR_STATIC:
        {
               pDC->SetTextColor(RGB(0,255,255));  // static text 글자색 변경
                 pDC->SetBkMode(TRANSPARENT);   // static text 배경색 투명
                 return (HBRUSH)::GetStockObject(NULL_BRUSH);
        }
}
// TODO:  Return a different brush if the default is not desired
return hbr;
}
 보면 알겠지만 switch 문으로 구분해서.. 버튼인경우 다이얼로그인경우... 등등을 나눠서 처리아래 define를 보고 원하는거 갖다 쓰면 됨..
   이런식으로 원하는 컨트롤의 색상만 변경할수도 있다.
 if(pWnd->GetDlgCtrlID() == 리소스아이디){ pDC->SetTextColor(RGB(0,255,255));pDC->SetBkMode(TRANSPARENT);return (HBRUSH)::GetStockObject(NULL_BRUSH);}
     간혹 라디오버튼, 체크박스등 의 컨트롤은 투명이 적용이 안돼는 경우가 있다.
XP테마의 버그라고 한다.
 
해결 방법은 
#pragma comment(lib, "UxTheme.lib") 를 추가하고, 
 
다이얼로그 초기화(Initdialog)에서 

SetWindowTheme(컨트롤변수.m_hWnd, L"", L""); 

 
를 추가 해준다.   컨틀롤과 브러시는 아래와 같이 정의되어 있습니다.

#define CTLCOLOR_MSGBOX         0
#define CTLCOLOR_EDIT           1
#define CTLCOLOR_LISTBOX        2
#define CTLCOLOR_BTN            3
#define CTLCOLOR_DLG            4
#define CTLCOLOR_SCROLLBAR      5
#define CTLCOLOR_STATIC         6
#define CTLCOLOR_MAX            7


#define WHITE_BRUSH         0
#define LTGRAY_BRUSH        1
#define GRAY_BRUSH          2
#define DKGRAY_BRUSH        3
#define BLACK_BRUSH         4
#define NULL_BRUSH          5
#define HOLLOW_BRUSH        NULL_BRUSH
#define WHITE_PEN           6
#define BLACK_PEN           7
#define NULL_PEN            8
#define OEM_FIXED_FONT      10
#define ANSI_FIXED_FONT     11
#define ANSI_VAR_FONT       12
#define SYSTEM_FONT         13
#define DEVICE_DEFAULT_FONT 14
#define DEFAULT_PALETTE     15
#define SYSTEM_FIXED_FONT   16









출처 : http://phiru.tistory.com/73
출처 : http://uglytree.tistory.com/144

 

 

=================================

=================================

=================================

 

 

 

반응형


관련글 더보기

댓글 영역