?
Solved

Fill Picture Box Control

Posted on 2006-05-10
16
Medium Priority
?
426 Views
Last Modified: 2013-11-20
I have a custom Active X and on the Property Page, I want to added a picture box.  So I added the picture box and noticed it was of type IDC_STATIC.  So it won't let me add a variable to it.  So I changed the naming to IDC_BOX.  I'm not sure if you can do this, but I did it anyways.  So when I got my variable, in the property page code on init.  I tried setting the color of the picture box, but no luck.  How would I go about this?
0
Comment
Question by:cophi
  • 6
  • 5
  • 4
  • +1
16 Comments
 
LVL 8

Expert Comment

by:mrblue
ID: 16651571
"So I changed the naming to IDC_BOX" - it is corrrect
"I tried setting the color of the picture box, but no luck. " - how exactly ?
0
 

Author Comment

by:cophi
ID: 16651627

      CClientDC dc(this);
   
      CRect Recto;

      evenBox.GetWindowRect(&Recto);
      ScreenToClient(&Recto);

      CBrush BrushColor(color);
      CBrush *pBrush = dc.SelectObject(&BrushColor);

      dc.Rectangle(&Recto);

      dc.SelectObject(pBrush);
0
 
LVL 8

Expert Comment

by:mrblue
ID: 16651758
1. I have derived interface class from CStatic (CMyStatic)

2. I have add handler for WM_PAINT message (OnPaint())

CPaintDC dc(this); // device context for painting
      
HBRUSH hBr = (HBRUSH)GetStockObject(WHITE_BRUSH);
pOldBr = dc.SelectObject(hBr);

CRect rRect;

GetClientRect(&rRect);
dc.Rectangle(&rRect);

dc.SelectObject(pOldBr);

3. I have used Class Wizard to add variable of tyle CMyStatic for picture control
& it works
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:cophi
ID: 16651789
Oh, well I don't have Option1
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16655559
Add a handler for the WM_CTLCOLOR message to either your view or dialog. This message is sent when a control is about to be drawn.

For example:

HBRUSH CCtsDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

     if( nCtlColor == CTLCOLOR_STATIC )  // A static is about to be displayed
    {
        if( pWnd->GetDlgCtrlID() == IDC_BOX )
            pDC->SetBkColor( COLOR(255,255,255) ); // Set background color to WHITE
    }
    return hbr;
}

The example above will change the color for only one static control, whose label is IDC_BOX.

-MAHESH
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16655563
CClientDC dc(this);

This is not picturebox DC. You need to get it's DC to draw in picturebox:

CDC* pDC = evenBox.GetDC();

// draw in pDC

evenBox.ReleaseDC(pDC);

Notice also that drawing should be done in picturebox client coordinates.
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16655592
a bit change not COLOR(255,255,255) but it should RGB(255,255,255)

HBRUSH CCtsDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

     if( nCtlColor == CTLCOLOR_STATIC )  // A static is about to be displayed
    {
        if( pWnd->GetDlgCtrlID() == IDC_BOX )
            pDC->SetBkColor( RGB(255,255,255) ); // Set background color to WHITE
    }
    return hbr;
}

-MAHESH
0
 

Author Comment

by:cophi
ID: 16661200
MAHESH,

I've tried your example.  The SetBkColor does not work, the box is still the default gray.  I know I have the correct object tho because I can do pDC->TextOut(0, 0, "Something");  And it will print out in the box.  So anyideas how to change the color of that box?
0
 
LVL 8

Expert Comment

by:mrblue
ID: 16661603
Have you checked what I suggested ? I am sure it works 'cause I've tested it.
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16665102
cophie,
Have you tried that with pDC->FillRect() or pDC->FillSolidRect() ?

-MAHESH
0
 

Author Comment

by:cophi
ID: 16666753
how would i get the LPCRECT lpRect?
0
 
LVL 8

Expert Comment

by:mrblue
ID: 16667261
GetClientRect(lpRect);          // CWnd class member
or
::GetClientRect(hWnd, lpRect);
0
 
LVL 8

Expert Comment

by:mrblue
ID: 16667445
CRect rRect;

GetClientRect(&rRect);
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16667518
you may also use

LPRECT rect;
GetClientRect(&rect) or GetWindowRect(&rect)..

