1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

customize background mfc dlg

Discussion in 'MFC' started by fenomeno83, Apr 14, 2006.

  1. fenomeno83

    fenomeno83 New Member

    Joined:
    Apr 14, 2006
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    how can i customize background of mfc dialog box?
    a simple example?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Just drawing on the dc in the PAINT method will do your job.

    Here is the default PAINT method of the default dialog based MFC
    Code:
    	if (IsIconic())
    	{
    		CPaintDC dc(this); // device context for painting
    
    		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
    
    		// Center icon in client rectangle
    		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;
    
    		// Draw the icon
    		dc.DrawIcon(x, y, m_hIcon);
    	}
    	else
    	{
    		CDialog::OnPaint();
    	}
    
    Just replace it with
    Code:
    	if (IsIconic())
    	{
    		CPaintDC dc(this); // device context for painting
    
    		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
    
    		// Center icon in client rectangle
    		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;
    
    		// Draw the icon
    		dc.DrawIcon(x, y, m_hIcon);
    	}
    	else
    	{
    		CPaintDC dc(this); // device context for painting
    
    		CRect rc;
    		GetClientRect(&rc);
    		dc.FillSolidRect(rc,RGB(98,84,84));
    		
    		CDialog::OnPaint();
    	}
    
    We just added the following lines
    Code:
    		CPaintDC dc(this); // device context for painting
    
    		CRect rc;
    		GetClientRect(&rc);
    		dc.FillSolidRect(rc,RGB(98,84,84));
    
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Sample attached in reply to your other thread [thread=726]customize font mfc dlg[/thread]
     
  4. fenomeno83

    fenomeno83 New Member

    Joined:
    Apr 14, 2006
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Thanks
     
    Last edited: Apr 19, 2006
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    My pleasure.
     

Share This Page