|
I use your CMemDC for years now and I never thanked you:
Merci, Danke, Sposibo, Euxaristw, Be in good health and happy !
|
|
|
|
|
All the people on CP are using this for very simple drawings indeed, and it's true that using update regions, clipping and intersecting rects and regions, resizing, combined with coordinates transforms, lack of generalized methods to do this right, and eventually MFC bugs workaround internally by just redrawing the controls, put a serious burden over the programmer's shoulder who otherwise would waist a lot of time for apparently a very simple problem. The good sollution using the mentioned methods is really missing on CP and in microsoft samples and documentation. Take a professional image application, load and move a picture. Then create a simple MFC project, use CMemDC and try moving the same picture.
I'm aware that almost all GUI projects are using this or variants derived from it, after this article appeared, however I would rather vote 3 for the coziness, knowing it's just for very very simple drawings indeed, no scrolling, no resizing, no zooming.
gugul
|
|
|
|
|
I agree that CMemDC isn't intended for highly optimized drawing. I have used it in scrolling applications and resizing applications, but some additional work is probably required by the programmer. Having said that, I’d still point out that there is a lot of value in simple solutions to difficult problems -- even when they don't cover every case.
Your comments sound to me like you have some specific changes you'd make to CMemDC. If so, I'd be very happy to evaluate them and update this article with your modifications. Alternatively there are probably many people who would value your solution to this gnarly problem. Please feel free to contribute an article on this topic to CodeProject.
|
|
|
|
|
|
This class is simply greate. It saved me a lot of time.
Anyway I'm experiencing this small problem when used in CListView-derived class: When I hold the PageDown key in order to scroll down, the items' labels are getting somehow "cut" and aren't drawn in their proper height. When I drag the vertical scrollbar, it is without problems.
Will someone help please?
|
|
|
|
|
This solved flicker problem, however, sometimes the text line is not fully pained and very reproducable. I am using MM_HIENGLISH mode. To me it seems a rounding issue from LP to DP, but not sure. Any advice?
|
|
|
|
|
Plugged right in, worked the first time and I didn't have to muck my code at all.
Thanks!!
|
|
|
|
|
Background:
I am new to MFC, and WinCE, and eVC (makes for a nice challenge it seems!)!
My Project:
I am trying to build an HMI that displays red, green or yellow circles (Ellipse!) over a bitmap background, switching colour in accordance with changing values (that code sorted-ish).
My eVC project is currently Dialogue-based. Wise?
I have a memDC with a bitmap selected. This BitBlt's OK on to the screen DC in OnPaint().
I then added Ellipse drawing code to OnPaint but this failed deep in MFC: "Assertion Failed in Wingdi".
So (and this is a better plan anyways!), I tried to do everything in the single memDC: ie. Select a bitmap (as background image) then overlay it with a coloured Ellipse or lines.
The code runs OK, but only the bitmap is displayed, no ellipse.
Is it possible to do a ".Select bitmap" followed by ".Select Brush" and maintain all elements in the memDC?
Are the coordinates different?
Do bitmaps and ellipse need separate DC's? How could these be combined on the dialogue DC?
Any assistance appreciated! Tight schedule busting at seams!
Regards
Jack
NON-WORKING PSEUDO CODE with stuff I tried...
=================================================
MyDialog::OnInitDialog...
m_bmp_BackDrop.LoadBitmap(IDB_APP_grey); // load image
// device context of client area of windows (dialogue!Ok?)
CClientDC dc(this);
// create memory device context to hold image
m_memDC_BackDrop.CreateCompatibleDC(&dc);
m_memDC_BackDrop.SelectObject( &m_bmp_BackDrop );
// create memory device context to hold DRAWING
// m_memDC_Overlay.CreateCompatibleDC(&dc);
// m_memDC_Overlay.SelectObject( &Bmp4 ); //select bitmap SAMPLE256
// image is painted in OnPaint function
// create solid brush (m_Brush<colour> defined in .h file)
m_BrushRed.CreateSolidBrush( RGB(255,0,0) );
// CBrush *pBrushSaved = ... //I'VE NO NEED FOR SAVE. NEEDED?
m_memDC_BackDrop.SelectObject ( &m_BrushRed );
// ??? m_memBMP_BackDrop.CreateCompatibleBitmap( &dc, 300, 200)
// draw Circle in bounding rectangle...
m_memDC_Overlay.Ellipse(5,5,30,30);
...
===============================
... ::OnPaint()
...
// bit block transfer image from memory-DC to paint-DC
dc.BitBlt( 0, 0, 300,300, &m_memDC_BackDrop, 0, 0, SRCCOPY);
Regards & Thanks, Jack@SIL
|
|
|
|
|
This article is exactly what i was looking for! Works perfectly, thankyou for saving me the trouble of digging through mfc documentation!
|
|
|
|
|
Hi folks,
I have a CStatic control whose text gets updated [using SetWindowText() ] on a OnTimer event (basically writing a ticker-tape scrolling text display). How can I achieve flicker-free drawing in this control? I have implemented CMemdc -based drawing for the view in which the control is contained but that doesn't seem to help much.
Thanks!
- Amit
|
|
|
|
|
Anyone tried this to CFormView?
|
|
|
|
|
I am getting an unhandled exception when using MemDC in a Dialog. My OnPaint looks like this:
void MyDialog::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect;
GetClientRect( &rect );
CMemDC pDC( &dc, &rect );
// Do not call CResizableDialog::OnPaint() for painting messages
}
The code throws and exception here:
...
if (m_bMemDC) {
// Create a Memory DC
CreateCompatibleDC(pDC);
pDC->LPtoDP(&m_rect);
m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(),
m_rect.Height()); < - - - HERE
m_oldBitmap = SelectObject(&m_bitmap);
SetMapMode(pDC->GetMapMode());
Any ideas?
c++ developer
|
|
|
|
|
Sorry, pilot error. Is now working. The flicker, however, is still there.
c++ developer
|
|
|
|
|
I'm writing a mapping application of sorts and since it updates the display rather frequently, I wanted to use double buffering to cut down on flickering. CMemDC works great for most of my drawing code, but when I added the calls to SetGraphicsMode(...) and SetWorldTransform(...) it failed to render anything at all, even things before and after the calls. If I simply replace the one line that creates the CMemDC object with a FillRectangle on the normal DC, the display draws properly and rotates my objects, but I get a fair amount of flickering.
Any tips or suggestions on how to fix this problem would be greatly appreciated.
Thanks,
Jason
|
|
|
|
|
Has anyone gotten this to work in dialogs with bitmapped backgrounds? My dialogs are bitmapped with LoadImage, and (to make things even more fun) are resizable. Needless to say the flicker is out of hand when resizing these dialogs. Dropping in this memdc code as is causes my dialog backgrounds to be completely transparent.
Anyone tried the Maintaining a Permanent Bitmap idea offered at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dngdi/html/msdn_flicker.asp
|
|
|
|
|
dunno what happened
when i first used this class yesterday, it was pretty cool, absolutely no flicker, checked up CPU usage, 6% max, today, no flicker, jerky mouse, CPU usage 80%. what got into it ???
the only thing i did was install PSDK update as i wanted to use Alphablend, a nice glass like door over my screen which slides out as the app starts. that too worked too smoothly, but only yesterday, i see no blending today if i use memdc
any ideas ??
------------------------
Gopal Raichur
|
|
|
|
|
FOUND THE PROBLEM
the problem was my browser, all the time i was debugging today, there was a browser running. i normally use MYIE2, which if running, causes problems with memdc . i found this by accident after writing my earlier post, closing browser and giving it a final try.
i tried IE, and it gave no probs, so some incompatibility b/w memdc and MYIE2.
this is not the first time i have seen such things happening -
1) any flash/shockwave app running, winamp plugin (BJR VU Meters) starts jerky updates
2) SUN java runtime, installed with mozilla, any applet running - flashget drop zone starts flickering along with some other topmost windows
all this on WinXP SP1
hmm, well have to check with other apps before giving this to client
------------------------
Gopal Raichur
|
|
|
|
|
This is a great class and I would like to see it improved. In the class you are assuming the background is Opaque by filling in the background of the DC.
<br />
FillSolidRect(m_rect, pDC->GetBkColor());<br />
Instead, you should be checking the BK mode and handling it appropriatly.
Constructor
<br />
CMemDC(CDC* pDC, const CRect* pRect = NULL, CDC* pParentDC) : CDC()<br />
And a little later
<br />
if(pDC->GetBkMode() == TRANSPARENT && pParentDC != NULL){<br />
}else <br />
FillSolidRect(m_rect, pDC->GetBkColor());<br />
|
|
|
|
|
CMyDialog::OnPaint()
{
CPaintDC dc(this);
CRect rcClient;
GetClientRect(rcClient);
CMemDC pDC(&dc, &rect);
....
}
but still flicker.
why?
|
|
|
|
|
You must handle wm_erasebkgnd
John
|
|
|
|
|
i have added it in MyDialog.h and MyDialog.cpp
MyDialog.h
{
...
// Generated message map functions
protected:
//{{AFX_MSG(CMyDialog)
afx_msg void OnTimer(UINT nIDEvent);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
}
Mydialog.cpp
BOOL CMyDialog::OnEraseBkgnd(CDC* pDC)
{
return FALSE;
}
the app still flicker.
why?
thx.
|
|
|
|
|
Not sure why, Sorry.
John
|
|
|
|
|
return TRUE on OnEraseBkgnd
|
|
|
|
|
The solution works well in CView.But in my case,I want to diplay a photo on a dialog.How to get flicker free drawing in CDialog?I added a funtion in CMyDlg,it is under below
LRESULT CMyDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch (message)
{
case WM_ERASEBKGND:
return 0;
}
return CDialog::WindowProc(message, wParam, lParam);
}
and used CMemDC like the article says.The background of the dialog become white!Who can help me?Thank you!
|
|
|
|
|
i use CMemDC class ,when i use OnPrepareDC as this :
CScrollView::OnPrepareDC(pDC, pInfo);
pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
// LOENGLISH units are based on physical inches
// We want logical inches so we have to do it differently
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetViewportExt(pDC->GetDeviceCaps(LOGPIXELSX),
pDC->GetDeviceCaps(LOGPIXELSY));
float fScale=pDoc->m_fViewScale ;
if(pDC->IsPrinting())
fScale=1.0;
pDC->SetWindowExt(int(100*fScale),int(100*fScale));
if (pDC->IsPrinting ()) { // If printing...
// int y = ((pInfo->m_nCurPage - 1)%pDoc->m_pRpt->GetPageCount()) * pDoc->m_nPageHeight;
int y = (pInfo->m_nCurPage - 1) * pDoc->m_nPageHeight;
pDC->SetViewportOrg (0, y);
}
The draw is cause some error,how to resolve it!!
|
|
|
|
|