MFC第三十天 通过CToolBar类开发文字工具栏和工具箱、GDI+边框填充以及基本图形的绘制方法、图形绘制过程的反色线模型和实色模型

文章目录

  • CControlBar
  • 通过CToolBar类开发文字工具栏和工具箱
    • CMainFrame.h
    • CApp
    • CMainFrm.cpp
    • CMainView.h
    • CMainView.cpp
    • CEllipse.h
    • CEllipse.cpp
    • CLine.h
    • CLine.cpp
    • CRRect .h
    • CRRect .cpp

CControlBar

class AFX_NOVTABLE CControlBar : public CWnd{DECLARE_DYNAMIC(CControlBar)protected:		// ConstructionCControlBar();public:			// Attributesint GetCount() const;CWnd *m_pInPlaceOwner;void SetInPlaceOwner(CWnd *pWnd);// for styles specific to CControlBarDWORD GetBarStyle();void SetBarStyle(DWORD dwStyle);
	BOOL m_bAutoDelete;// getting and setting border spacevoid SetBorders(LPCRECT lpRect);void SetBorders(int cxLeft = 0, int cyTop = 0, int cxRight = 0, int cyBottom = 0);CRect GetBorders() const;CFrameWnd* GetDockingFrame() const;BOOL IsFloating() const;virtual CSize CalcFixedLayout(BOOL bStretch, BOOL bHorz);virtual CSize CalcDynamicLayout(int nLength, DWORD nMode);// Operationsvoid EnableDocking(DWORD dwDockStyle);

CBRS_控制条属性

// ControlBar styles(理论上包括状态栏、工具栏等)
#define CBRS_ALIGN_LEFT     0x1000L
#define CBRS_ALIGN_TOP      0x2000L
#define CBRS_ALIGN_RIGHT    0x4000L
#define CBRS_ALIGN_BOTTOM   0x8000L
#define CBRS_ALIGN_ANY      0xF000L#define CBRS_BORDER_LEFT    0x0100L
#define CBRS_BORDER_TOP     0x0200L
#define CBRS_BORDER_RIGHT   0x0400L
#define CBRS_BORDER_BOTTOM  0x0800L
#define CBRS_BORDER_ANY     0x0F00L
#define CBRS_TOOLTIPS       0x0010L 小字条提示(\n后半)
#define CBRS_FLYBY          0x0020L  状态栏提示的另一半文字
#define CBRS_FLOAT_MULTI    0x0040L
#define CBRS_BORDER_3D      0x0080L
#define CBRS_HIDE_INPLACE   0x0008L
#define CBRS_SIZE_DYNAMIC   0x0004L 可以拉扯工具栏变形
#define CBRS_SIZE_FIXED     0x0002L 固定形状(不可拉扯)
#define CBRS_FLOATING       0x0001L  #define CBRS_GRIPPER        0x00400000L 掐子(去掉之后就是锁定工具栏的属性)
#define CBRS_ORIENT_HORZ    (CBRS_ALIGN_TOP|CBRS_ALIGN_BOTTOM)
#define CBRS_ORIENT_VERT    (CBRS_ALIGN_LEFT|CBRS_ALIGN_RIGHT)
#define CBRS_ORIENT_ANY     (CBRS_ORIENT_HORZ|CBRS_ORIENT_VERT)
#define CBRS_ALL            0x0040FFFFL// the CBRS_ style is made up of an alignment style and a draw border style
//  the alignment styles are mutually exclusive
//  the draw border styles may be combined
#define CBRS_NOALIGN        0x00000000L
#define CBRS_LEFT           (CBRS_ALIGN_LEFT|CBRS_BORDER_RIGHT)
#define CBRS_TOP            (CBRS_ALIGN_TOP|CBRS_BORDER_BOTTOM)
#define CBRS_RIGHT          (CBRS_ALIGN_RIGHT|CBRS_BORDER_LEFT)
#define CBRS_BOTTOM         (CBRS_ALIGN_BOTTOM|CBRS_BORDER_TOP)

通过CToolBar类开发文字工具栏和工具箱

高级工具栏的开发
a)文字工具栏开发:调用CToolBar::SetButtonText和CBoolBar::SetSizes方法;
b)工具箱创建时要指定:CBRS_SIZE_FIXED
调用CToolBar::SetButtonStyle方法,为n个按钮一行做分行属性。

