how to implement textbox

實作 textbox 使用 c++
應該要叫 TextBox 才對,啊嘶~
第三版:http://damody.googlecode.com/files/TextEditor_v3.7z

changelog
修正 s3748679 版友說的問題
加入 Ctrl-C Ctrl-V Ctrl-X Delete 多行單行的功能

第二版:http://damody.googlecode.com/files/TextEditor_v2.7z

縮圖:http://ppt.cc/X6TJ

change log:
修正 text width 算不準的問題
加入 mouse select 的功能

http://damody.googlecode.com/files/TextEditor.7z

縮圖: http://ppt.cc/cCEu

大概講一下步驟:

定一個抽象層 EditorConcept

定一個實作層 GdiEditor

定一下要用的資料結構

struct Size
	{
		Size();
		Size(int _w, int _h);
		int w, h;
	};

拿來存有大小的東西

struct Vec2
	{
		Vec2();
		Vec2(int _x, int _y);
		int x, y;
	};

存位置

struct Rect
	{
		Rect();
		Rect(int _x, int _y, int _w, int _h);
		union
		{
			struct
			{
				int x, y, w, h;
			};
			struct
			{
				int left, right, top, bottom;
			};
		};
	};

存矩形

struct Color
	{
		Color();
		Color(unsigned char _r, unsigned char _g, unsigned char _b);
		unsigned char r, g, b;
	};

存顏色

 

定義外框大小
白邊大小

	Size	m_Size;
	Rect	m_MarginSize;

定義一下顏色

	Color	m_MarginColor;
	Color	m_BackGroundColor;

定一下字的大小
要不要顯示行號
要不要顯示專注點
愛的閃耀間隔

Size	m_FontSize;
	bool	m_ShowLineNumber;
	bool	m_ShowFocus;
	double	m_BlinkTime;

定一下基本拿 size 的函數

Size	GetSize();
	void	SetSize(int w, int h);
	Rect	GetMarginSize();
	void	SetMarginSize(const Rect& rc);

定一下繪圖函數

Render();

畫背景函數

RenderBackGround();

用純虛擬函數 定義畫靠右/左字串

virtual void	DrawLeftString(int x, int y, const std::wstring& s) = 0;
virtual void	DrawRightString(int x, int y, const std::wstring& s) = 0;

用純虛擬函數 定義需要平台支援的函數

 

virtual void	DrawRect(const Rect& area, const Color& c) = 0;
	virtual void	SetFontSize(const EditorConcept::Size& s) = 0;
	virtual Size	GetFontSize() = 0;

 

再把一些業務邏輯加進來

	virtual void	SetFocusBlinkTime(double v);
	virtual double	GetFocusBlinkTime();
	virtual void	UpdateBlink();
	virtual bool	SafePositon(int x, int y);
	virtual void	TypeEnter();
	virtual void	TypeHome();
	virtual void	TypeEnd();
	virtual void	TypeDelete();
	virtual void	TypeBackSpace();
enum MOVE_DIRECTION
	{
		UP,
		DOWN,
		LEFT,
		RIGHT
	};
	virtual void	MoveFocus(MOVE_DIRECTION md);

實作一下

平台相依函數

	virtual void	DrawRect(const Rect& area, const Color& c);
	virtual void	DrawRightString(int x, int y, const std::wstring& s);
	virtual void	DrawLeftString(int x, int y, const std::wstring& s);
	virtual void	SetFontSize(const EditorConcept::Size& s);
	virtual Size	GetFontSize();
protected:
	virtual void	InterRenderStart();
	virtual void	InterRenderEnd();

後記:

我是一個虛擬函數一個平台相依函數成對實作的,
這樣可以快速驗證,
後來有用 double buffer 來解決 愛一直閃亮的問題,
就不多說了,因為這是對平台不夠了解的問題,
中間把平台相依函數做完後,
就剩把大量的業務邏輯實作在 EditorConcept 虛擬層的工作,

還有把這些邏輯跟WndProc 接在一起了。

arrow
arrow
    全站熱搜

    讓地獄深紅的天亮 發表在 痞客邦 留言(1) 人氣()