第11章 GUI Page426~427 步骤七 设置直线前景色

发布时间:2023年12月24日

运行效果:

关键代码:

接口类 IItem中新增29,30行两个设置前景色纯虚方法

//item_i.hpp 抽象“图形元素”接口定义
#ifndef ITEM_I_HPP_INCLUDED
#define ITEM_I_HPP_INCLUDED

#include <wx/gdicmn.h>
#include <wx/dc.h>

class IItem
{
public:
    //作为接口,记得要有虚析构
    virtual ~IItem()
    {

    }

    //使用DC画出自己
    //注意:“画”的方法不应该修改对象的数据
    virtual void Draw(wxDC& dc) const = 0; //纯虚函数

    //开始在某一点上绘图
    virtual void OnDrawStart(wxPoint const& point) = 0; //纯虚函数
    //结束在某一点
    virtual void OnDrawEnd(wxPoint const& point) = 0; //纯虚函数
    //设置于取得颜色的方法,纯虚方法,
//    virtual void SetFontColor(wxColor const& color) = 0;
//    virtual wxColor const& GetFrontColor() const = 0;
    //书上的方法,有错误,应该为:
    virtual void SetForegroundColor(wxColor const& color) = 0;
    virtual wxColor const& GetForegroundColor() const = 0;

};

#endif // ITEM_I_HPP_INCLUDED

新增头文件item_with_foreground_color.hpp

类ItemWithForegroundColor作为中间层,继承类IItem, 实现两个设置前景色的方法

//item_with_foreground_color.hpp
#ifndef ITEM_WITH_FOREGROUND_COLOR_HPP_INCLUDED
#define ITEM_WITH_FOREGROUND_COLOR_HPP_INCLUDED

#include "item_i.hpp"

class ItemWithForegroundColor : public IItem
{
public:
    ItemWithForegroundColor()
        : _foregroundColor(*wxBLACK)
    {

    }

    virtual void SetForegroundColor(wxColor const& color)
    {
        this->_foregroundColor = color;
    }

    wxColor const& GetForegroundColor() const
    {
        return _foregroundColor;
    }
private:
    wxColor _foregroundColor;
};

#endif // ITEM_WITH_FOREGROUND_COLOR_HPP_INCLUDED

LineItem类修改为:

修改LineItem类的Draw()方法

主菜单下新建 “设置菜单”,其下建立“前景色”菜单项

wxMyPainter类新增一个私有成员并初始化

为“前景色”菜单项绑定回调函数

当设置前景色时,回调函数会把选择的颜色值,传递给? _foregroundColor

在鼠标按下的函数中设置图形的前景色:

第195行的代码,会把设置的颜色,传递给ItemWithForegroundColor对象(中间层)的

_foregroundColor属性值,当创建_newItem对象时,可以用来设置画笔的颜色

文章来源:https://blog.csdn.net/yanzhenxi/article/details/135176934
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。