Board index » cppbuilder » Drawing onto form background

Drawing onto form background

I'm trying to draw an image onto the background of an MDI parent.

Placing a VCL component doesn't work too well since the component is
either invisible or appears infront of all non-modal forms (but
behind modal forms).

I read somewhere that the answer was to respond to WM_ERASEBKGND so I
implemented the following:

void __fastcall TfrmMain
  ::BkGroundProc(TMessage & Message)
{
  if( Message.WParam!=WM_ERASEBKGND )
    WndProc( Message );
  else
    Message.Result=0;

Quote
}

// and set

{
  ..
  WindowProc=BkGroundProc;

Quote
}

This calls my handler but I can't get anything to appear and in any
case it mysteriously prevents the close button (top right 'X') from
working.

If anyone has any ideas as to how to draw on a form's background I'd
love to read them..

Andrue Cope
[Bicester, UK]

 

Re:Drawing onto form background


Quote
"Andrue Cope" <not.a.va...@email.address.sorry> wrote in message

news:VA.00000ecf.00ef801b@email.address.sorry...

Quote
> I read somewhere that the answer was to respond to WM_ERASEBKGND so I
> implemented the following:

<snip>

Quote
>   if( Message.WParam!=WM_ERASEBKGND )

You need to test Message.Msg value, not Message.WParam.

Quote
>   WindowProc=BkGroundProc;

For a single message, it's better to use a MESSAGE_MAP instead of overriding
WindowProc:

class TfrmMain : public TForm
{
private:
    void __fastcall WMEraseBkGnd(TMessage &Message);
public:
    BEGIN_MESSAGE_MAP
        VCL_MESSAGE_HANDLER(WM_ERASEBKGND , TMessage, WMEraseBkGnd)
    END_MESSAGE_MAP(TForm)

Quote
};

void __fastcall TfrmMain::WMEraseBkGnd(TMessage &Message)
{
    HDC hDC = (HDC)Message.WParam;
    // do your painting to hDC
    Message.Result = 1;

Quote
}

Gambit

Re:Drawing onto form background


Remy,

Quote
> You need to test Message.Msg value, not Message.WParam.

D'oh! I knew I must have been more tired than I thought yesterday
afternoon <s>.

I changed my code over to using MESSAGEMAP and it works - but only
for none MDI forms. Unfortunately this application is MDI. I tried
putting the code into a WMPaint handler but that just slowed the
application down to a crawl.

Surely there is /some way/ this can be done?

Andrue Cope
[Bicester, UK]

Re:Drawing onto form background


Remy,

I'm still looking for a 'proper' solution to this problem but I found
a workaround. I've created a child MDI form without a caption that
sizes itself to fill the entire parent window. MDI children can be
drawn on w/out problems so I've added a TPicture and used that.

Andrue Cope
[Bicester, UK]

Re:Drawing onto form background


Hello Andrue,

"Andrue Cope" <not.a.va...@email.address.sorry> schreef in bericht
news:VA.00000ecf.00ef801b@email.address.sorry...

Quote
> I'm trying to draw an image onto the background of an MDI parent.

> Placing a VCL component doesn't work too well since the component is
> either invisible or appears infront of all non-modal forms (but
> behind modal forms).

> I read somewhere that the answer was to respond to WM_ERASEBKGND so I

Hmm, that's only part of it<g>, you have to subclass the client area.
I've used the following example successful on W95 and W98se, don't know if
anything
has changed with newer versions though.

// in the TForm's header
private:
  Pointer OriginalClientProc;
  Pointer ClientObjectInstance;
protected:
  virtual void __fastcall CreateWnd();
  virtual void __fastcall DestroyWnd();
  virtual void __fastcall ClientProc(TMessage & Msg);
  virtual void __fastcall DrawClientWindow(HDC & Hdc);
  void __fastcall WMEraseBkgnd(TWMEraseBkgnd & Msg);
  BEGIN_MESSAGE_MAP
      VCL_MESSAGE_HANDLER(WM_ERASEBKGND, TWMEraseBkgnd, WMEraseBkgnd)
  END_MESSAGE_MAP(TForm)

// in the TForm's source

void __fastcall TMainForm::CreateWnd()
{
  TForm::CreateWnd();
  ClientObjectInstance = MakeObjectInstance(ClientProc);
  OriginalClientProc = (Pointer)SetWindowLong(ClientHandle, GWL_WNDPROC,
(long)ClientObjectInstance);

Quote
}

// -------------------------------------------------------------------------

void __fastcall TMainForm::DestroyWnd()
{
  SetWindowLong(ClientHandle, GWL_WNDPROC, (long)OriginalClientProc);
  FreeObjectInstance(ClientObjectInstance);
  TForm::DestroyWnd();

Quote
}

// -------------------------------------------------------------------------

void __fastcall TMainForm::ClientProc(TMessage & Msg)
{
  switch( Msg.Msg ) {
    case WM_ERASEBKGND:
      DrawClientWindow((HDC)Msg.WParam);
      Msg.Result = true;
      break;
    case WM_HSCROLL:
    case WM_VSCROLL:
      Msg.Result = CallWindowProc( (FARPROC)OriginalClientProc,
                                   ClientHandle,
                                   Msg.Msg, Msg.WParam, Msg.LParam );
      InvalidateRect(ClientHandle, 0, true);
      break;
    default:
      Msg.Result = CallWindowProc( (FARPROC)OriginalClientProc,
                                   ClientHandle,
                                   Msg.Msg, Msg.WParam, Msg.LParam );
  }

Quote
}

// -------------------------------------------------------------------------

void __fastcall TMainForm::WMEraseBkgnd(TWMEraseBkgnd & Msg)
{
  Msg.Result = false;

Quote
}

Btw, the DrawClientWindow() member you have to do yourself<g>

Quote
> If anyone has any ideas as to how to draw on a form's background I'd
> love to read them..

Hope this toggles the brain...;-))

Quote
> Andrue Cope

--
Greetings from overcast Amsterdam

         Jan

email: bijs...@worldonline.nl
http://home.worldonline.nl/~bijster

Re:Drawing onto form background


J.A.,

Thanks - I'll look at that today.

Andrue Cope
[Bicester, UK]

Re:Drawing onto form background


Quote
Andrue wrote:
> I'm trying to draw an image onto the background of an MDI parent.

Something here:
http://bcbcaq.com/CAQs/MDI_Back.html

Re:Drawing onto form background


Fishface,

Excellent - just what I was after. Thanks!

Andrue Cope
[Bicester, UK]

Re:Drawing onto form background


Thanks.

"Fishface" <fishf...@drift.net> ???Y/???Y ????? ???Y??:
news:3bff1343$1_1@dnews...

Quote

> Andrue wrote:
> > I'm trying to draw an image onto the background of an MDI parent.

> Something here:
> http://bcbcaq.com/CAQs/MDI_Back.html

Other Threads