第11章 GUI Page411~420 步骤五 支持其他图形

2023-12-22 18:52

本文主要是介绍第11章 GUI Page411~420 步骤五 支持其他图形,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

运行效果:

源代码

//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; //纯虚函数
};#endif // ITEM_I_HPP_INCLUDED
//item_line.hpp 修改现有的LineItem类,实现IItem接口#ifndef ITEM_LINE_HPP_INCLUDED
#define ITEM_LINE_HPP_INCLUDED#include "item_i.hpp"class LineItem : public IItem
{
public:LineItem(): _startPosition(0, 0), _endPosition(0, 0){}virtual void Draw(wxDC& dc) const;//    virtual void OnDrawStart(wxPoint const& point) //书上写法void OnDrawStart(wxPoint const& point) override //C++11{_startPosition = point;}virtual void OnDrawEnd(wxPoint const& point) //写不写virtual不影响{_endPosition = point;}private://不写Logic后缀,但仍然是存储逻辑坐标wxPoint _startPosition, _endPosition;
};#endif // ITEM_LINE_HPP_INCLUDED

//item_line.cpp
#include "item_line.hpp"void LineItem::Draw(wxDC& dc) const
{dc.DrawLine(_startPosition, _endPosition);//就这一行,画直线
}

/**************************************************************** Name:      wxMyPainterApp.h* Purpose:   Defines Application Class* Author:    yanzhenxi (3065598272@qq.com)* Created:   2023-12-21* Copyright: yanzhenxi ()* License:**************************************************************/#ifndef WXMYPAINTERAPP_H
#define WXMYPAINTERAPP_H#include <wx/app.h>class wxMyPainterApp : public wxApp
{public:virtual bool OnInit();
};#endif // WXMYPAINTERAPP_H

