Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CButton dynamic create

274 views
Skip to first unread message

Malcolm Groom

unread,
Apr 28, 1999, 3:00:00 AM4/28/99
to
I am trying to dynamically add some buttons to a dialog I have in the
OnInitDialog. Whilst the create is returning a success value nothing is
appearing one the dialog apart from those buttons designed with the
resource editor.

I am using VC 6 SP2 on NT4 SP4

The following is my code, can anyone tell me what I'm forgetting to
do/doing wrong?

(I've included my code for the routine rather than just the relevant few
lines in case I've missed anything else out <g>)

CWnd *wndDlg = GetParentOwner();
RECT rect;
wndDlg->GetWindowRect(&rect);
wndDlg->SetWindowPos(NULL, 0, 0, rect.right - rect.left, rect.bottom -
rect.top + 100, SWP_NOMOVE);
CWnd *wndOKCtrl = wndDlg->GetDlgItem(IDOK);
wndOKCtrl->GetWindowRect(&rect);
wndDlg->ScreenToClient(&rect);
wndOKCtrl->SetWindowPos(NULL, rect.left, rect.top + 100, 0, 0,
SWP_NOSIZE);
CWnd *wndCancelCtrl = wndDlg->GetDlgItem(IDCANCEL);
wndCancelCtrl->GetWindowRect(&rect);
wndDlg->ScreenToClient(&rect);
wndCancelCtrl->SetWindowPos(NULL, rect.left, rect.top + 100, 0, 0,
SWP_NOSIZE);
CButton Button1;
CRect ButtonRect;
ButtonRect.top = 80;
ButtonRect.bottom = 200;
ButtonRect.left = 80;
ButtonRect.right = 200;
if (Button1.Create("", BS_OWNERDRAW | BS_PUSHBUTTON | WS_VISIBLE |
WS_CHILD | WS_TABSTOP, ButtonRect, wndDlg, 1234) !=
0)
{
m_bitmapTemaC.LoadBitmap(IDB_TEMAC_BITMAP);
HBITMAP hpic = (HBITMAP) m_bitmapPic.GetSafeHandle();
((CButton *) GetDlgItem(1234))->SetBitmap(hPic);
AfxMessageBox("Success");
}
else
{
AfxMessageBox("Failure");
}

--
Malcolm Groom

Rick Genter

unread,
Apr 28, 1999, 3:00:00 AM4/28/99
to
You have to call CButton::ShowWindow (SW_SHOW) after you create the button.

--
Rick Genter
ICQ UIN# 8291497
<mailto:rgenter at vipcalling.com>
<http://www.vipcalling.com>
Malcolm Groom <Mal...@WCompSys.XXXdemon.co.uk> wrote in message
news:EWl4kAA2...@wcompsys.demon.co.uk...

Bruno BORNIL

unread,
Apr 28, 1999, 3:00:00 AM4/28/99
to

Malcolm Groom a écrit :

Hello,

Sorry, but I search the same thing...

Maybe the first argument is not ok: try something like that:
- Button1.Create("BUTTON",...)
- or Button1.Create( _T("BUTTON"),...)

Thanks for help me too...


Noam Rathaus

unread,
Apr 28, 1999, 3:00:00 AM4/28/99
to
Hi,

Sorry but you are mistaken, it is drawing your button, but it is drawning
NOTHING.

You told MFC to: BS_OWNERDRAW

Making it look for a function called DrawItem (a virtual function, which
should have ASSERTED because I am sure you haven't created such a function),
and when it "finds" this funtion it executes it, but nothing happens because
it doesn't exist, so it doesn't paint it.

If you remove this OWNERDRAW style, it should show up.

--

Thanks
Noam Rathaus


Malcolm Groom <Mal...@WCompSys.XXXdemon.co.uk> wrote in message
news:EWl4kAA2...@wcompsys.demon.co.uk...

> I am trying to dynamically add some buttons to a dialog I have in the
> OnInitDialog. Whilst the create is returning a success value nothing is
> appearing one the dialog apart from those buttons designed with the
> resource editor.
>
> I am using VC 6 SP2 on NT4 SP4
>
> The following is my code, can anyone tell me what I'm forgetting to
> do/doing wrong?
>
> (I've included my code for the routine rather than just the relevant few
> lines in case I've missed anything else out <g>)
>
>

> --
> Malcolm Groom

Vikas Gupta

unread,
Apr 28, 1999, 3:00:00 AM4/28/99
to Malcolm Groom
Just override the WM_SHOWWINDOW message for the dialog and in that create all
the buttons.

CDialog / CFormView :: OnShowWindow(BOOL bShow, UINT nStatus)
{


CButton Button1;
CRect ButtonRect;
ButtonRect.top = 80;
ButtonRect.bottom = 200;
ButtonRect.left = 80;
ButtonRect.right = 200;
if (Button1.Create("", BS_OWNERDRAW | BS_PUSHBUTTON | WS_VISIBLE |
WS_CHILD | WS_TABSTOP, ButtonRect, wndDlg, 1234) !=
0)
{
m_bitmapTemaC.LoadBitmap(IDB_TEMAC_BITMAP);
HBITMAP hpic = (HBITMAP) m_bitmapPic.GetSafeHandle();
((CButton *) GetDlgItem(1234))->SetBitmap(hPic);
AfxMessageBox("Success");
}
else
{
AfxMessageBox("Failure");
}
}

I hope this will work for u.

-Vikas

Malcolm Groom wrote:

> I am trying to dynamically add some buttons to a dialog I have in the
> OnInitDialog. Whilst the create is returning a success value nothing is
> appearing one the dialog apart from those buttons designed with the
> resource editor.
>
> I am using VC 6 SP2 on NT4 SP4
>
> The following is my code, can anyone tell me what I'm forgetting to
> do/doing wrong?
>
> (I've included my code for the routine rather than just the relevant few
> lines in case I've missed anything else out <g>)
>

vgupta.vcf

Malcolm Groom

unread,
Apr 29, 1999, 3:00:00 AM4/29/99
to
In article <7g9g3n$dg3$1...@quasimooto.altair.com>, Ken Vadella
<k...@altair.com> writes
>I think the real problem is that your 'Button1' instance is in the scope of
>the function. When the function goes out of scope your button gets
>destructed.

Yep, I sussed it this morning after going through the other suggestions
people gave me. One of those where you slap yourself for not spotting
the obvious <g>. The OWNERDRAW caused problems too but as I wanted a
bitmap on it...

Anyway, having pointers to CButton with global dialog scope solved it.
Then changing to CBitmapButton got around the ownerdraw problem.

Thanks to everybody who made suggestions.

--
Malcolm Groom

Ken Vadella

unread,
Apr 30, 1999, 3:00:00 AM4/30/99
to
I think the real problem is that your 'Button1' instance is in the scope of
the function. When the function goes out of scope your button gets
destructed.

Ken

Vikas Gupta <vgu...@rtp-bosch.com> wrote in message
news:37274EAB...@rtp-bosch.com...

Joseph M. Newcomer

unread,
May 5, 1999, 3:00:00 AM5/5/99
to
Note that this has an interesting implication: the button is actually
there, but you can't see it. You can actually take advantage of this
to create buttons "behind" bitmaps, creating hotspots on the bitmap.
But I agree: you should have gotten an ASSERT failure because the
default OnDraw method calls ASSERT(FALSE). Unless, of course, you were
only compiling in the Release version.
joe

On Wed, 28 Apr 1999 19:47:48 +0200, "Noam Rathaus"
<no...@radiotel.co.il> wrote:

>Hi,
>
>Sorry but you are mistaken, it is drawing your button, but it is drawning
>NOTHING.
>
>You told MFC to: BS_OWNERDRAW
>
>Making it look for a function called DrawItem (a virtual function, which
>should have ASSERTED because I am sure you haven't created such a function),
>and when it "finds" this funtion it executes it, but nothing happens because
>it doesn't exist, so it doesn't paint it.
>
>If you remove this OWNERDRAW style, it should show up.

Joseph M. Newcomer
newc...@flounder.com
http://www3.pgh.net/~newcomer

Joseph M. Newcomer

unread,
May 5, 1999, 3:00:00 AM5/5/99
to
Yep, that's it. CButton objects must exist on the heap, either as
CButton variables in your window class (allowing Button1.Create) or as
dynamically allocated objects (CButton * button; to use this you would
do button = new CButton(); button->Create(...) )
joe

On Fri, 30 Apr 1999 07:29:33 -0400, "Ken Vadella" <k...@altair.com>
wrote:

Joseph M. Newcomer
newc...@flounder.com
http://www3.pgh.net/~newcomer

Malcolm Groom

unread,
May 6, 1999, 3:00:00 AM5/6/99
to
In article <37408b61....@206.210.64.12>, Joseph M. Newcomer
<newc...@flounder.com> writes

>Note that this has an interesting implication: the button is actually
>there, but you can't see it. You can actually take advantage of this
>to create buttons "behind" bitmaps, creating hotspots on the bitmap.
>But I agree: you should have gotten an ASSERT failure because the
>default OnDraw method calls ASSERT(FALSE). Unless, of course, you were
>only compiling in the Release version.

Been a while since I looked at this now. But it was definitely in the
debug version and no assert was issued. I can't remember everything I
tried before I got it working but I did manage some quite impressive
crashes before I finished :)
--
Malcolm Groom

0 new messages