mfc >> looking for custom control sample code

by Z3JlYW5pZQ » Thu, 23 Sep 2004 22:17:13 GMT

Hello, I am trying my first custom control. I need a window in which to
display text formatted in a special way. I have been going down the path of
a custom control and expecting to create a custom control and associated
class. The sample I have found so far comes from

This seems like a simple enough approach. I already have a FormView SDI
type window. I created the dll project within the workspace, and pretty much
copied the code presented (except for the one error I found in the dll init
function with declaring the custom class).

When I run, I get an error after the custom dll is loaded (as seen in trace

Warning: could not create view for frame.
Failed to create client pane/view for frame.
Warning: Window creation failed: GetLastError returns 0x00000000
Warning: failed to create CFrameWnd.

The crash happens in CWnd::CreateDlgIndirect, dlgcore.cpp, at the
assert(hWnd == m_hWnd). hWnd gets assigned earlier at

hWnd = ::CreateDialogIndirect(hInst, lpDialogTemplate,
pParentWnd->GetSafeHwnd(), AfxDlgProc);

I checked all the parameters passed and they look ok, hWnd is still null,
which causes the failed assertion. I can't step inot the call because I end
up only in GetSafeHwnd and back out again. If I delete the custom view from
the CFormView resource, the app loads fine.

I am looking for perhaps some hints as to my problem with the current
approach or another road to go down. Thanks!

mfc >> looking for custom control sample code

by Joseph M. Newcomer » Sat, 25 Sep 2004 09:56:53 GMT

There are a couple ways. One of the simplest is to use a CStatic, subclass it, override
its OnPaint handler, and draw whatever you want. This is by far the simplest method. Just
add a control variable for the static control, which means you must give the static
control and ID other ID_STATIC. Either make the class your derived subclass, or you may
have to hand-edit your .h file to change it from CStatic to CMyCustomControl.

The other is to use a generic CWnd-derived class. The problem with this is that you can't
name the control type in the dialog, so you have to register the window class
(AfxRegisterWndClass, I think is the call) so you can use the class name.

So while the code you are showing is interesting, the code you don't show is the important
code. How are you defining your window class, and how are you specifying it in the dialog
(e.g., show the line from the .rc file that describes the control)?

If you have a registered window class, you must first register the class; if it is in a
DLL, you would do this in the InitInstance handler for the DLL, or in the
DLL_PROCESS_ATTACH handler, and you must uregister the class in the ExitInstance or

This is the most common cause of the symptoms you are describing. Ultimately, this results
in the CFormView dialog not being created, which cascades to a frame-not-created error.

Joseph M. Newcomer [MVP]
MVP Tips:

Similar Threads

1. Looking for some sample codes in C#

Hi all,

I am looking for some sample codes in C#, in which, it can read a schema xsd 
file and use it to validate an xml stream or file. Ideally the sample will 
use XmlValidatingReader.



2. looking for code sample for non-blocking TCP sockets - CSharp/C#

3. Looking for WinSNMP sample code

I'm looking for sample code for WinSNMP. Can someone 
please tell me where to get them? Thanks,

4. Looking for sample code to send email with exchange

5. Looking for sample code

I need to write an app that will (magically) create a bitmap, and display it in a window
of specific width, but (possible) scrollable in height.  Is there something out there that
gets close?  TIA.

#include <standard.disclaimer>
Kevin D Quitt  USA 91387-4454         96.37% of all statistics are made up

6. Creating a Database using the STL: Newbie looking for code samples

7. Delphi 7, Excel - have problem - looking for sample code

Hi all, 

I had an app that worked just fine in D5; no longer under D7 (as I've read 
from others as well). 

Have heard of sample code floating around but cannot find it. Can anyone 
point me in the right direction please.

I need to 

    	- create workbook with shhets
    	- insert data into cells
    	- set come cell properties (bold fint etc.)
TIA !!!!

8. question about custom attributes, please review sample code - CSharp/C#