文章目錄
- 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});
}