Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1998-03-18
9
Medium Priority
?
567 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Accepted Solution

by:
tflai earned 150 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

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.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

971 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