2011-10-01 6 views
4

나는 쉽게 글꼴 크기를 변경할 수 있기를 원하는 CListCtrl 클래스가 있습니다. MyListControl로 CListCtrl을 서브 클래 싱했습니다. PreSubclassWindow 이벤트 처리기에서이 코드를 사용하여 글꼴을 성공적으로 설정할 수 있습니다.MFC : 동적으로 제어 글꼴 크기를 변경합니까?

void MyListControl::PreSubclassWindow() 
{ 
    CListCtrl::PreSubclassWindow(); 

    // from http://support.microsoft.com/kb/85518 
    LOGFONT lf;      // Used to create the CFont. 

    memset(&lf, 0, sizeof(LOGFONT)); // Clear out structure. 
    lf.lfHeight = 20;     // Request a 20-pixel-high font 
    strcpy(lf.lfFaceName, "Arial"); // with face name "Arial". 
    font_.CreateFontIndirect(&lf); // Create the font. 
    // Use the font to paint a control. 
    SetFont(&font_); 
} 

이것은 작동합니다. 그러나, 내가하고 싶은 것은 SetFontSize (int 크기)라는 메서드를 만드는 것입니다.이 메서드는 기존 글꼴 크기를 그대로 변경합니다 (얼굴과 다른 특성을 그대로 남겨 둡니다). 그래서 나는 (이 내 프로그램을 죽이는)이 방법은 기존의 글꼴을 가져온 다음 글꼴 크기 그러나 이것은 실패 할 내 시도를 변경해야 믿습니다

void MyListControl::SetFontSize(int pixelHeight) 
{ 
    LOGFONT lf;      // Used to create the CFont. 

    CFont *currentFont = GetFont(); 
    currentFont->GetLogFont(&lf); 
    LOGFONT lfNew = lf; 
    lfNew.lfHeight = pixelHeight;     // Request a 20-pixel-high font 
    font_.CreateFontIndirect(&lf); // Create the font. 

    // Use the font to paint a control. 
    SetFont(&font_); 

} 
나는이 방법을 만드는 방법을

?

+0

이 방법으로 프로그램을 "종료"합니까? – Jollymorphic

+0

실패한 mfc ASSERT가 있습니다. – User

답변

3

해결책을 찾았습니다. 나는 개선을위한 제안에 열려있어 :

void MyListControl::SetFontSize(int pixelHeight) 
{ 
    // from http://support.microsoft.com/kb/85518 
    LOGFONT lf;      // Used to create the CFont. 

    CFont *currentFont = GetFont(); 
    currentFont->GetLogFont(&lf); 
    lf.lfHeight = pixelHeight; 
    font_.DeleteObject(); 
    font_.CreateFontIndirect(&lf); // Create the font. 

    // Use the font to paint a control. 
    SetFont(&font_); 
} 

두 개의 키를 일이 점점로했다 :

  1. LOGFONT, lfNew의 사본을 제거.
  2. 새 글꼴을 만들기 전에 font_.DeleteObject();을 호출하십시오. 이미 존재하는 글꼴 개체가 이미있을 수는 없습니다. 기존 포인터를 검사하는 MFC 코드에는 ASSERT가 있습니다. 그 ASSERT가 내 코드를 실패하게 만들었다.
+3

font_ 무엇입니까? 그것은 선언되지 않았습니다. –

+0

font_는 CFont 유형이며 MyList MyList의 멤버 변수입니다. – User

관련 문제