is FillSolidRect() is working as suggested ?

e.g.
pDC->FillSolidRect(&rect, RGB(255,255,255));

-MAHESH
0
 
LVL 8

Assisted Solution

by:mrblue
mrblue earned 800 total points
ID: 16667917
This is my project (VC++ 6.0). I hope it will be helpfull.

// TEST_EE_PictureBoxDlg.h : header file
//

#if !defined(AFX_TEST_EE_PICTUREBOXDLG_H__DDE6168E_21E3_47E4_B3C7_A89BBA3EE11A__INCLUDED_)
#define AFX_TEST_EE_PICTUREBOXDLG_H__DDE6168E_21E3_47E4_B3C7_A89BBA3EE11A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "MyStatic.h"

/////////////////////////////////////////////////////////////////////////////
// CTEST_EE_PictureBoxDlg dialog

class CTEST_EE_PictureBoxDlg : public CDialog
{
// Construction
public:
      CTEST_EE_PictureBoxDlg(CWnd* pParent = NULL);      // standard constructor

// Dialog Data
      //{{AFX_DATA(CTEST_EE_PictureBoxDlg)
      enum { IDD = IDD_TEST_EE_PICTUREBOX_DIALOG };
      CMyStatic      m_ctrlPicture;                     // <-------- control variable
      //}}AFX_DATA

      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CTEST_EE_PictureBoxDlg)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);      // DDX/DDV support
      //}}AFX_VIRTUAL

// Implementation
protected:
      HICON m_hIcon;

      // Generated message map functions
      //{{AFX_MSG(CTEST_EE_PictureBoxDlg)
      virtual BOOL OnInitDialog();
      afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
      afx_msg void OnPaint();
      afx_msg HCURSOR OnQueryDragIcon();
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_TEST_EE_PICTUREBOXDLG_H__DDE6168E_21E3_47E4_B3C7_A89BBA3EE11A__INCLUDED_)
====================================================================================================
#if !defined(AFX_MYSTATIC_H__73A4FF48_6832_4372_AB35_5D5F925B783F__INCLUDED_)
#define AFX_MYSTATIC_H__73A4FF48_6832_4372_AB35_5D5F925B783F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyStatic.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CMyStatic window

class CMyStatic : public CStatic
{
// Construction
public:
      CMyStatic();

// Attributes
public:

// Operations
public:

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CMyStatic)
      //}}AFX_VIRTUAL

// Implementation
public:
      virtual ~CMyStatic();

      // Generated message map functions
protected:
      //{{AFX_MSG(CMyStatic)
      afx_msg void OnPaint();
      //}}AFX_MSG

      DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYSTATIC_H__73A4FF48_6832_4372_AB35_5D5F925B783F__INCLUDED_)
====================================================================================================
// MyStatic.cpp : implementation file
//

#include "stdafx.h"
#include "TEST_EE_PictureBox.h"
#include "MyStatic.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyStatic

CMyStatic::CMyStatic()
{
}

CMyStatic::~CMyStatic()
{
}


BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
      //{{AFX_MSG_MAP(CMyStatic)
      ON_WM_PAINT()
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyStatic message handlers

void CMyStatic::OnPaint()
{
      CPaintDC dc(this); // device context for painting
      
      HBRUSH hBr = (HBRUSH)GetStockObject(WHITE_BRUSH);
      dc.SelectObject(hBr);

      CRect rRect;

      GetClientRect(&rRect);
      dc.Rectangle(&rRect);
}
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 1200 total points
ID: 16669527
Following is working at my end....NO need to derive your own control..using pDC->FillSolidRect() and WM_CTLCOLOR
just put following on WM_CTLCOLOR handler...

HBRUSH CClDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

     if( nCtlColor == CTLCOLOR_STATIC )  // A static is about to be displayed
    {
        if( pWnd->GetDlgCtrlID() == IDC_MYYSTATIC) // Give Control ID of your static picturebox control
          {
           CRect rect;
           pWnd->GetClientRect(&rect);
           pDC->FillSolidRect( rect,RGB(255,255,255) ); // Set background color to WHITE
          }
    }
       // TODO: Return a different brush if the default is not desired
      return hbr;
}

-MAHESH
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question