如何完美的继承CView,并把它添加到对话框程序里?

laogong5i0 2013-05-12 02:54:12
我自己写了个类用来显示图片的,
并且样式已经设为WS_EX_CLIENTEDGE了, 为什么还是没有立体效果?


在对话框里的初始函数OnInitDialog()加入了引用代码
testView* m_view = new testView;
((CView*)m_view)->Create(NULL, NULL, WS_EX_CLIENTEDGE, CRect(250,120,800,520), this, NULL, NULL);
m_view->ShowWindow(SW_SHOW);
m_view->OnInitialUpdate();

下面是testView类的代码
.h文件
#pragma once

#pragma comment(lib, "../debug/valib.lib")
#include "valib.h"
// testView 视图

VA_NS_BEGIN
class testView : public CView
{
DECLARE_DYNCREATE(testView)
VASprite* m_rootSP;
public:
testView(); // 动态创建所使用的受保护的构造函数
virtual ~testView();

public:
virtual void OnFinalRelease();
virtual void OnDraw(CDC* pDC); // 重写以绘制该视图
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif

protected:
DECLARE_MESSAGE_MAP()
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()
public:
virtual void OnInitialUpdate();
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg void OnPaint();
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};


VA_NS_END


.cpp文件
// testView.cpp : 实现文件
//

#include "stdafx.h"
#include "ImgTools.h"
#include "testView.h"


// testView
US_VA_NS
IMPLEMENT_DYNCREATE(testView, CView)

testView::testView()
:m_rootSP(NULL)
{

EnableAutomation();
}

testView::~testView()
{
}

void testView::OnFinalRelease()
{
// 释放了对自动化对象的最后一个引用后,将调用
// OnFinalRelease。基类将自动
// 删除该对象。在调用该基类之前,请添加您的
// 对象所需的附加清理代码。

CView::OnFinalRelease();
}

BEGIN_MESSAGE_MAP(testView, CView)
ON_WM_TIMER()
ON_WM_PAINT()
ON_WM_LBUTTONDBLCLK()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(testView, CView)
END_DISPATCH_MAP()

// 注意: 我们添加 IID_ItestView 支持
// 以支持来自 VBA 的类型安全绑定。此 IID 必须同附加到 .IDL 文件中的
// 调度接口的 GUID 匹配。

// {ED946E81-95CB-491A-90E1-3EA850D4E5B7}
static const IID IID_ItestView =
{ 0xED946E81, 0x95CB, 0x491A, { 0x90, 0xE1, 0x3E, 0xA8, 0x50, 0xD4, 0xE5, 0xB7 } };

BEGIN_INTERFACE_MAP(testView, CView)
INTERFACE_PART(testView, IID_ItestView, Dispatch)
END_INTERFACE_MAP()


// testView 绘图

void testView::OnDraw(CDC* pDC)
{
//CDocument* pDoc = GetDocument();
// TODO: 在此添加绘制代码
CView::OnInitialUpdate();
}


// testView 诊断

#ifdef _DEBUG
void testView::AssertValid() const
{
CView::AssertValid();
}

#ifndef _WIN32_WCE
void testView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif
#endif //_DEBUG



void testView::OnInitialUpdate()
{
if(!m_hWnd)return;

VADirector::sharedDirector()->init(m_hWnd);

m_rootSP = new VASprite("root");
VASprite* m_cake;
m_cake = new VASprite("cake");
m_rootSP->addChild(m_cake);

TCHAR szCurrDir[_MAX_PATH];
::GetCurrentDirectory(_MAX_PATH, szCurrDir);;
TCHAR m_bgName[_MAX_PATH];
wsprintf(m_bgName,L"%s\\res\\img.jpg",szCurrDir);
TCHAR m_cakeName[_MAX_PATH];
wsprintf(m_cakeName,L"%s\\res\\cake.png", szCurrDir);

Bitmap* m_imgBG = Bitmap::FromFile(m_bgName);
Bitmap* m_imgCake = Bitmap::FromFile(m_cakeName);
m_rootSP->setBitmap(m_imgBG);
m_cake->setBitmap(m_imgCake);

CView::OnInitialUpdate();
}



// testView 消息处理程序


void testView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

CView::OnTimer(nIDEvent);
}


void testView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CView::OnPaint()

//增加精灵
m_rootSP->draw();

CView::OnPaint();
}


void testView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

CView::OnLButtonDblClk(nFlags, point);
}


void testView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

CView::OnLButtonDown(nFlags, point);
}


void testView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

CView::OnLButtonUp(nFlags, point);
}

...全文
270 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
laogong5i0 2013-05-15
  • 打赏
  • 举报
回复
引用 7 楼 schlafenhamster 的回复:
view->Create(NULL,NULL, WS_EX_CLIENTEDGE, rect, this, NULL, NULL); 第3个是 DWORD dwStyle,不是 Extended style 用 CreateEx 或者加: ModifyStyleEx
哈哈!!加了 view->ModifyStyleEx(WS_EX_STATICEDGE|WS_EX_WINDOWEDGE||WS_EX_DLGMODALFRAME, WS_EX_CLIENTEDGE,SWP_DRAWFRAME); 总算搞定了!! 原来这么简单,居然困扰了我好几天!! thank you!
schlafenhamster 2013-05-15
  • 打赏
  • 举报
