Thread: MSCV++ 4.0 - Dialog Boxes and SelectObject

  1. #1
    Registered User
    Join Date
    Oct 2002

    MSCV++ 4.0 - Dialog Boxes and SelectObject

    Having built a 'std' SDI program, I was wanting to control the font & cursor for the dialog. S ofar, the only help /code I can find always relates to the primary apps "ondraw" function. Is there an alternative to use SelectObject or get the device context?

    I have snipped a piece of code I've been working with ....
    // CDCTestFont3App commands
    BOOL CAboutDlg::OnInitDialog() 
    // In this situation - what would be the code to handle resources correctly using
    // SelectObject()  - I'm presuming it is neccesary and that a CFont *oldfont would
    // also be required. (The book i'm reading warns about resource leaks in this situation.
    // The problem is determining what device context would be used to handle
    // SelectObject for the dialog box. - is it: this.SelectObject, or is some form
    // of assignment of a type via a pointer required?
    LOGFONT fontdata;  										// Create the LOGFONT structure...
    ZeroMemory( &fontdata, sizeof(LOGFONT) );
    GetDlgItem(IDOK)->GetFont()->GetLogFont(&fontdata); // Copies font info into fontdata
    fontdata.lfWeight = FW_SEMIBOLD; 				// Set the weight of the font
    lstrcpy(fontdata.lfFaceName, "Arial Narrow"); 	// Change the name of the font 
    new_font.CreateFontIndirect(&fontdata); 	// Create a new font with the specified LOGFONT parameters 
    GetDlgItem(IDOK)->SetFont(&new_font); 	// Assign the font to the widget 
    // If attempt to use same font again it would fail as font create already 
    // need to have individual font for each style to be used.
    ZeroMemory( &fontdata, sizeof(LOGFONT) );
    fontdata.lfWeight = FW_THIN;											// THIN TEXT
    fontdata.lfItalic = TRUE;
    lstrcpy(fontdata.lfFaceName, "Comic Sans MS");
        CWinApp* pApp = AfxGetApp();
    if( pApp != 0 )
        m_hIconOkay = pApp->LoadIcon( IDI_GreenIcon );
        m_hIconCancel = pApp->LoadIcon( IDI_RedIcon );
        m_AboutOK.SetIcon( m_hIconOkay );
        m_AboutCancel.SetIcon( m_hIconCancel );
    return TRUE;  // return TRUE unless you set the focus to a control
    	              // EXCEPTION: OCX Property Pages should return FALSE
    Code tags added by Kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    cout << "No code tags are bad";

    With Code Tags:
    for(int i=0;i<5;i++)
         cout << "This code is easy to read";
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.

    Good Luck,

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed