Solved

Drawing inside a control (picturebox) - VC++ 5.0

Posted on 1998-03-18
9
549 Views
Last Modified: 2012-05-05
The following code draws a bunch of solid boxes in a picturebox on a dialog, however the drawing goes outside the bounds of the picture box (and actually draws onto the dialog itself). Why is this? Eventually I want to attach scrollbars to the box to view any drawing that are larger than the box.
 
**************************
void ce05cDialog::OnPaint()
{
      CPaintDC dc(this); // device context for painting
      //Add your message handler code here
      int x1 = 0,y1 = 0,x2 = 7.5,y2 =7.5,i;
      
      CBrush brushsolid1(RGB(255,0,0));
      CBrush brushsolid2(RGB(0,0,255));
      
      CDC* pControlDC = m_picbox1.GetDC();
      
      m_picbox1.Invalidate();
      m_picbox1.UpdateWindow();
      pControlDC->SelectObject(brushsolid1);
      
      for (i = 1; i <=10; i++)
      {      
            pControlDC->Rectangle(x1,y1,x2,y2);
            x1 = x1 + 7.5;
            y1 = y1 + 7.5;
            x2 = x2 +7.5;            
            y2 = y2 +7.5;
      }

      pControlDC->SelectObject(brushsolid2);
      x1 = 0;
      y1 = 40;
      x2 = 7.5;
      y2 =47.5;

      for (i = 1; i <=10; i++)
      {      
            pControlDC->Rectangle(x1,y1,x2,y2);
            x1 = x1 + 7.5;
            y1 = y1 - 7.5;
            x2 = x2 + 7.5;            
            y2 = y2 - 7.5;
      }
      

      m_picbox1.ReleaseDC(pControlDC);
}
0
Comment
Question by:englm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1183628
You created a device context for the dialog window, not the control (Picturebox) window.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183629
Opps I'm wrong!  You are creating a DC for the dialog, but then you create and use a DC for the control, which is correct.

Sorry.


Interesting.  I don't see what is wrong.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183630
Do you know for a fact that the drawing is occuring outside of the control window.  Is it possible that the control window is larger than you think?  (Assuming it doesn't have a visible border that would make this a stupid question?)
0
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!

 
LVL 4

Accepted Solution

by:
tflai earned 50 total points
ID: 1183631
You should try using double-buffering to draw first on a memory DC and flip it on using BitBlt or StretchBlt.  It will not only fix your problem but will also make the drawing much smoother, without flickery and visible drawing sequence.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183632
That will make the drawing appear without flickering, but there is no reason that it should prevent this problem, if fact there is no reason this problem should occur.  A DC created for a window should not be able to draw outside of the window.  If it does there is something wrong.  If you can use the DC to draw a rectangle outside of a window, then you can probably use it to BitBlt() outside of a window.
0
 
LVL 4

Expert Comment

by:tflai
ID: 1183633
Dialog's controls will have CS_PARENTDC as one of its class style's flag, therefore it will draw on parent's DC:
----------from MSVC HELP----------
CS_PARENTDC      Sets the clipping rectangle of the child window to that of the parent window so that the child can draw on the parent. A window with the CS_PARENTDC style bit receives a regular device context from the system's cache of device contexts. It does not give the child the parent's device context or device context settings.
0
 
LVL 4

Expert Comment

by:tflai
ID: 1183634
By the way, BitBlt() will not cause drawing on outside of its bouding rectangle.
0
 
LVL 4

Expert Comment

by:tflai
ID: 1183635
Or if you don't want to bother with double-buffering technique, be sure to set the clipping rectangle of the control window yourself to the control's client boundary.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183636
I see.  I do my own dialogs, so I wasn't aware that dialog controls use the CS_PARENTDC style.  That seems very unfriendly, I guess that must be faster.  However, that means the only reliable way to not draw on the parent is to change the DC's clipping rectangle/region.  Just like the other drawing procedures BitBlt() WILL anywhere inside the DC that you tell it so, so you must either draw only at the coordinates that are inside the control (which can be inconvenient) or you must set the clipping rectangle to the control's coordinates and then draw freely.
0

Featured Post

Independent Software Vendors: 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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

738 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