c++ 如何在基于对话框的MFC项目上创建自定义控件(Visual Studio 2012)

icomxhvb  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(172)

(抱歉我的英语不好)我正在尝试创建一个基于对话框的MFC项目(Visual Studio 2012)的自定义控件。这是我创建基于MFC对话框的项目时的设置:
MFC应用程序向导设置:

自定义控件属性:

当我把自定义控件对话框,总是有编译错误。
日志:警告:对话框创建失败,因此应用程序意外终止。警告:如果正在对话框上使用MFC控件,则无法#define_AFX_NO_MFC_CONTROLS_IN_DIALOGS。程序"[0x2524] CustomControl.exe"已退出,代码为0(0x0)。


#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;

};
  • ---------------------------------我的自定义控件. 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 : 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

有谁能告诉我有什么问题吗?非常感谢。

cld4siwp

cld4siwp1#

我看过同样的例子,但是我的构造函数看起来不同。

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

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

您还缺少MESSAGE_MAP的实现

相关问题