/**************************************************************** Name:      wxMyPainterApp.cpp* Purpose:   Code for Application Class* Author:    yanzhenxi (3065598272@qq.com)* Created:   2023-12-21* Copyright: yanzhenxi ()* License:**************************************************************/#include "wxMyPainterApp.h"//(*AppHeaders
#include "wxMyPainterMain.h"
#include <wx/image.h>
//*)IMPLEMENT_APP(wxMyPainterApp);bool wxMyPainterApp::OnInit()
{//(*AppInitializebool wxsOK = true;wxInitAllImageHandlers();if ( wxsOK ){wxMyPainterFrame* Frame = new wxMyPainterFrame(0);Frame->Show();SetTopWindow(Frame);}//*)return wxsOK;}

/**************************************************************** Name:      wxMyPainterMain.h* Purpose:   Defines Application Frame* Author:    yanzhenxi (3065598272@qq.com)* Created:   2023-12-21* Copyright: yanzhenxi ()* License:**************************************************************/#ifndef WXMYPAINTERMAIN_H
#define WXMYPAINTERMAIN_H
#include <list>
#include "item_i.hpp"//(*Headers(wxMyPainterFrame)
#include <wx/scrolwin.h>
#include <wx/sizer.h>
#include <wx/menu.h>
#include <wx/listbox.h>
#include <wx/frame.h>
#include <wx/statusbr.h>
//*)class wxMyPainterFrame: public wxFrame
{public:wxMyPainterFrame(wxWindow* parent,wxWindowID id = -1);virtual ~wxMyPainterFrame();private://(*Handlers(wxMyPainterFrame)void OnQuit(wxCommandEvent& event);void OnAbout(wxCommandEvent& event);void OnScrolledWindow1LeftDown(wxMouseEvent& event);void OnScrolledWindow1MouseMove(wxMouseEvent& event);void OnScrolledWindow1LeftUp(wxMouseEvent& event);void OnScrolledWindow1Paint(wxPaintEvent& event);//*)//(*Identifiers(wxMyPainterFrame)static const long ID_LISTBOX1;static const long ID_SCROLLEDWINDOW1;static const long idMenuQuit;static const long idMenuNone;static const long idMenuLine;static const long idMenuRectangle;static const long idMenuCircle;static const long idMenuText;static const long idMenuAbout;static const long ID_STATUSBAR1;//*)//(*Declarations(wxMyPainterFrame)wxMenuItem* MenuItemLine;wxMenuItem* MenuItemNone;wxScrolledWindow* ScrolledWindow1;wxMenu* Menu3;wxMenuItem* MenuItemText;wxStatusBar* StatusBar1;wxMenuItem* MenuItemRectangle;wxMenuItem* MenuItemCircle;wxListBox* ListBox1;//*)private:void RemoveAllItems();IItem* CreateNewItem();/*每当用户鼠标按下,就创建一个新的_newItem,等画完之后(鼠标抬起),就将它加入“_items”列表中。这也解释了为什么不需要“_drawing”这个标志,因为当“_newItem”不为空,就说明用户正在画新的线。OnPaint事件的思路配套调整为:每次都画出“_items”中的所有元素;然后如果“_newItem”不为空,则把用户正在拖拽的线也画出来。*/IItem* _newItem;std::list <IItem*> _items;DECLARE_EVENT_TABLE()
};#endif // WXMYPAINTERMAIN_H

/**************************************************************** Name:      wxMyPainterMain.cpp* Purpose:   Code for Application Frame* Author:    yanzhenxi (3065598272@qq.com)* Created:   2023-12-21* Copyright: yanzhenxi ()* License:**************************************************************/#include "wxMyPainterMain.h"
#include <wx/msgdlg.h>
#include <wx/dcclient.h>
#include "item_line.hpp"//(*InternalHeaders(wxMyPainterFrame)
#include <wx/settings.h>
#include <wx/intl.h>
#include <wx/string.h>
//*)//helper functions
enum wxbuildinfoformat {short_f, long_f };wxString wxbuildinfo(wxbuildinfoformat format)
{wxString wxbuild(wxVERSION_STRING);if (format == long_f ){
#if defined(__WXMSW__)wxbuild << _T("-Windows");
#elif defined(__UNIX__)wxbuild << _T("-Linux");
#endif#if wxUSE_UNICODEwxbuild << _T("-Unicode build");
#elsewxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE}return wxbuild;
}//(*IdInit(wxMyPainterFrame)
const long wxMyPainterFrame::ID_LISTBOX1 = wxNewId();
const long wxMyPainterFrame::ID_SCROLLEDWINDOW1 = wxNewId();
const long wxMyPainterFrame::idMenuQuit = wxNewId();
const long wxMyPainterFrame::idMenuNone = wxNewId();
const long wxMyPainterFrame::idMenuLine = wxNewId();
const long wxMyPainterFrame::idMenuRectangle = wxNewId();
const long wxMyPainterFrame::idMenuCircle = wxNewId();
const long wxMyPainterFrame::idMenuText = wxNewId();
const long wxMyPainterFrame::idMenuAbout = wxNewId();
const long wxMyPainterFrame::ID_STATUSBAR1 = wxNewId();
//*)BEGIN_EVENT_TABLE(wxMyPainterFrame,wxFrame)//(*EventTable(wxMyPainterFrame)//*)
END_EVENT_TABLE()wxMyPainterFrame::wxMyPainterFrame(wxWindow* parent,wxWindowID id): _newItem(nullptr)
{//(*Initialize(wxMyPainterFrame)wxMenuItem* MenuItemAbout;wxMenu* Menu1;wxMenuItem* MenuItemQuit;wxBoxSizer* BoxSizer1;wxMenuBar* MenuBar1;wxMenu* Menu2;Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));SetClientSize(wxSize(208,94));BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);ListBox1 = new wxListBox(this, ID_LISTBOX1, wxDefaultPosition, wxSize(168,68), 0, 0, 0, wxDefaultValidator, _T("ID_LISTBOX1"));BoxSizer1->Add(ListBox1, 0, wxALL|wxEXPAND, 5);ScrolledWindow1 = new wxScrolledWindow(this, ID_SCROLLEDWINDOW1, wxDefaultPosition, wxDefaultSize, wxVSCROLL|wxHSCROLL, _T("ID_SCROLLEDWINDOW1"));ScrolledWindow1->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));BoxSizer1->Add(ScrolledWindow1, 1, wxALL|wxEXPAND, 5);SetSizer(BoxSizer1);MenuBar1 = new wxMenuBar();Menu1 = new wxMenu();MenuItemQuit = new wxMenuItem(Menu1, idMenuQuit, _("退出\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);Menu1->Append(MenuItemQuit);MenuBar1->Append(Menu1, _("文件[&F]"));Menu3 = new wxMenu();MenuItemNone = new wxMenuItem(Menu3, idMenuNone, _("无"), wxEmptyString, wxITEM_RADIO);Menu3->Append(MenuItemNone);MenuItemLine = new wxMenuItem(Menu3, idMenuLine, _("直线"), wxEmptyString, wxITEM_RADIO);Menu3->Append(MenuItemLine);MenuItemRectangle = new wxMenuItem(Menu3, idMenuRectangle, _("矩形"), wxEmptyString, wxITEM_RADIO);Menu3->Append(MenuItemRectangle);MenuItemCircle = new wxMenuItem(Menu3, idMenuCircle, _("圆形"), wxEmptyString, wxITEM_RADIO);Menu3->Append(MenuItemCircle);MenuItemText = new wxMenuItem(Menu3, idMenuText, _("文字"), wxEmptyString, wxITEM_RADIO);Menu3->Append(MenuItemText);MenuBar1->Append(Menu3, _("组件[&I]"));Menu2 = new wxMenu();MenuItemAbout = new wxMenuItem(Menu2, idMenuAbout, _("关于\tF1"), _("Show info about this application"), wxITEM_NORMAL);Menu2->Append(MenuItemAbout);MenuBar1->Append(Menu2, _("帮助[&H]"));SetMenuBar(MenuBar1);StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));int __wxStatusBarWidths_1[1] = { -1 };int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);SetStatusBar(StatusBar1);SetSizer(BoxSizer1);Layout();ScrolledWindow1->Connect(wxEVT_PAINT,(wxObjectEventFunction)&wxMyPainterFrame::OnScrolledWindow1Paint,0,this);ScrolledWindow1->Connect(wxEVT_LEFT_DOWN,(wxObjectEventFunction)&wxMyPainterFrame::OnScrolledWindow1LeftDown,0,this);ScrolledWindow1->Connect(wxEVT_LEFT_UP,(wxObjectEventFunction)&wxMyPainterFrame::OnScrolledWindow1LeftUp,0,this);ScrolledWindow1->Connect(wxEVT_MOTION,(wxObjectEventFunction)&wxMyPainterFrame::OnScrolledWindow1MouseMove,0,this);Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&wxMyPainterFrame::OnQuit);Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&wxMyPainterFrame::OnAbout);//*)//设置滚动选项ScrolledWindow1->SetScrollRate(10, 10);ScrolledWindow1->SetVirtualSize(500, 480);
}wxMyPainterFrame::~wxMyPainterFrame()
{RemoveAllItems();//(*Destroy(wxMyPainterFrame)//*)
}void wxMyPainterFrame::OnQuit(wxCommandEvent& event)
{Close();
}void wxMyPainterFrame::OnAbout(wxCommandEvent& event)
{wxString msg = wxbuildinfo(long_f);wxMessageBox(msg, _("Welcome to..."));
}IItem* wxMyPainterFrame::CreateNewItem()
{if(this->MenuItemLine->IsChecked())//小心,别写成IsCheckable(){return new LineItem();}return nullptr;
}void wxMyPainterFrame::RemoveAllItems()
{for(std::list<IItem*>::iterator it = _items.begin(); it != _items.end(); ++it){delete *it; //释放每个_newItem占据的内存}_items.clear(); //释放_items中元素(指向_newItem的指针)占据的内存
}void wxMyPainterFrame::OnScrolledWindow1LeftDown(wxMouseEvent& event)
{if(_newItem) return; //正在划线,返回_newItem = this->CreateNewItem();if(!_newItem) return;_newItem->OnDrawStart(ScrolledWindow1->CalcUnscrolledPosition(event.GetPosition()));
}void wxMyPainterFrame::OnScrolledWindow1MouseMove(wxMouseEvent& event)
{if(!_newItem) //不在绘画状态{return;//直接退出}_newItem->OnDrawEnd(//物理坐标转为逻辑坐标ScrolledWindow1->CalcUnscrolledPosition(event.GetPosition()));ScrolledWindow1->Refresh();
}void wxMyPainterFrame::OnScrolledWindow1LeftUp(wxMouseEvent& event)
{if(!_newItem) //不在绘画状态?{return;//直接退出}_newItem->OnDrawEnd(//物理坐标转为逻辑坐标ScrolledWindow1->CalcUnscrolledPosition(event.GetPosition()));_items.push_back(_newItem); //记录到列表中_newItem = nullptr; //结束绘画状态ScrolledWindow1->Refresh();
}
//将_items中的所有线都画出来
void wxMyPainterFrame::OnScrolledWindow1Paint(wxPaintEvent& event)
{wxPaintDC dc(ScrolledWindow1);//DoPrepareDC会自行调校ScrolledWindow1->DoPrepareDC(dc);//先画出list中的每一项for(std::list <IItem*> :: const_iterator it = _items.begin(); it != _items.end(); ++it){IItem const* item = *it;item->Draw(dc);}//如果刚巧用户正在画新项,把它也显示出来//否则用户只能“盲画”了if(_newItem){_newItem->Draw(dc);}
}

关键代码:

抽象出“图形元素”接口类

改造LineItem

改造外部使用环境

实现CreateNewItem()和RemoveAllItems()

实现鼠标按下,移动,抬起函数

实现作画函数

这篇关于第11章 GUI Page411~420 步骤五 支持其他图形的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/525073

相关文章

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)

《k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)》本文记录在K8s上运行的MySQL/MariaDB备份方案,通过工具容器执行mysqldump,结合定时任务实... 目录前言一、获取需要备份的数据库的信息二、备份步骤1.准备工作(X86)1.准备工作(arm)2.手

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定