Solved

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

Posted on 1998-03-18
9
540 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

829 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