I have receently started developing an MFC SDI project. Previously I used
Dialog based MFC.
In my SDI app I have added a CBitmapButton, using the mechanism recommended
to me by members of the list. This is the code in OnCreate() where I load up
my CBitmapButton -
int CGuiView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
(CView::OnCreate(lpCreateStruct) == -1)
return -1;
// Create the bitmap button
m_exitButton.Create(_T("Exit"), WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
CRect(10,10,100,100), this, 1);
m_exitButton.LoadBitmaps(IDB_BTNUP, IDB_BTNDOWN);
m_exitButton.SizeToContent();
return 0;
}
This displays the button on the screen and I can click on it fine and watch
the button change state i.e up and down. However, I now want to add some
'control functionality' to it i.e. do something when I press the button. In
my Dialog based apps it was fairly easy to do. I would just drag over a
button control, name it, double click on it and a fucntion would be added
where I could write my code for what should happen when I press the button.
But I'm not sure how to do the same with my SDI app?? I.e. how do I connect
my bitmapbutton to a control for a button?
This is probably very obvious but things like this 'really' slow me down.
Thanks once again for any tips,
Best Regards,
David
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_Child_Window_Notification_Message_Handlers.asp
--
Jeff Partch [VC++ MVP]
Thanks for your quick answer. I have seen the ON_BN_CLICKED in my dialog
app, but in the dialog app the ID of the button is generated automatically
i.e.
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
And the IDC_BUTTON1 is linked to the button 'on the fly'. I cant see how I
can assign an ID to my CBitmapButton?? Thats whats confusing me.
Thanks again for any tips,
David
You did/do that in your call to Create...
> Hi there,
>
> Thanks for your quick answer. I have seen the ON_BN_CLICKED in my dialog
> app, but in the dialog app the ID of the button is generated automatically
> i.e.
>
> ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
>
> And the IDC_BUTTON1 is linked to the button 'on the fly'. I cant see how I
> can assign an ID to my CBitmapButton?? Thats whats confusing me.
>
> Thanks again for any tips,
> David
Adding controls and message handlers is exactly the same in SDI and
dialog apps. The difference you have introduced is not due to SDI but
because you have created the CBitmapButton from code (dynamically)
instead of putting it on the dialog resource.
One solution is to put a button on the resource (set the owner draw
property) and use the IDE to create a CButton member variable. Then
edit CButton to CBitmapButton, and remove your call to Create. This
should give you the standard wizard support for adding message handlers.
The other solution, using what you already have, is to type in the
message map entries by hand. You already did assign an ID to the
button: the "1" in your Create call. There is an IDE command
(somewhere!) to allocate a new ID symbol, or you can hand-edit
resource.h to allocate a new ID symbol.
--
Scott McPhillips [VC++ MVP]
In Resource.h
#define IDC_BUTTON1 1000
In MyView.h
CBitmapButton m_exitButton;
In MyView.cpp
m_exitButton.Create(_T("Exit"), WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
CRect(10,10,100,100), this, IDC_BUTTON1);
and also...
BEGIN_MESSAGE_MAP(CGuiView, CView)
// Standard printing commands
// a few other things here
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
END_MESSAGE_MAP()
Hopefully thats the correct procedure.
Thanks again for your help,
Best Regards,
David Ross
Thanks for your answer and information. You must of posted just a second
before my reply! I'll look into the techniques you mentioned. I think I must
have worked out the last way you described i.e doing all the coding by hand.
But I'll take a look at the other methods you suggested as well for
completeness.
Many Thanks!
David
David:
Have you considered using an SDI based on CFormView rather than CView?
HTH,
David Wilkinson
I agree with Scott. Using the resource editor for editing dialog resources
whenever you can will make it easy to do these kinds of changes, update the
correct files, make tab order setting much easier, and you'll get to see
what your dialog looks like before you even compile. Visual creation of
components is one of the best things about the Visual Studio IDE. You'll
also be able to hook in variables and events for controls in the dialog
editor and not have to worry too much about what code to add to your
program. This is especially useful when you're trying to learn the whole
thing.
My two cents,
Tom
"David++" <Da...@discussions.microsoft.com> wrote in message
news:5CF2BE8F-0F5B-4710...@microsoft.com...
So is it possible to 'visualize' the gui I am constructing in SDI, in the
same way as one can drag and drop onto the Dialog in a Dialog based MFC app?
I cant see how I can edit the dialog resource of my SDI app. I can only see
one dialog which is the IDD_ABOUTBOX, or maybe I'm missing something
completely obvious.
The reason I decided to switch to SDI is because I thought (could be very
wrong here) that it would allow me to use custom made buttons and such like
(nice graphics) instead of using the default toolbox controls?
Again, I am learning so all this is quite new (and fun!). Thanks for your
insights.
Best regards,
David
> Hi David,
>
> Nope, I havent yet considered using CFormView. To be honest I dont really
> know too much about it! Thats another thing I'll have to have a read up on. I
> presume it is useful for doing interfaces with buttons and textfields and
> such like?
>
David:
Please don't top-post.
I think several of the answers you got here (not mine) assumed that you
were using a FormView. When you create an SDI project in the wizard,
there is a place where you can choose the base class of the view (CView,
CScrollView, CFormView, ...). It is possible to change the base class
afterward, but you need to know what you are doing.
HTH,
David Wilkinson
When you create a new project tell it to make the base view a CFormView and
it will create a dialog template for you.
Tom
"David++" <Da...@discussions.microsoft.com> wrote in message
news:B684DEB1-B9F4-4EBD...@microsoft.com...
:o)
"David Wilkinson" <no-r...@effisols.com> wrote in message
news:ue3m%23P0zF...@TK2MSFTNGP14.phx.gbl...
David,
I tried the CFormView in MFC. I'm using .NET 2003 and there is an option
like you said where you can change the base class to be CFormView. This does
indeed give a dialog onto which you can drag and drop components. There seems
to be so many options! Just like life, so many options, so little time :-)
Thanks for everyones help today,
Best Regards,
David
Tom
"David++" <Da...@discussions.microsoft.com> wrote in message
news:4406CAB4-E464-4099...@microsoft.com...
>Yes. When you use a CFormView it's just like having a dialog in a view.
>The difference between a dialog app and an SDI (form view) based app is
>mostly the use of "views" and the addition of a menu and toolbar in a frame
>window (a different sort of look).
>
>When you create a new project tell it to make the base view a CFormView and
>it will create a dialog template for you.
>
>Tom
Is OnInitialUpdate the best place to put the form's
initialization code (since there's no more OnInitDialog) or
is there somewhere more appropriate. I've only been using
SDI for about a year but still wonder if there's a better
way to do it than using OnInitialUpdate.
Thanks,
Dave
Oops, I just reread the whole thread and I saw OnCreate was
used. Is that the best virtual function to put the code that
used to be in OnInitDialog?
OnInitialUpdate() works great for doing either so I typically do all of my
initialization there.
Tom
"Dave" <d...@nospam.com.net> wrote in message
news:1fqrk19ohibj0vpr1...@4ax.com...
>You can only put code that doesn't need to access windows or controls there.
>For example, you could initialize variables in the constructor on
>OnCreate(), but you could set text to a control because the windows aren't
>created yet.
>
>OnInitialUpdate() works great for doing either so I typically do all of my
>initialization there.
I think I might've found that out the hard way before. It
seems like OnCreate would otherwise have been a good place
to init the formview.
Another thing I learned the hard way was that
OnInitialUpdate can be entered more than once and so I had
to add code to prevent multiple initializations, such as a
static flag. That's why I was hoping to find a cleaner way
to handle the dialog init.