#ifndef PCH_H
#define PCH_H
#include "framework.h"
#include <gdiplus.h>
//图形软件开发的关键架构  公共的基类  虚函数架构  没有实际意义 自己也不能画 也不可以自己建立对象(抽象类)无法实例化 只能由派生类来构造 也必须实现所有的抽象接口 
struct SLayer {  enum STAT {ST_DRAW = 0, //绘制状态ST_NORMAL, //正常状态ST_SELECT,	//选中状态	};STAT m_stat{ ST_DRAW };static CPoint m_last;virtual	void OnLButtonDown(UINT nFlags, CPoint point)=0; //形成多态virtual	void OnLButtonUp(UINT nFlags, CPoint point)=0;virtual	void OnMouseMove(UINT nFlags, CPoint point,CDC * pDC=NULL)=0;virtual	void OnDraw(CDC* pDC)=0;  // 重写以绘制该视图
};
#endif //PCH_H

CMainFrame.h

class CMainFrame : public CMDIFrameWnd{DECLARE_DYNAMIC(CMainFrame)void InitTools();
public:		CMainFrame() noexcept;
public:		virtual BOOL PreCreateWindow(CREATESTRUCT& cs); //重写
public:		virtual ~CMainFrame();//实现
protected:  		// 控件条嵌入成员CToolBar	m_toolBox;CToolBar    m_wndToolBar;CStatusBar  m_wndStatusBar;protected:			// 生成的消息映射函数afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);DECLARE_MESSAGE_MAP()
};	
// pch.cpp: 与预编译标头对应的源文件
#include "pch.h"
CPoint SLayer::m_last{ MAXWORD,MAXWORD };  //鼠标移动最终的点 MAXWORD 65535超大值

CApp

在CApp初始化要对GDI+进行初始化 加载 头文件 命名空间
EnableTaskbarInteraction(FALSE);GdiplusStartupInput gdiplusStartupInput;ULONG_PTR gdiplusToken;GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

CMainFrm.cpp

#include "pch.h"
#include "framework.h"
#include "DrawLx.h"
#include "MainFrm.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)ON_WM_CREATE()
END_MESSAGE_MAP()
static UINT indicators[] =
{ID_SEPARATOR,           // 状态行指示器ID_INDICATOR_CAPS,ID_INDICATOR_NUM,ID_INDICATOR_SCRL,
};// CMainFrame 构造/析构
CMainFrame::CMainFrame() noexcept
{
}
CMainFrame::~CMainFrame()
{
}
void CMainFrame::InitTools(){int i = -1,nCount = m_wndToolBar.GetCount();LPCTSTR ts[] ={_T("新建"),_T("打开"),_T("保存"),_T(""),_T("剪切"),_T("拷贝"),_T("粘贴"),_T(""),_T("打印"),_T("帮助")	};while(++i<nCount)m_wndToolBar.SetButtonText(i,ts[i]);CRect rect;m_wndToolBar.GetItemRect(0,rect);m_wndToolBar.SetSizes(rect.Size(), { 16,15 });GetWindowRect(rect);//auto b = m_toolBox.IsFloating();  //b = m_toolBox.IsFloating();m_toolBox.SetButtonStyle(1, TBBS_BUTTON | TBBS_WRAPPED);m_toolBox.SetButtonStyle(3, TBBS_BUTTON | TBBS_WRAPPED);m_toolBox.SetButtonStyle(5, TBBS_BUTTON | TBBS_WRAPPED);FloatControlBar(&m_toolBox, { rect.right - 60,rect.top + 100 });		}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)return -1;if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))	{TRACE0("未能创建工具栏\n");return -1;      // 未能创建}if (!m_toolBox.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER| CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_FIXED) ||!m_toolBox.LoadToolBar(IDR_TOOLBOX)){TRACE0("未能创建工具栏\n");return -1;      // 未能创建}
	if (!m_wndStatusBar.Create(this))	{TRACE0("未能创建状态栏\n");return -1;      // 未能创建}m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));//int nCount = m_wndStatusBar.GetCount();m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);    // 如果不需要可停靠工具栏,则删除这三行m_toolBox.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);DockControlBar(&m_wndToolBar);m_toolBox.SetWindowTextW(_T("工具箱"));m_wndToolBar.SetWindowText(_T("标准"));InitTools();return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{if( !CMDIFrameWnd::PreCreateWindow(cs) )return FALSE;// TODO: 在此处通过修改//  CREATESTRUCT cs 来修改窗口类或样式return TRUE;
}

CMainView.h

class CMainView : public CScrollView{int m_nIndex{ID_DRAW_DRAG}; //工具编号CArray<SLayer*>m_ls;  //类似于蝴蝶的架构
protected: // 仅从序列化创建CMainView() noexcept;DECLARE_DYNCREATE(CMainView)// 特性
public:CMainDoc* GetDocument() const;// 重写
public:virtual void OnDraw(CDC* pDC);  // 重写以绘制该视图virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:virtual void OnInitialUpdate(); // 构造后第一次调用virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
protected:DECLARE_MESSAGE_MAP()
public:afx_msg void OnDrawTools(UINT);afx_msg void OnUpdateDrawTools(CCmdUI* pCmdUI);afx_msg void OnLButtonDown(UINT nFlags, CPoint point);afx_msg void OnLButtonUp(UINT nFlags, CPoint point);afx_msg void OnMouseMove(UINT nFlags, CPoint point);
};

CMainView.cpp

#include "pch.h"
#include "framework.h"
#include "CLine.h"
#include "CPencil.h"
#include "CRecta.h"
#include "CEllipse.h"
#include "CRRect.h"
#include "DrawXq.h"
#endif
#include "CMainDoc.h"
#include "CMainView.h"
CMainView::CMainView() noexcept{     // CMainView 构造/析构
}
CMainView::~CMainView(){
}
BOOL CMainView::PreCreateWindow(CREATESTRUCT& cs)
{//  CREATESTRUCT cs 来修改窗口类或样式return CScrollView::PreCreateWindow(cs);
}
// CMainView 绘图
void CMainView::OnDraw(CDC* pDC) //传来paintDc 因为基类中已经做了这个,你再做是无效的
{auto nCount = m_ls.GetCount();int i = -1;while (++i<nCount){m_ls[i]->OnDraw(pDC);}
}
void CMainView::OnInitialUpdate()
{CScrollView::OnInitialUpdate();CSize sizeTotal;// TODO: 计算此视图的合计大小sizeTotal.cx = sizeTotal.cy = 100;SetScrollSizes(MM_TEXT, sizeTotal);
}
 void CMainView::OnDrawTools(UINT nID)
{m_nIndex = nID;
}void CMainView::OnUpdateDrawTools(CCmdUI* pCmdUI){//pCmdUI->SetCheck(); //这个TRUE的话7个都会亮起来pCmdUI->SetCheck(pCmdUI->m_nID == m_nIndex);}
void CMainView::OnLButtonDown(UINT nFlags, CPoint point){SLayer* pLayer = nullptr;switch (m_nIndex){case ID_DRAW_LINE:pLayer = new CLine;break;case ID_DRAW_RECT:pLayer = new CRecta;break;case ID_DRAW_PENCIL:pLayer = new CPencil;break;
	case ID_DRAW_ELLIPSE:pLayer = new CEllipse;break;case ID_DRAW_RRECT:pLayer = new CRRect;break;}if (pLayer){pLayer->OnLButtonDown(nFlags, point);m_ls.Add(pLayer);}CScrollView::OnLButtonDown(nFlags, point);
}
void CMainView::OnLButtonUp(UINT nFlags, CPoint point)
{CScrollView::OnLButtonUp(nFlags, point);SLayer::m_last ={ MAXWORD,MAXWORD }; //恢复到未开始的状态auto nCount = m_ls.GetCount();if (nCount < 1)return;m_ls[nCount - 1]->OnLButtonUp(nFlags, point);Invalidate();
}
void CMainView::OnMouseMove(UINT nFlags, CPoint point)
{CScrollView::OnMouseMove(nFlags, point);auto nCount = m_ls.GetCount();if (nCount < 1)return;CClientDC dc(this);// this是窗口类  做的是反差色dc.SetROP2(R2_NOT);m_ls[nCount - 1]->OnMouseMove(nFlags, point, &dc);
}

CEllipse.h

#pragma once
#include "pch.h"
class CEllipse : public SLayer
{CRect m_rect;void OnLButtonDown(UINT nFlags, CPoint point); //形成多态void OnLButtonUp(UINT nFlags, CPoint point);void OnMouseMove(UINT nFlags, CPoint point, CDC* pDC);void OnDraw(CDC* pDC);  // 重写以绘制该视图
};

CEllipse.cpp

#include "pch.h"
#include "CEllipse.h"
using namespace Gdiplus;
void CEllipse::OnLButtonDown(UINT nFlags, CPoint point) {m_rect.TopLeft() = point;
}
void CEllipse::OnLButtonUp(UINT nFlags, CPoint point)
{m_rect.BottomRight() = point;m_rect.NormalizeRect();
}
void CEllipse::OnMouseMove(UINT nFlags, CPoint point, CDC* pDC)
{
}
#include<gdiplusbrush.h>
void CEllipse::OnDraw(CDC* pDC) {Graphics g(pDC->GetSafeHdc());Pen pen({ 0xff,0,0,255 }, 3.0); //0xff,0,0,255第一个参数为透明度 第二三四为RGB ,3.0为粗度Point startPoint(m_rect.left, m_rect.top);Point endPoint(m_rect.right, m_rect.bottom);LinearGradientBrush brush(startPoint, endPoint, Color(0x80, 255, 0, 0), Color(0x80, 0, 0, 255));g.FillEllipse(&brush, m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height());g.DrawEllipse(&pen, m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height());
}
	/*Pen p2({ 0xff,0,0xff,0 }, 3.0f);g.DrawLine(&p2, m_rect.left, m_rect.top, m_rect.right, m_rect.bottom);g.DrawEllipse(&pen, m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height());pDC->Ellipse(m_rect);*/

CLine.h

#pragma once
#include "pch.h"
class CLine : public SLayer
{CPoint m_ps,m_pe; //statr-end; void OnLButtonDown(UINT nFlags, CPoint point); //形成多态void OnLButtonUp(UINT nFlags, CPoint point);void OnMouseMove(UINT nFlags, CPoint point, CDC* pDC);void OnDraw(CDC* pDC);  // 重写以绘制该视图
};

CLine.cpp

#include "pch.h"
#include "CLine.h"
void CLine::OnLButtonDown(UINT nFlags, CPoint point)
{m_ps = point;
}void CLine::OnLButtonUp(UINT nFlags, CPoint point)
{	if (ST_DRAW == m_stat){	m_pe = point;m_stat = ST_NORMAL;}
}
void CLine::OnMouseMove(UINT nFlags, CPoint point, CDC* pDC){if (ST_DRAW ==m_stat &&nFlags &MK_LBUTTON)	{if (m_last.x!=MAXWORD)	{pDC->MoveTo(m_ps);pDC->LineTo(m_last);}pDC->MoveTo(m_ps);pDC->LineTo(point);m_last = point;}
}
void CLine::OnDraw(CDC* pDC){pDC->MoveTo(m_ps);pDC->LineTo(m_pe);
}

CRRect .h

#pragma once
#include "pch.h"
class CRRect : public SLayer
{CRect m_rect;void OnLButtonDown(UINT nFlags, CPoint point); //形成多态void OnLButtonUp(UINT nFlags, CPoint point);void OnMouseMove(UINT nFlags, CPoint point, CDC* pDC);void OnDraw(CDC* pDC);  // 重写以绘制该视图
};

CRRect .cpp

#include "pch.h"
#include "CRRect.h"
void CRRect::OnLButtonDown(UINT nFlags, CPoint point)
{m_rect.TopLeft() = point;
}void CRRect::OnLButtonUp(UINT nFlags, CPoint point)
{m_rect.BottomRight() = point;m_rect.NormalizeRect();
}
void CRRect::OnMouseMove(UINT nFlags, CPoint point, CDC* pDC)
{
}void CRRect::OnDraw(CDC* pDC)
{int nWidth = m_rect.Width();int nHeight = m_rect.Height();pDC->RoundRect(m_rect, {nWidth/5,nHeight/5});
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/72497.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【云原生,k8s】Helm应用包管理器介绍

目录 一、为什么需要Helm&#xff1f; &#xff08;一&#xff09;Helm介绍 &#xff08;二&#xff09;Helm有3个重要概念&#xff1a; &#xff08;三&#xff09;Helm特点 二、Helm V3变化 &#xff08;一&#xff09;架构变化 &#xff08;二&#xff09;自动创建名…

算法笔记:A*算法

A*算法是一种很常用的路径查找和图形遍历算法。它有较好的性能和准确度 1 中心思路 A*算法通过下面这个函数来计算每个节点n的优先级 f(n)g(n)h(n) f(n)是节点n的综合优先级。当选择下一个要遍历的节点时&#xff0c;总会选取综合优先级最高&#xff08;f(n)值最小&#xff0…

酷开系统 | 酷开科技大数据,更好的与目标消费人群建立联系

众所周知&#xff0c;OTT的一大优势在于强曝光&#xff0c;能够给消费者带来强烈的视觉冲击&#xff0c;强化品牌认知。但是&#xff0c;要想达到提升品牌认知&#xff0c;首先要保证OTT的流量规模&#xff0c;实现对目标人群的有效覆盖。得年轻消费者得“天下”&#xff0c;年…

MySQL 中 不等于 会过滤掉 Null 的问题

null值与任意值比较时都为fasle not in 、"!"、"not like"条件过滤都会过滤掉null值的数据 SELECT * from temp; SELECT * from temp where score not in (70); 返回null解决方法: SELECT * from temp where score not in (70) or score is null;SELECT…

基于CentOS 7 部署社区版Haproxy

HAProxy是法国开发者 威利塔罗(Willy Tarreau) 在2000年使用C语言开发的一个开源软件&#xff0c;是一款具 备高并发(一万以上)、高性能的TCP和HTTP负载均衡器&#xff0c;支持基于cookie的持久性&#xff0c;自动故障切换&#xff0c;支 持正则表达式及web状态统计。 目录 1…

如何使用CSS实现一个响应式视频播放器?

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 使用CSS实现响应式视频播放器⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前端之旅 欢迎来到前端入门之旅&#xff01;这个专栏是为那些对Web开发感兴趣…

【腾讯云 Cloud Studio 实战训练营】在线 IDE 编写 canvas 转换黑白风格头像

关于 Cloud Studio Cloud Studio 是基于浏览器的集成式开发环境(IDE)&#xff0c;为开发者提供了一个永不间断的云端工作站。用户在使用Cloud Studio 时无需安装&#xff0c;随时随地打开浏览器就能在线编程。 Cloud Studio 作为在线IDE&#xff0c;包含代码高亮、自动补全、Gi…

P12-Retentive NetWork-RetNet挑战Transformer

论文地址:https://arxiv.org/abs/2307.08621 目录 Abstract 一.Introduction 二.Retentive Networks 2.1Retention 2.2Gated Multi-Scale Retention 2.3Overall Architecture of Retention Networks 2.4Relation to and Differences from Previous Methods 三.Experime…

Stable Diffusion Webui源码剖析

1、关键python依赖 &#xff08;1&#xff09;xformers&#xff1a;优化加速方案。它可以对模型进行适当的优化来加速图片生成并降低显存占用。缺点是输出图像不稳定&#xff0c;有可能比不开Xformers略差。 &#xff08;2&#xff09;GFPGAN&#xff1a;它是腾讯开源的人脸修…

Java中的异常

认识异常 异常就是程序出现的问题&#xff1b; Integer.valueOf("aaaa"); 异常体系 因为写代码时经常会出现问题&#xff0c;Java的设计者们早就为我们写好了很多个异常类&#xff0c;来描述不同场景下的问题。而有些类是有共性的所以就有了异常的继承体系 Error&…

[MySQL] — 数据类型和表的约束

目录 数据类型 数据类型分类 数值类型 tinyint类型 bit类型 小数类型 float decimal 字符串类型 char varchar char和varchar的区别 日期和时间类型 enum 和 set 表的约束 空属性 默认值 列描述 zeorfill 主键 创建表时在字段上指定主键 删除主键&#xff1a; 追…

Kubernetes 部署DolphinScheduler 创建租户失败

创建租户 报错创建租户失败。后台日志如下 源代码跟踪 org.apache.dolphinscheduler.api.service.impl.TenantServiceImpl / if hdfs startup if (PropertyUtils.getResUploadStartupState()) {createTenantDirIfNotExists(tenantCode); }需要将 resource.storage.type 置为…