Device context
-
What is needed to draw on a device context CDC dc; dc.LineTo(10,10); i'm new to this so how should this peace of code look like ... what am i missing
-
What is needed to draw on a device context CDC dc; dc.LineTo(10,10); i'm new to this so how should this peace of code look like ... what am i missing
Well where are you getting the DC from?? Typically if you use an override of the virtual CView::OnDraw(CDC* pDC) method, the framework provides you the device context. From there you simply use it! CMyView::OnDraw(CDC* pDC) { pDC->MoveTo(50, 50); // Line start pDC->LineTo(10, 10); // Line end pDC->Ellipse(0,0,100,100); // Draw a circle.
-
Well where are you getting the DC from?? Typically if you use an override of the virtual CView::OnDraw(CDC* pDC) method, the framework provides you the device context. From there you simply use it! CMyView::OnDraw(CDC* pDC) { pDC->MoveTo(50, 50); // Line start pDC->LineTo(10, 10); // Line end pDC->Ellipse(0,0,100,100); // Draw a circle.
isn't it possible to create my own device context to play around with ... in any member function void MyClass::myfunc(int i,int a) { CDC dc; dc.LineTo(10,10); }
-
isn't it possible to create my own device context to play around with ... in any member function void MyClass::myfunc(int i,int a) { CDC dc; dc.LineTo(10,10); }
Usually, you will want to draw in a window's device context. As Paul said above, you will get such a DC in handlers like OnPaint() or in OnDraw() for views. If you have a window, you can get the window's DC using GetDC(), draw into it, then use ReleaseDC(). As a rule of thumb, at least for the beginning, try to keep the painting stuf in OnDraw(). You can call from there helper functions, giving them a pointer to the provided DC (pDC is fine). Also, you can create a "memory DC", which basically behaves like a normal DC, but you will not see the results. However, you can copy (blit) the contents of such a DC into a normal window DC. Also, you can use a memory DC to draw on a bitmap. For example: CDC dc; dc.CreateCompatibleDC(NULL); // creates a mem DC compatible with the display. ... // paint here, etc. dc.DeleteDC(); Other device context are related to printers, metafiles, etc.
-
Usually, you will want to draw in a window's device context. As Paul said above, you will get such a DC in handlers like OnPaint() or in OnDraw() for views. If you have a window, you can get the window's DC using GetDC(), draw into it, then use ReleaseDC(). As a rule of thumb, at least for the beginning, try to keep the painting stuf in OnDraw(). You can call from there helper functions, giving them a pointer to the provided DC (pDC is fine). Also, you can create a "memory DC", which basically behaves like a normal DC, but you will not see the results. However, you can copy (blit) the contents of such a DC into a normal window DC. Also, you can use a memory DC to draw on a bitmap. For example: CDC dc; dc.CreateCompatibleDC(NULL); // creates a mem DC compatible with the display. ... // paint here, etc. dc.DeleteDC(); Other device context are related to printers, metafiles, etc.
thank you all for your replies ... all i wanted to do is have a memory DC on which i would put a bitmap and manipulate it around and then put it on the device context i get in the OnDraw() ... the problem i ran into was that the memory context is 1x1 (as it should be i guess) but how can i do LineTo or FloodFill if it is of that size
-
isn't it possible to create my own device context to play around with ... in any member function void MyClass::myfunc(int i,int a) { CDC dc; dc.LineTo(10,10); }
-
thank you all for your replies ... all i wanted to do is have a memory DC on which i would put a bitmap and manipulate it around and then put it on the device context i get in the OnDraw() ... the problem i ran into was that the memory context is 1x1 (as it should be i guess) but how can i do LineTo or FloodFill if it is of that size
You have to create a bitmap and select it in the memory DC first. Once you selected your bitmap, all drawing changes it directly. See also the GDI section for examples of usage.