Application and practice of software engineering in Shandong University -- WeaselUI

Posted by parena on Sun, 21 Nov 2021 02:36:17 +0100

2021SC@SDUSC
After analyzing the header files referenced by WeaselPanel.h in the first two articles, we can finally take a look at the specific contents of WeaselPanel.h and WeaselPanel.cpp.

typedef CWinTraits<WS_POPUP|WS_CLIPSIBLINGS|WS_DISABLED, WS_EX_TOOLWINDOW|WS_EX_TOPMOST> CWeaselPanelTraits;

class WeaselPanel : 
	public CWindowImpl<WeaselPanel, CWindow, CWeaselPanelTraits>,
	CDoubleBufferImpl<WeaselPanel>
{
public:
	BEGIN_MSG_MAP(WeaselPanel)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
		CHAIN_MSG_MAP(CDoubleBufferImpl<WeaselPanel>)
	END_MSG_MAP()

	LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	void CloseDialog(int nVal);

	WeaselPanel(weasel::UI &ui);
	~WeaselPanel();

	void MoveTo(RECT const& rc);
	void Refresh();

	void DoPaint(CDCHandle dc);

private:
	void _CreateLayout();
	void _ResizeWindow();
	void _RepositionWindow();
	bool _DrawPreedit(weasel::Text const& text, CDCHandle dc, CRect const& rc);
	bool _DrawCandidates(CDCHandle dc);
	void _HighlightText(CDCHandle dc, CRect rc, COLORREF color);
	void _TextOut(CDCHandle dc, int x, int y, CRect const& rc, LPCWSTR psz, int cch);

	weasel::Layout *m_layout;
	weasel::Context &m_ctx;
	weasel::Status &m_status;
	weasel::UIStyle &m_style;

	CRect m_inputPos;
	CIcon m_iconDisabled;
	CIcon m_iconEnabled;
	CIcon m_iconAlpha;
};

public CWindowImpl<WeaselPanel, CWindow, CWeaselPanelTraits>

CWindowImpl template class in ATL

  • First, take a look at the declaration of this template class
template <class T, class TBase = CWindow, class TWinTraits = CControlWinTraits> class CWindowImpl;

As you can see from the declaration, this class has two default parameters. Among them, TBase is the default window base class. This CWindow simply encapsulates HWND and encapsulates the interface with HWND as the first parameter in almost all Use32 APIs. CWindow provides a common member m_hWnd can directly handle HWND. It also provides an operator HWND(), which can directly be CWindow as the function parameter of the HWND object. CWindow is different from CWnd in MFC. CWindow is easy to create. It does not provide object relationships like HWND to CWnd in MFC. When a CWindow object is out of scope, it is destroyed, but its associated actual window is not destroyed. Therefore, you do not need to detach the temporary CWindow object you created. Twintrains is the default window feature.

  • Defined code
template <class T, class TBase /* = CWindow */, class TWinTraits /* = CControlWinTraits */>
class ATL_NO_VTABLE CWindowImpl :
    public CWindowImplBaseT< TBase, TWinTraits >

CWindowImplBaseT template class is inherited here

template <class TBase = CWindow, class TWinTraits = CControlWinTraits>
class ATL_NO_VTABLE CWindowImplBaseT : 
    public CWindowImplRoot< TBase >

Finally, it inherits the CWindowImplRoot template class

template <class TBase /* = CWindow */>
class ATL_NO_VTABLE CWindowImplRoot : 
    public TBase, 
    public CMessageMap

CWindowImplRoot class inherits a TBase and CMessageMap. TBase is the CWindow class by default. Of course, the TBase base class can also be encapsulated by itself, but CWindow is generally enough.

This class allows you to create a new window or make an existing window a subclass. The CWindowImpl windower uses message mapping to direct detailed handles to the response.
T: Your class inherits from CWindowImpl
TBase: the base class of your class. The default class is CWindow
Twintrain: a feature class that defines your window style. The default is CControlWinTraits

public CDoubleBufferImpl<WeaselPanel>

WTL double buffer drawing
There are two mix in classes in WTL: CDoubleBufferImpl and CDoubleBufferWindowImpl, which are used to create double buffered drawing windows. It is very easy to use this function provided by Vista in WTL. The latest WTL library provides two classes, cbbufferedpaintimpl and cbbufferedpaintwindowimpl. The usage of these two classes is almost the same as that of the two self-contained double buffer classes of WTL mentioned earlier. The only difference is that the parameters of the overloaded DoPaint() function are slightly different.

Topics: Python C++ Visual Studio