CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2006
    Posts
    23

    MFC Picture Control W/ Bitmap Mayhem.

    Ok.. My problem is kind of screwed up, I'm not even sure if there is a solution available for this. If there isn't, I'd like to know any alternative way of doing this.

    I placed a Picture Control using resource compiler and using that, I was able to load a BMP or JPEG file on it. Here is the problem - I'd like to draw rectangles ON TOP of the picture that has been loaded. Let me show you my OnPaint function to see what I have attempted so far.

    Code:
    void CMy2d_3d_applicationDlg::OnPaint()
    {
    	CPaintDC dc(this); // device context for painting
    	
    	CRect RectColorChanger;
    
    	// Get the location and dimensions of the control
    	m_picCtrl.GetWindowRect(&RectColorChanger);
    	CBrush BrushRed(RGB(255, 55, 5));
    
    	// Select the new brush
    	CBrush *pOldBrush = dc.SelectObject(&BrushRed);
    
    	// Convert the current coordinates from Screen to Client
    	ScreenToClient(&RectColorChanger);
    	// Change the background of the control
    	dc.Rectangle(RectColorChanger);
    	
    	// Restore the old brush
    	dc.SelectObject(pOldBrush);
    
    	if (IsIconic())
    	{
    		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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();
    	}
    }
    This code is supposed to paint the whole background of the picture control with color red and it works - only if the type of the picture control is set to frame. Well, if I set the type to frame, then I can't load the JPEG or BMP files onto the picture control anymore. On the other hand, if I change the type of picture control into bitmap, then the picture loads properly - only that I will not be able to use device context to create any rectangles or circles.

    Does anyone have bright ideas to overcome this problem?

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: MFC Picture Control W/ Bitmap Mayhem.

    Quote Originally Posted by l46kok
    Does anyone have bright ideas to overcome this problem?
    Sure. Many ideas, all of them are bright
    1. You should only paint your window in *its own* OnPaint(), NOT its parent!
    2. Your picture control is a separate window. It might share its parent DC or have its own. You should not assume that it will share DC. Generally child windows are clipped from the parent DC so painting under them makes no sense, and has no effect.
    3. If you want to draw ON TOP of the picture, you should let the control do its thing first (in WM_PAINT handler), then draw whatever; not the other way around.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Oct 2006
    Posts
    23

    Re: MFC Picture Control W/ Bitmap Mayhem.

    Ok, so if I understood you correctly, you want me to generate a class for the picture control, map it with OnPaint function and draw the rectangles there?

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: MFC Picture Control W/ Bitmap Mayhem.

    Quote Originally Posted by l46kok
    Ok, so if I understood you correctly, you want me to generate a class for the picture control, map it with OnPaint function and draw the rectangles there?
    Yes, please.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Join Date
    Oct 2006
    Posts
    23

    Re: MFC Picture Control W/ Bitmap Mayhem.

    Strange.. It didn't work either in Frame type or Bitmap type. Here's what I have for the Picture Control Class

    Code:
    // PictureCtrl.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "2d_3d_application.h"
    #include "PictureCtrl.h"
    
    
    // PictureCtrl dialog
    
    IMPLEMENT_DYNAMIC(PictureCtrl, CDialog)
    
    PictureCtrl::PictureCtrl(CWnd* pParent /*=NULL*/)
    	: CDialog(PictureCtrl::IDD, pParent)
    {
    
    }
    
    PictureCtrl::~PictureCtrl()
    {
    }
    
    void PictureCtrl::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    }
    
    
    BEGIN_MESSAGE_MAP(PictureCtrl, CDialog)
    	ON_WM_PAINT()
    END_MESSAGE_MAP()
    
    void PictureCtrl::OnPaint()
    {
    	CPaintDC dc(this);
    
    	CRect RectColorChanger;
    
    	// Get the location and dimensions of the control
    	this->GetWindowRect(&RectColorChanger);
    	CBrush BrushRed(RGB(255, 55, 5));
    
    	// Select the new brush
    	CBrush *pOldBrush = dc.SelectObject(&BrushRed);
    
    	// Convert the current coordinates from Screen to Client
    	ScreenToClient(&RectColorChanger);
    	// Change the background of the control
    	dc.Rectangle(RectColorChanger);
    	
    	// Restore the old brush
    	dc.SelectObject(pOldBrush);
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured