Re: static hide - the static is not hide

Tech-Archive recommends: Fix windows errors by optimizing your registry



See below...
On Mon, 17 Dec 2007 09:21:16 -0800 (PST), fiversen <fiversen@xxxxxxxxxxxxxxxxxxxx> wrote:

your OnPaint handler.

I try to make it simpler - but it is a little bit long.

------------------------------------------------------------------------
void COStatic::OnPaint()
{
CPaintDC pdc(this); //bb
****
And why did you change the name to use a 'p' prefix which is usually used to designate
pointers; this is not a pointer, and if you create this using the tooling, it is called
CPaintDC dc(this);
Note that the concept of bounding box is marginal anyway, and the CPaintDC *does* define a
bounding box, but the bounding box is generally irrelevant for most drawing; it is used
only when there is significant performance issues, which I'm not seeing here
****
if (!pdc)
{
cout << "COStatic:OnPaint: no pdc!" << endl;
return;
}
****
It is not clear what this is testing. Also, cout is meaningless in Windows programs, so
you should not be considering it to exist at all. Note that if the BeginPaint failed,
this is going to go into an infinite loop anyway.
****

CRect rect;
GetClientRect(rect);

if (rect.Width() < 2 && rect.Height() < 2)
return;
****
Did you really mean &&? This requires BOTH to be < 2, and therefore it would attempt to
draw if the window was 0 pixels or 1 pixel wide but 200 pixels high...

UiActivFld *uiactiv = (UiActivFld*)owner;

CPen pen;
CBrush br;
COLORREF fillCol = 0;
BOOL hasFill = TRUE;
DetectPaintColor(hasFill, fillCol);

//background
if (cRgn)
{
COLORREF c = bgColor;
CBrush brush(c);
pdc.FillRgn(cRgn, &brush);
return;
}
****
I didn't see the region being created. Why a complex thing that requires some region to
exist, when it is so easy to just create a rectangle and fill it? What is the point of
using a region? Note that this merely fills the region and doesn't actually draw any
text, so it is not clear why this is just returning. Why not simply
pdc.FillSolidRect(&r, bgColor);
and compute whatever r is supposed to be. Since I see no computation of a region here, is
isafe to assume that the region is essentially meaningless. So you might be filling an
empty region and returning. There are too many places you return based on information you
have not supplied, so it is impossible to analyze your code for correctness.
****

if (GetExStyle() & WS_EX_TRANSPARENT) // transparent background
{
CBrush brush;
brush.CreateStockObject(NULL_BRUSH);
CBrush *oldBr = pdc.SelectObject(&brush);
****
Generally, avoid using "old" values. When you come in, do something like
int save = pdc.SaveDC();
and restore it at the end of the code

You would then write
pdc.SelectStockObject(NULL_BRUSH);
you don't need intermediate variables as you have done. So the three lines above can be
replaced by one line.
****

CPen pen;
pen.CreateStockObject(NULL_PEN);
CPen *oldP = pdc.SelectObject(&pen);
****
Ditto. No need for three lines when one line would do:
pdc.SelectStockObject(NULL_PEN);
****

pdc.Rectangle(&rect);
****
Does this really serve any useful purpose? You are drawing a rectangle with a NULL_PEN
and NULL_BRUSH, which does precisely NOTHING. So why are you doing it?
****

pdc.SelectObject(oldBr);
pdc.SelectObject(oldP);
}
else if (hasFill)
{
pdc.FillSolidRect (&rect, fillCol);
}
****
And what does hasFill tell you? What happens if it is not transparent and HasFill is
FALSE?
****

if (owner && owner->isA(&uiSubFormDef) &&
((UiSubForm*)owner)->GetDrawAreaW())
{
DrawRaster(pdc, rect);
return;
}
****
I have no idea what this code is doing, but it does a return, so are you sure that the
reason you see nothing displayed is that you are taking one of these return statements?
Have you considered adding some TRACE statements, or simply single-stepping through the
code (you have to arrange your app and your debugger to not overlap, which can be a pain
on a single-monitor system, but I've done it many times)
****


if (isBitmap) // draw Bitmap
{
if (!dibT)
return;
****
Every time you have a return statement you have the potential for "seeing nothing".
Consider putting breakpoints on all the return statements to see if one of them is being
taken unexpectedly
****

int cxClient = rect.Width() ;
int cyClient = rect.Height();

CRect rect2;
GetClientRect(rect2); // = paint.rcPaint; //lpDrawItemStruct-
rcItem;
****
Why not do
rect2 = rect;
because rect already has the GetClientRect()?
****

int sw = (int) dibT->GetWidth();
int sh = (int) dibT->GetHeight();

// Calculate the positions for the bitmap in the client area
int bmX = int((float)rect2.Width()/2 - (float)sw/2 +
0.5);
int bmY = int((float)rect2.Height()/2 - (float)sh/2 +
0.5);
int bmWidth = sw;
int bmHeight = sh;

rect.SetRect(0, 0, cxClient, cyClient);

if (hasFill)
pdc.FillSolidRect (&rect, fillCol);

// draw bitmap
CRect rcDest (bmX, bmY, bmX+bmWidth, bmY+bmHeight);
dibT->Paint(GetSafeHwnd(), pdc.GetSafeHdc(),
&rcDest);
****
I'm curious why this method wants an HWND and an HDC when you could just as easily pass a
CWnd & or CWnd * and a CDC & or a CDC *?
****

FillSalesFields(pdc, rcDest);
}

else // draw Text
{
if (!uiactiv)
{
cout << " COStatic:only use for UiLabelFld\n";
****
And what does cout do? There's no stdout handle defined for a GUI app, so what is this
going to do?
****
return;
}

if(!uiactiv->isA(&uiLabelFldDef))
{
//cout << "COStatic:only use for UiLabelFld:"
// << uiactiv->fieldName()
// << endl;
return;
}

UiLabelFld *uilabelfld = (UiLabelFld*)uiactiv;

BfString str = uilabelfld->textLabel();
if (str.length() == 0)
return;
****
Another return statement...
****

Gui::replaceNL(str);

int x = 0;
int y = 0;
UINT st = TA_TOP;

if (uilabelfld->getVBottom()) {
short w=0, h=0;
gui->GetTextExtent(this, w, h, str, 2, 2, GetFont());
y = rect.top + (rect.bottom - rect.top) - h;
}
if (uilabelfld->getVCenter())
{
short w=0, h=0;
gui->GetTextExtent(this, w, h, str, 2, 2, GetFont());
int space = (rect.bottom - rect.top) - h;
y = rect.top + space / 2;
}

if (uilabelfld->alignValue() & 1)
{
x = (rect.right - rect.left) / 2;
pdc.SetTextAlign(st | TA_CENTER);
}
else if (uilabelfld->alignValue() & 2)
{
x = rect.right;
pdc.SetTextAlign(st | TA_RIGHT);
}
else
{
x = 0;
pdc.SetTextAlign(st | TA_LEFT);
}

if (uilabelfld && uilabelfld->fgColor())
pdc.SetTextColor (fgColor); //ccc

CFont *oldF = (CFont*) pdc.SelectObject (uilabelfld-
getFontRef());
****
Get rid of oldF and use SaveDC()
****

pdc.SetBkMode (TRANSPARENT);
pdc.TextOut (x,y, wChar(str), str.length());

if (oldF)
pdc.SelectObject(oldF);
****
Use RestoreDC
****
}
}
------------------------------------------------------------------------

Problem
(no refresh of the background):
If I use WS_EX_TRANSPARENT - to draw the text on a window with a
bitmap:
// draw transparent
// draw Text

I want to force a repaint of the control, with hide and show.
But
static->ShowWindow(SW_HIDE);
and nothing happens.
****
I just noticed that you are using static->. static is a reserved word, and in any case
you would not use -> if you have a control variable, you would use something like
c_MyListControl.ShowWindow(SW_HIDE);
you have not shown how you initialize this reserved word, or how you use it.
****



OK
(text refreshes with every new text):
I don't use bitmaps,
I have a colored background,
draw text with a fill color,
anything is OK.
// draw Text


I use CDC and Gdi++ to draw the bitmaps.
I hope - it is not a problem to have CPaintDC and Gdiplus::Graphics -
side by side.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Custom Draw LIstCtrl and scrolling
    ... I wrote a custom draw CListCtrl that allow to display different row height on windows mobile ... void CListCtrlCommands::OnSize(UINT nType, int cx, int cy) ... CRect rect; ... GetClientRect(&rect); ...
    (microsoft.public.vc.mfc)
  • Re: Custom Draw LIstCtrl and scrolling
    ... I wrote a custom draw CListCtrl that allow to display different row ... void CListCtrlCommands::OnSize(UINT nType, int cx, int cy) ... CRect rect; ... GetClientRect(&rect); ...
    (microsoft.public.vc.mfc)
  • Re: static hide - the static is not hide
    ... pdc.FillSolidRect; ... if (isBitmap) // draw Bitmap ... int cyClient = rect.Height; ...
    (microsoft.public.vc.mfc)
  • ATL OnDraw with POPUP window
    ... My problem is when I am trying to draw an BITMAP to the ... HBITMAP hbm; ... RECT rect; ... how can I get Events in a different Window or Draw the ...
    (microsoft.public.vc.atl)
  • Re: Graphics help please
    ... I've got something similar, with a 3D turtle. ... You can also draw a line from to, with the ... final int width = 1024; ... static public void main ...
    (comp.lang.java.programmer)