How can I create a control inherit from CWnd

I want to write a control inherit from CWnd,what should I do if I want to create it? I call the function CWnd::create(),but it fails.Please give me a instance.

I am waiting online. thanks.
closed account (z05DSL3A)
This might help:
Creating Custom Controls
By Chris Maunder
http://www.codeproject.com/KB/miscctrl/customcontrol.aspx
thanks for your help,this link is useful . I have an other problem.I use CDC in OnPaint() function.but Endless loop happen.
I don't know why,could you help me?thanks.
the code is like this:
CRect rect; //begin
GetClientRect(rect);
CDC dc;
CBrush brush;
COLORREF color=RGB(155,155,155);
brush.CreateSolidBrush(color);
dc.FillRect(&rect,&brush);//if break point come to here,it will go to begin. the program can not go down.
brush.DeleteObject();
dc.DeleteDC();

why FillRect() will lead to endless loop?
closed account (z05DSL3A)
try somthing like:
1
2
3
4
5
6
7
8
9
10
    CPaintDC dc(this); // device context for painting
   
    CRect rect;
    GetClientRect(rect);

    CBrush brush;
    COLORREF color=RGB(155,155,155);
    brush.CreateSolidBrush(color);
    dc.FillRect(&rect,&brush);
    //... 
Last edited on
if break point come to here,it will go to begin. the program can not go down.
The problem is the break point: if you continue after the break point the window must refresh (hence OnPaint() is called) because it was hidden. But then you will face the break point again. So you cannot set a break point within your OnPaint() function.
closed account (z05DSL3A)
The problem is the break point: ... So you cannot set a break point within your OnPaint() function.

I'm not entirely sure that that is the case. internal WM_PAINT message is sent only once by Windows. OnPaint() will not be called again until the window is invalidated or RedrawWindow() is called (with RDW_INTERNALPAINT flag set).

In the code the OP posted, the DC doesn't look like it is set up correctly
1
2
3
4
5
6
7
//...
CDC dc;    
CBrush brush;
COLORREF color=RGB(155,155,155);
brush.CreateSolidBrush(color);
dc.FillRect(&rect,&brush);// MFC will beak here as the DC is not setup correctly 
//... 
I agree: OnPaint() cannot be debugged by means of a breakpoint. I also agree that WM_PAINT is only sent if no other WM_PAINT is in queue. But then again, OnPaint() is called after retrieving WM_PAINt via GetMessage(), right? That I think is enough to produce another WM_PAINT. It would be interesting to know if there is a point where a breakpoint could be set without producing another WM_PAINT message. I haven't found one, though. Maybe before the call to BeginPaint()?
closed account (z05DSL3A)
I agree: OnPaint() cannot be debugged by means of a breakpoint.
Strange because after posting I set up a test project (VS2010) and and debugged the OnPaint() method with a break point and no problems.
It is strange for sure. When the breakpoint activates, is your window obscured by Visual Studio? Because this only happens if the breakpoint causes the Visual Studio (or whatever IDE, my apologies for forgetting there are others) window to cover part of the test window.
closed account (z05DSL3A)
It has been a while since I last used MFC but I have a feeling that Just because there is WM_PAINT it does not mean that OnPaint will be called. There is a habit of cashing the view and painting that back if nothing has changed (window is invalidated).

Anyway The OPs code was tested and MFC breaks at the point indicated.
I did observe the same as webJose. But Grey Wolf you're right that CDC dc; doesn't look right and I'd say that it has to be CPaintDC dc(this); // device context for painting But I'd expect that the former simply does nothing.

I haven't used MFC since a couple of years. I'm somewhat surprised that there're ppl using it dispite of by far better alternatives
I guest the CDC::FillRect() will refresh the control or it produce the message ON_PAINT.so the OnPaint() function
called again. and my program is running on VS2005.

thanks for everyone.
closed account (z05DSL3A)
fanrongqi wrote:
I guest the CDC::FillRect() will refresh the control or it produce the message ON_PAINT.so the OnPaint() function called again.
If CDC::FillRect() did refresh/post WM_PAINT, then your app would sit in an endless loop of drawing the controls, it does not.
Topic archived. No new replies allowed.