Click here to Skip to main content
15,919,879 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralOnSetFont Pin
Vancouver7-Nov-03 13:34
Vancouver7-Nov-03 13:34 
GeneralRe: OnSetFont Pin
Antti Keskinen8-Nov-03 9:41
Antti Keskinen8-Nov-03 9:41 
GeneralOnSetFont Pin
Vancouver8-Nov-03 19:55
Vancouver8-Nov-03 19:55 
GeneralSharing MFC objects among threads... Pin
Suhail Yousaf2-Nov-03 19:16
Suhail Yousaf2-Nov-03 19:16 
GeneralFiles and folders Pin
Selevercin2-Nov-03 17:26
Selevercin2-Nov-03 17:26 
GeneralRe: Files and folders Pin
Michael Dunn2-Nov-03 20:04
sitebuilderMichael Dunn2-Nov-03 20:04 
Generaladding a cbutton to a custom control Pin
mindows2-Nov-03 17:11
mindows2-Nov-03 17:11 
GeneralRe: adding a cbutton to a custom control Pin
Antti Keskinen2-Nov-03 23:03
Antti Keskinen2-Nov-03 23:03 
I'm a 'purist' MFC dude, so I wouldn't mix API and MFC together (CWnd with custom painted buttons (Non-CButton's))

Short explanation:

Use CButton as a static member of your CWnd-derived class. Call it's 'Create' in your derived class's WM_CREATE handler, and leave 'WS_VISIBLE' style out. Display and position the button with SetWindowPos in your WM_PAINT handler. The 'DestroyWindow' should be called automatically for all child windows when WM_DESTROY is posted to your CWnd-derived class's window object's message queue. To make sure, call it yourself in your WM_DESTROY handler.

Detailed explanation:

First, add a static member CButton m_TheButton to your class's header file, and order your constructor to call the CButton's constructor, if necessary. You can also create a CButton pointer and use 'new' to create a dynamic button. Both solutions are fine, the latter being a bit more flexible memory-wise.

Now, your constructed class contains a constructed CButton object. To create the button control, you call this member's 'Create' function, and specify the control's properties (Is child, parent is your control / Sends messages to parent etc etc). Note that when you create the button, you don't need to make it visible. See below.

A good place to call Create for the button control would be in the WM_CREATE message handler of your parent. There, you would create the button, specifying it as non-visible (leave WS_VISIBLE out). Next, go to your WM_PAINT handler, and in there use SetWindowPos to position the control to it's correct place, giving it the correct size, and making it visible. This way, the button is 'Created' only once, but if your window gets repainted, the button will remain in it's specified position and size.

Now, if you write a message handler for WM_COMMAND and make it check if the ID is the button's ID, you can handle messages caused by the user clicking on the button. As the custom control is the parent, the WM_COMMAND messages are posted to it's queue. So, overwrite WM_COMMAND handler for your custom control.

Hope this helps. If it doesn't, I can write a small app which demonstrates "a CWnd object which has a CButton child in it's middle" for you Smile | :)

Note that if you need to use the custom control inside a frame window (SDI Frame, for example), then you should derive it from CCtrlView instead of CWnd. CCtrlView supports all "views which have controls inside them". The Tab Window control (Window with tabs to select different views) is a perfect example of this. CCtrlView is a parent for views such as CEditView (View with Edit control) etc.

If you need, I can write an example of this also, it's good practise Smile | :)

Greets,
Antti Keskinen

----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
GeneralRe: adding a cbutton to a custom control Pin
mindows3-Nov-03 11:54
mindows3-Nov-03 11:54 
GeneralRe: adding a cbutton to a custom control Pin
Antti Keskinen5-Nov-03 1:44
Antti Keskinen5-Nov-03 1:44 
QuestionHow to manage objects unknown at run-time Pin
Steve Messer2-Nov-03 16:42
Steve Messer2-Nov-03 16:42 
AnswerRe: How to manage objects unknown at run-time Pin
Christian Graus2-Nov-03 17:08
protectorChristian Graus2-Nov-03 17:08 
GeneralRe: How to manage objects unknown at run-time Pin
Steve Messer2-Nov-03 21:26
Steve Messer2-Nov-03 21:26 
GeneralRe: How to manage objects unknown at run-time Pin
Christian Graus3-Nov-03 9:07
protectorChristian Graus3-Nov-03 9:07 
GeneralRe: How to manage objects unknown at run-time Pin
Steve Messer3-Nov-03 10:23
Steve Messer3-Nov-03 10:23 
GeneralRe: How to manage objects unknown at run-time Pin
Steve S3-Nov-03 23:02
Steve S3-Nov-03 23:02 
GeneralFloat window in MDI application Pin
novachen2-Nov-03 16:35
novachen2-Nov-03 16:35 
QuestionHow can I put my window under any other application window? Pin
golden06062-Nov-03 16:24
golden06062-Nov-03 16:24 
GeneralText in the taskbar vs. title bar Pin
Vancouver2-Nov-03 15:21
Vancouver2-Nov-03 15:21 
GeneralFoundation Problem, #include Problem Pin
Mehran Ziadloo2-Nov-03 15:12
Mehran Ziadloo2-Nov-03 15:12 
GeneralRe: Foundation Problem, #include Problem Pin
ZoogieZork2-Nov-03 15:21
ZoogieZork2-Nov-03 15:21 
GeneralRe: Foundation Problem, #include Problem Pin
Christian Graus2-Nov-03 15:22
protectorChristian Graus2-Nov-03 15:22 
GeneralProblem with CAsyncSocket Pin
Daniel13242-Nov-03 14:30
Daniel13242-Nov-03 14:30 
GeneralHelp with file management... Pin
Snyp2-Nov-03 14:18
Snyp2-Nov-03 14:18 
GeneralRe: Help with file management... Pin
Ravi Bhavnani2-Nov-03 15:03
professionalRavi Bhavnani2-Nov-03 15:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.