CreateNewFrame()跟踪

本文详细介绍了在MFC中创建SDI和MDI文档时,如何使用CreateNewFrame()函数创建框架窗口的过程。从CSingleDocTemplate的OpenDocumentFile()开始,到CMDIFrameWnd的LoadFrame(),以及PreCreateWindow()的多次调用,展示了框架窗口和视图的创建步骤。此外,还提到了窗口类的注册、资源的加载以及在创建过程中的各种细节处理,如窗口风格、菜单和控件的创建。
摘要由CSDN通过智能技术生成

在新建SDI文档的过程中,框架指针为空(pFrame = NULL),需要根据新创建的文档指针pDocument来CreateNewFrame;在新建或打开MDI文档的过程中,则一直需要CreateNewFrame。

1       CDocument* CSingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,BOOL bMakeVisible)

{

       pDocument = CreateNewDocument();///创建一个新文档

       ...

        CFrameWnd* pFrame = CreateNewFrame(pDocument, NULL);

       ...

}

2       CFrameWnd* CDocTemplate::CreateNewFrame(CDocument* pDoc, CFrameWnd* pOther)
{

       if (pDoc != NULL)

              ASSERT_VALID(pDoc);

       // create a frame wired to the specified document

 

       ASSERT(m_nIDResource != 0); // must have a resource ID to load from

       CCreateContext context;

       context.m_pCurrentFrame = pOther;

       context.m_pCurrentDoc = pDoc;

       context.m_pNewViewClass = m_pViewClass;

       context.m_pNewDocTemplate = this;

 

       if (m_pFrameClass == NULL)

       {

              TRACE0("Error: you must override CDocTemplate::CreateNewFrame./n");

              ASSERT(FALSE);

              return NULL;

       }

      CFrameWnd* pFrame = (CFrameWnd*)m_pFrameClass->CreateObject();//利用CRunTimeClass真正创建框架指针

       if (pFrame == NULL)

       {

              TRACE1("Warning: Dynamic create of frame %hs failed./n",

                     m_pFrameClass->m_lpszClassName);

              return NULL;

       }

       ASSERT_KINDOF(CFrameWnd, pFrame);

 

       if (context.m_pNewViewClass == NULL)

              TRACE0("Warning: creating frame with no default view./n");

 

       // create new from resource

       if (!pFrame->LoadFrame(m_nIDResource,

                     WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,   // default frame styles

                     NULL, &context))

       {

              TRACE0("Warning: CDocTemplate couldn't create a frame./n");

              // frame will be deleted in PostNcDestroy cleanup

              return NULL;

       }

}

3      BOOL CFrameWnd/CMDIFrameWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,  CWnd* pParentWnd, CCreateContext* pContext)
{

       // only do this once

       ASSERT_VALID_IDR(nIDResource);

       ASSERT(m_nIDHelp == 0 || m_nIDHelp == nIDResource);

WIC (Windows Imaging Component) 是一个 Windows 平台上的图像编解码器框架,它提供了一个统一的编解码器接口,使得开发人员能够轻松地读取、写入、转换和处理各种图像格式。 要使用 WIC 存储 PNG 图片,可以按照以下步骤进行操作: 1. 创建 WIC 编码器实例。 ```c++ IWICImagingFactory *pFactory = NULL; IWICBitmapEncoder *pEncoder = NULL; // 创建 WIC 工厂 CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFactory)); // 创建 PNG 编码器 pFactory->CreateEncoder(GUID_ContainerFormatPng, NULL, &pEncoder); ``` 2. 创建一个 WIC 编码器输出流。 ```c++ IStream *pStream = NULL; pFactory->CreateStream(&pStream); // 打开输出流 pStream->InitializeFromFilename(L"output.png", GENERIC_WRITE); // 将输出流与编码器关联 pEncoder->Initialize(pStream, WICBitmapEncoderNoCache); ``` 3. 创建一个 WIC 编码器帧,并将要存储的图像数据写入帧中。 ```c++ IWICBitmapFrameEncode *pFrame = NULL; pEncoder->CreateNewFrame(&pFrame, NULL); // 初始化帧 pFrame->Initialize(NULL); // 设置帧大小和格式 pFrame->SetSize(width, height); pFrame->SetPixelFormat(&pixelFormat); // 将图像数据写入帧 pFrame->WritePixels(height, stride, imageSize, pData); // 关闭帧 pFrame->Commit(); // 关闭编码器 pEncoder->Commit(); ``` 4. 释放资源。 ```c++ pFrame->Release(); pStream->Release(); pEncoder->Release(); pFactory->Release(); ``` 其中,`width` 和 `height` 分别表示图像的宽度和高度,`pixelFormat` 表示图像的像素格式,`stride` 表示每一行像素数据的字节数,`imageSize` 表示图像的总大小,`pData` 表示指向图像数据的指针。 需要注意的是,WIC 框架并不支持所有的 PNG 特性,例如 alpha 通道混合模式、灰度图像、动画等,因此在使用 WIC 存储 PNG 图像时需要注意选择合适的像素格式和图像处理方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值