gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:51:40 35 4
gpt4 key购买 nike

(抱歉我的英语不好)我正在尝试在基于对话框的 MFC 项目(visual studio 2012)上创建自定义控件。这是我创建基于 MFC 对话框的项目时的设置:

MFC 应用程序向导设置: enter image description here

自定义控件属性: enter image description here

当我在Dialog上放置自定义控件时,总是会出现编译错误。

日志:警告:对话框创建失败,因此应用程序意外终止。警告:如果您在对话框上使用 MFC 控件,则不能#define _AFX_NO_MFC_CONTROLS_IN_DIALOGS。程序“[0x2524] CustomControl.exe”已退出,代码为 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

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

最佳答案

我见过同样的例子,但我的构造函数看起来不一样。看下面的代码

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

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

您还缺少 MESSAGE_MAP 的实现

关于c++ - 如何在基于对话框的 MFC 项目上创建自定义控件 (visual studio 2012),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27602464/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com