Click here to Skip to main content
15,895,142 members

Trouble with area fixing using ValidateRect(). The best way to optimize the code

JOHN 602 asked:

Open original thread
Hello everyone

I'm trying to resolve the problem with image blinking and I almost get it ,but I got some problems.

I want to redraw all the rest of the window except only the image. I'm trying to throw the Image's rectangle(i.e.coordinates) into ValidateRect and perform rendering but somehow it doesn't work

If I just overriding OnEraseBkgnd(CDC* pDC) {return false;}
It gives me a workable code,without any flickering(especially it looks great with high-definition images like 1680x1050) . But with small pictures where all the rest window is free when I begin resize it, it lefts remains(thats why I need to redraw it)
I've tried a lot of different ways and can't make it work
All the code is in OnDraw(CDC* pDC/*pDC*/) function
1)
if(oldpathName!=pathName){
	memDC.DeleteDC();
	memDC.CreateCompatibleDC(pDC);
	cImg.Destroy();
	if(cImg.Load((LPCTSTR)pathName)!=S_OK)
		AfxMessageBox(L"Error");
	memDC.SelectObject(cImg);
	pDC->BitBlt(0, 0, cImg.GetWidth(), cImg.GetHeight(), &memDC, 0, 0, SRCCOPY);
	oldpathName=pathName;erase=true;}
	else{
		CRect s(0,0,tmpImg->width,tmpImg->height);
		////InvalidateRect(s,FALSE);
		//RedrawWindow(s,NULL,RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
		ValidateRect(&s);
		pDC->BitBlt(0, 0, cImg.GetWidth(), cImg.GetHeight(), &memDC, 0, 0, SRCCOPY);
		//RedrawWindow(s,NULL,RDW_VALIDATE | RDW_UPDATENOW | RDW_ERASENOW);


If I run the program,the area where the image hangs (I validated it) begins blinking when I resize the borders.With small images I almost see nothing , but when I do it with the large ones some blinks occurs. It means that the function didn't fix the image

Maybe I should carry my code from OnDraw() into OnPaint(),than (maybe) OnPaint()'d call ->WM_PAINT->and then OnEraseBkgnd() would redraw the window or smth like that? It may look stupidly but I dont see any way now.Where is the mistake???

Please gimme a clue what I should change

After many efforts I at last got What I need
To resolve my problem I dragged all drawing operations from OnDraw() to OnPaint()
But somehow I noticed on GPU widget, that my video accelerator(i.e. videocard) sustains some load when I begin load\reload some amount of images. If I operate with resizing on MsPaint all look fine. I assume maybe I somewhere have some unnecessary overheads.
Now my code looks that way:
void CMicrofilmOfficeView::OnPaint()
{
if(tmpImg){
	CClientDC aDC(this);
    CDC* pDC =&aDC;
	if(oldpathName!=pathName){
	memDC.DeleteDC();
	memDC.CreateCompatibleDC(pDC);
	cImg.Destroy();
	if(cImg.Load((LPCTSTR)pathName)!=S_OK)
		AfxMessageBox(L"Error");
	memDC.SelectObject(cImg);
	pDC->BitBlt(0, 0, cImg.GetWidth(), cImg.GetHeight(), &memDC, 0, 0, SRCCOPY);
	oldpathName=pathName;}
	else{
		pDC->BitBlt(0, 0, cImg.GetWidth(), cImg.GetHeight(), &memDC, 0, 0, SRCCOPY);
		ValidateRect(&CRect(0,0,tmpImg->width,tmpImg->height));
	}
}


Please gimme plz an advice how to maximum optimize my code /
Tags: C++, MFC

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900