回复
view->Create(NULL,NULL, WS_EX_CLIENTEDGE, rect, this, NULL, NULL); 第3个是 DWORD dwStyle,不是 Extended style 用 CreateEx 或者加: ModifyStyleEx
laogong5i0 2013-05-15
  • 打赏
  • 举报
回复
引用 4 楼 tiger9991 的回复:
这么在意框的话就自己画一个,View毕竟不是控件
这问题不解决的话,那我不是每次都要自己画外框。 那样会很蛋痛的! 而且默认的外框是在客户区外的!不会覆盖到我的内容。 如果自己画一个2px的外框的话,那窗口的内容就有2个像素被遮住了! 是不是有办法画客户区外的东西呢? 难道要嵌套的画吗?蛋痛得不得了!!
laogong5i0 2013-05-15
  • 打赏
  • 举报
回复
引用 3 楼 Shen_Juntao 的回复:
应该有呀,你怎么写的?

因为MFC很不熟,所以一直都怀疑是我自己的代码为题下面是代码,帮忙看看吧!
是不是Create的时候参数错了?
下面是写在对话框的OnInitDialog()函数里的。就一个简单的创建。
CRect clientRect;
this->GetClientRect(clientRect);
CRect rect(10, 10, clientRect.Width()-10, clientRect.Height()-10);
TestVIew* view = new TestVIew;
view->Create(NULL,NULL, WS_EX_CLIENTEDGE, rect, this, NULL, NULL);
view->ShowWindow(SW_SHOW);

TestVIew的代码
.h文件
#pragma once
// TestVIew
class TestVIew : public CWnd
{
DECLARE_DYNAMIC(TestVIew)

public:
TestVIew();
virtual ~TestVIew();

protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnPaint();
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);

private:
void drawBackground(CDC* dc);
};

.cpp文件
// TestVIew.cpp : implementation file
//

#include "stdafx.h"
#include "test.h"
#include "TestVIew.h"
// TestVIew

IMPLEMENT_DYNAMIC(TestVIew, CWnd)

TestVIew::TestVIew()
{
}

TestVIew::~TestVIew()
{
}

BEGIN_MESSAGE_MAP(TestVIew, CWnd)
END_MESSAGE_MAP()

// TestVIew message handlers

void TestVIew::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
/*CRect rect;
this->GetWindowRect(rect);
dc.Draw3dRect(0, 0, rect.right, rect.bottom, RGB(192,192,192),RGB(0,0,0) );*/

drawBackground(&dc);

// Do not call CWnd::OnPaint() for painting messages
CWnd::OnPaint();
}


LRESULT TestVIew::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch(message){
case WM_CREATE:
Invalidate(true);
break;
case WM_PAINT:
OnPaint();
break;
}
return CWnd::WindowProc(message, wParam, lParam);
}

void TestVIew::drawBackground(CDC* dc)
{
CRect clientRect;
this->GetClientRect(clientRect);

CBrush newBrush;
CBrush bgGrush;

CPen newPen;
newPen.CreatePen(PS_NULL,5, RGB(0, 255, 255));
dc->SelectObject(&newPen);

bgGrush.CreateSolidBrush(RGB(210,210, 210));
dc->SelectObject(bgGrush);
dc->Rectangle(clientRect);

newBrush.CreateSolidBrush(RGB(255, 255, 255));
dc->SelectObject(&newBrush);

int m_space = 22;
for(int i = 0; i<=clientRect.Height()/m_space; i++){
CRect lineRcet(0,i*m_space, clientRect.Width(), i*m_space+m_space/2);
dc->Rectangle(lineRcet);
}
for(int i = 0; i<=clientRect.Width()/m_space; i++){
CRect lineRcet(i*m_space, 0, i*m_space+m_space/2, clientRect.Width());
dc->Rectangle(lineRcet);
}
//dc.SelectObject(oldBrush);
//dc.SelectObject(oldPen);
}


TestVIew类只是简单的画了背景,没有特别的操作啊!
运行后还是没有外框,求解!!或者你给我个例子啦!!
傻X 2013-05-14
  • 打赏
  • 举报
回复
这么在意框的话就自己画一个,View毕竟不是控件
沈-军涛 2013-05-13
  • 打赏
  • 举报
回复
对话框不建议用CView类,CView是应用于文档视图结构的,如果只是显示继承自CWnd更好. 至于没有外框是不是因为你自己画的原因呀?还不清楚
沈-军涛 2013-05-13
  • 打赏
  • 举报
回复
应该有呀,你怎么写的?
laogong5i0 2013-05-13
  • 打赏
  • 举报
回复
引用 1 楼 Shen_Juntao 的回复:
对话框不建议用CView类,CView是应用于文档视图结构的,如果只是显示继承自CWnd更好. 至于没有外框是不是因为你自己画的原因呀?还不清楚
但即使是继承CWnd,也没有外框啊!为什么? 难道外框要自己画吗?

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