//item_rectangle.hpp
#ifndef ITEM_RECTANGLE_HPP_INCLUDED
#define ITEM_RECTANGLE_HPP_INCLUDED
#include "item_i.hpp"
class RectangleItem : public IItem
{
public:
RectangleItem()
: _startPosition(0, 0), _endPosition(0, 0)
{}
virtual void Draw(wxDC& dc) const;
void OnDrawStart(wxPoint const& point) override
{
_startPosition = point;
}
virtual void OnDrawEnd(wxPoint const& point)
{
_endPosition = point;
}
private:
wxPoint _startPosition, _endPosition;
};
#endif // ITEM_RECTANGLE_HPP_INCLUDED
//item_rectangle.cpp
#include "item_rectangle.hpp"
#include <cmath> //需要一些数学函数abs(绝对值),min(二者最小)
void RectangleItem::Draw(wxDC& dc) const
{
//得到左上角的坐标
int left_x = std::min(_startPosition.x, _endPosition.x);
int top_y = std::min(_startPosition.y, _endPosition.y);
//得到长和宽(绝对值)
int width = std::abs(_endPosition.x - _startPosition.x);
int height = std::abs(_endPosition.y - _startPosition.y);
dc.DrawRectangle(left_x, top_y, width, height);
}
CreateNewItem()函数中 155~158行