Solved

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

Posted on 1998-03-18
9
532 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

785 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