简体   繁体   中英

How to create a custom controls on a dialog based MFC project (visual studio 2012)

(Sorry for my poor English) I'm trying to create a custom control on dialog based MFC project (visual studio 2012). This is the setting when i create MFC Dialog based project :

MFC App Wizard setting : 在此处输入图像描述

custom control proverty:在此处输入图像描述

when i put custom control on Dialog, there's always compile error.

log : Warning: dialog creation failed, so application is terminating unexpectedly. Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS. The program '[0x2524] CustomControl.exe' has exited with code 0 (0x0).

-------------------------------------------MyCustomControl.h----------------------------------

#pragma once


// MyCustomControl
#define MYWNDCLASS "MyDrawPad"
class MyCustomControl : public CWnd
{
    DECLARE_DYNAMIC(MyCustomControl)

public:
    MyCustomControl();
    virtual ~MyCustomControl();

protected:
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);

    DECLARE_MESSAGE_MAP()
private:
    CDC cDC;
    BOOL RegisterWndClass();
    CPoint oldpt;
    BOOL flag;

};

---------------------------------MyCustomControl.cpp---------------------------------------

// MyCustomControl.cpp : implementation file
//

#include "stdafx.h"
#include "CustomControl.h"
#include "MyCustomControl.h"


// MyCustomControl

IMPLEMENT_DYNAMIC(MyCustomControl, CWnd)

MyCustomControl::MyCustomControl()
{

}

MyCustomControl::~MyCustomControl()
{
}


BEGIN_MESSAGE_MAP(MyCustomControl, CWnd)
END_MESSAGE_MAP()



// MyCustomControl message handlers

/////////////////////////////////////////////////////////////////////////////
// MyCustomControl message handlers

BOOL MyCustomControl::RegisterWndClass()
{
    WNDCLASS windowclass;
    HINSTANCE hInst = AfxGetInstanceHandle();

    //Check weather the class is registerd already
    if (!(::GetClassInfo(hInst, MYWNDCLASS, &windowclass)))
    {
        //If not then we have to register the new class
        windowclass.style = CS_DBLCLKS;// | CS_HREDRAW | CS_VREDRAW;
        windowclass.lpfnWndProc = ::DefWindowProc;
        windowclass.cbClsExtra = windowclass.cbWndExtra = 0;
        windowclass.hInstance = hInst;
        windowclass.hIcon = NULL;
        windowclass.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
        windowclass.hbrBackground = ::GetSysColorBrush(COLOR_WINDOW);
        windowclass.lpszMenuName = NULL;
        windowclass.lpszClassName = MYWNDCLASS;


        if (!AfxRegisterClass(&windowclass))
        {
            AfxThrowResourceException();
            return FALSE;
        }
    }

    return TRUE;

}


int MyCustomControl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    if (CWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    // TODO: Add your specialized creation code here


    return 0;
}


void MyCustomControl::OnLButtonDown(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default

    if(flag==FALSE)
    {
        oldpt=point;
        flag=TRUE;
    }

    //CWnd::OnLButtonDown(nFlags, point);
}

void MyCustomControl::OnMouseMove(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default

    if(flag==TRUE)
    {

        CDC *d=GetDC();

        d->MoveTo(oldpt);
        d->LineTo(point);


        oldpt=point;

        ReleaseDC(d);
    }

    //CWnd::OnMouseMove(nFlags, point);
}

void MyCustomControl::OnLButtonUp(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default

    flag=FALSE;

    //CWnd::OnLButtonUp(nFlags, point);
}

------------------------------------------stdafx.h--------------------------------------

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently

#pragma once

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN            // Exclude rarely-used stuff from Windows headers
#endif

#include "targetver.h"

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // some CString constructors will be explicit

// turns off MFC's hiding of some common and often safely ignored warning messages
#define _AFX_ALL_WARNINGS

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdisp.h>        // MFC Automation classes



#ifndef _AFX_NO_OLE_SUPPORT
#include <afxdtctl.h>           // MFC support for Internet Explorer 4 Common Controls
#endif
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>             // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

#include <afxcontrolbars.h>     // MFC support for ribbons and control bars



#ifdef _UNICODE
#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
#endif

Could anyone can told me what's the problems? thank you very much.

I have seen the same example, but my constructor looks different. See the code below

MyCustomControl::MyCustomControl()
{
    //Register My window class
    RegisterWndClass();

    flag=FALSE; //Sets the drawing flag off
}

You are also missing the implementation of the MESSAGE_MAP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM