⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rolloutctrl.h

📁 一款最完整的工业组态软源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
#ifndef __ROLLOUTCTRL__H
#define __ROLLOUTCTRL__H

#pragma once

/////////////////////////////////////////////////////
//	Rollout control and container implementation
//	Based on the CodeProject Bjoern Graf article
//
//	Written by Alexey Shirshov (qqqaqa@mail.ru)
//	Copyright (c) 2003 Alexey Shirshov
//
//


#if (WINVER < 0x0500) && ((_WIN32_WINNT < 0x0400) || !defined(_WIN32_WINNT))
#include <ZMOUSE.H>
#endif


#ifndef __ATLCTRLS_H__
#error rolloutctrl.h requires atlctrls.h to be included first
#endif

#ifndef __ATLSCRL_H__
#error rolloutctrl.h requires atlscrl.h to be included first
#endif

#ifndef __ATLMISC_H__
#error rolloutctrl.h requires atlmisc.h to be included first
#endif

#include "chf.h"

namespace AWTL
{

/////////////////////////////////////////////////////
// CRolloutCtrlButton
//
//

class CRolloutCtrlButton : public CWindowImpl<CRolloutCtrlButton, CButton>
{
	typedef CWindowImpl<CRolloutCtrlButton, CButton> _baseClass;
	typedef CRolloutCtrlButton _thisClass;

private:
	// Internal states
	bool m_fChecked;
	bool m_fPressed;
	bool m_fEnabled;
	COLORREF clrTextColor;
	COLORREF clrTextColorShadow;

public:
	DECLARE_WND_SUPERCLASS(_T("AWTL_RolloutButton"), GetWndClassName())

	// Construction
	CRolloutCtrlButton() : m_fChecked(true), m_fPressed(false),
		m_fEnabled(false)
	{}

/*	// overridden to provide proper initialization
	HWND Create(HWND hWndParent, LPCTSTR szWindowName,
			DWORD dwStyle = 0, DWORD dwExStyle = 0,
			UINT nID = 0U, LPVOID lpCreateParam = NULL)
	{
		CWindowImpl<T, TBase, TWinTraits>::Create(hWndParent, rcDefault, szWindowName,
			TWinTraits::GetWndStyle(dwStyle), TWinTraits::GetWndExStyle(dwExStyle),
			nID, lpCreateParam);
		if (IsWindow()){
			Init();
		}
		return m_hWnd;
	}
*/
	BOOL SubclassWindow(HWND hWnd)
	{
		BOOL bRet = _baseClass::SubclassWindow(hWnd);
		if (bRet) Init();
		return bRet;
	}

// Attributes
	bool IsChecked() const
	{
		return m_fChecked;
	}

	void SetChecked(bool fChecked)
	{
		m_fChecked = fChecked;
	}

private:
	// Initialization
	void Init()
	{
		// We need this style to prevent Windows from painting the button
		ModifyStyle(0, WS_GROUP|WS_TABSTOP|BS_OWNERDRAW|WS_CLIPCHILDREN|WS_CLIPSIBLINGS);
		m_fEnabled = IsWindowEnabled() ? true : false;
		clrTextColor = ::GetSysColor(COLOR_3DHILIGHT);
		clrTextColorShadow = ::GetSysColor(COLOR_3DSHADOW);
	}

// Overrideables
	void DoPaint(CDCHandle dc)
	{
		CRect rect;
		CRect rcClient;
		GetClientRect(&rcClient);
		rect = rcClient;

		const int title_len = GetWindowTextLength();
		PTSTR szTitle = (PTSTR)_alloca(title_len+1);
		GetWindowText(szTitle, title_len+1);

		const HFONT hOldFont = dc.SelectFont(GetFont());

		//CSize ptTextExtent;
		//dc.GetTextExtent(szTitle, title_len, &ptTextExtent);

		dc.DrawFrameControl(&rcClient, DFC_BUTTON,DFCS_BUTTONPUSH|DFCS_FLAT|
			(m_fPressed?DFCS_PUSHED:0)|(m_fChecked?DFCS_CHECKED:0)|
			(m_fEnabled?DFCS_INACTIVE:0));
		
		rect.top += 2 * ::GetSystemMetrics(SM_CYBORDER);
		
		if (m_fPressed)
			rect.OffsetRect(1, 1);

		const int oldBkMode = dc.SetBkMode(TRANSPARENT);

		if (m_fEnabled){
			dc.DrawText(szTitle, title_len, &rect, DT_CENTER|DT_TOP|DT_SINGLELINE);
		}
		else{
			COLORREF clrOldText = dc.SetTextColor(clrTextColor);
			rect.OffsetRect(1, 1);
			dc.DrawText(szTitle, title_len, &rect, DT_CENTER|DT_TOP|DT_SINGLELINE);
			dc.SetTextColor(clrTextColorShadow);
			rect.OffsetRect(-1, -1);
			dc.DrawText(szTitle, title_len, &rect, DT_CENTER|DT_TOP|DT_SINGLELINE);
			dc.SetTextColor(clrOldText);
		}

		rect.top -= 2 * ::GetSystemMetrics(SM_CYBORDER);
		// draw expand/collapse button
/*		if (m_fChecked)
			lstrcpy(szTitle, _T("-"));
		else
			lstrcpy(szTitle, _T("+"));
		
		rect.left += 2 * ::GetSystemMetrics(SM_CXEDGE);
		
		if (m_fEnabled){
			dc.DrawText(szTitle, 1, &rect, DT_LEFT|DT_TOP|DT_SINGLELINE);
		}
		else{
			COLORREF clrOldText = dc.SetTextColor(clrTextColor);
			rect.OffsetRect(1, 1);
			dc.DrawText(szTitle, 1, &rect, DT_LEFT|DT_TOP|DT_SINGLELINE);
			dc.SetTextColor(clrTextColorShadow);
			rect.OffsetRect(-1, -1);
			dc.DrawText(szTitle, 1, &rect, DT_LEFT|DT_TOP|DT_SINGLELINE);
			dc.SetTextColor(clrOldText);
		}
*/
		rect.left += 2 * ::GetSystemMetrics(SM_CXEDGE);
		rect.top = rect.Height()/2-4;
		rect.right = rect.left + 9;
		rect.bottom = rect.top + 9;
		
		AWTL::DrawFrameControl(dc,&rect,DFC_CATEGORY,m_fChecked?DFCS_MINUS:DFCS_PLUS);
		
		dc.SelectFont(hOldFont);

		// draw focus rect
		if (::GetFocus() == m_hWnd){
			rect = rcClient;
			rect.DeflateRect(::GetSystemMetrics(SM_CXEDGE), ::GetSystemMetrics(SM_CYEDGE));
			dc.DrawFocusRect(&rect);
		}
	}
	
protected:
// Message map and handlers
	BEGIN_MSG_MAP(_thisClass)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackground)
		MESSAGE_HANDLER(WM_PAINT, OnPaint)
		MESSAGE_HANDLER(WM_PRINTCLIENT, OnPaint)
		MESSAGE_HANDLER(WM_ENABLE, OnEnable)
		MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
		MESSAGE_HANDLER(WM_LBUTTONDBLCLK, OnLButtonDblClk)
		MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
//		MESSAGE_HANDLER(WM_CAPTURECHANGED, OnCaptureChanged)
		MESSAGE_HANDLER(WM_SETFOCUS, OnFocus)
		MESSAGE_HANDLER(WM_KILLFOCUS, OnFocus)
		MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)
		MESSAGE_HANDLER(WM_KEYUP, OnKeyUp)
		MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
//		MESSAGE_HANDLER(WM_MOVE, OnMove)
	END_MSG_MAP()

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
	{
		Init();
		return 1;
	}

	LRESULT OnEraseBackground(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		return 1;	// no background needed
	}

	LRESULT OnPaint(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		if(wParam != NULL){
			//Handle WM_PRINTCLIENT
			DoPaint((HDC)wParam);
		}
		else{
			CPaintDC dc(m_hWnd);
			DoPaint(dc.m_hDC);
		}
		return 0;
	}

	LRESULT OnEnable(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
	{
		m_fEnabled = wParam?true:false;
		return 0;
	}

	LRESULT OnLButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
//		if(::GetCapture() == m_hWnd)
		{
			SetFocus();
			SetCapture();
			m_fPressed = true;
			Invalidate();
//			UpdateWindow();
		}
		return 0;
	}

	LRESULT OnLButtonDblClk(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
/*		if(::GetCapture() != m_hWnd)
			SetCapture();*/
		if(!m_fPressed){
			m_fPressed = true;
			Invalidate();
//			UpdateWindow();
		}
		return 0;
	}

	LRESULT OnLButtonUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		if(m_fPressed){
			m_fChecked = !m_fChecked;
			m_fPressed = false;
			Invalidate();
			::SendMessage(GetParent(), WM_COMMAND,
				MAKEWPARAM(GetDlgCtrlID(), BN_CLICKED), (LPARAM)m_hWnd);
		}
		::ReleaseCapture();
		return 0;
	}

	LRESULT OnCaptureChanged(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
	{
		if(m_fPressed){
			m_fPressed = false;
			Invalidate();
			UpdateWindow();
		}
		bHandled = FALSE;
		return 1;
	}

	LRESULT OnFocus(UINT uMsg, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
	{
		Invalidate();
		bHandled = FALSE;
		return 0;
	}

	LRESULT OnKeyDown(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		if(wParam == VK_SPACE && !m_fPressed){
			m_fPressed = true;
			Invalidate();
		}
		return 0;
	}

	LRESULT OnKeyUp(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		if(wParam == VK_SPACE && m_fPressed)
		{
			m_fPressed = false;
			m_fChecked = !m_fChecked;
			::SendMessage(GetParent(), WM_COMMAND,
				MAKEWPARAM(GetDlgCtrlID(), BN_CLICKED), (LPARAM)m_hWnd);
			Invalidate();
		}
		return 0;
	}

	LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)
	{
		if(::GetCapture() == m_hWnd)
		{
			CPoint ptCursor(lParam);
			CRect rc;
			GetClientRect(&rc);
			const bool uPressed = rc.PtInRect(ptCursor) == TRUE;
			if(m_fPressed != uPressed){
				m_fPressed = uPressed;
				Invalidate();
			}
		}
		bHandled = FALSE;
		return 1;
	}

};	//end of CRolloutCtrlButton


/////////////////////////////////////////////////////
// Rollout - one pane holder
//
//

struct Rollout
{
	~Rollout()
	{
		if (rloButton.IsWindow())
			rloButton.DestroyWindow();
		else
			rloButton.m_hWnd = NULL;
	}

	HWND hWnd;
	//DWORD dwDefButton;
	CRolloutCtrlButton rloButton;
	
	Rollout(HWND Wnd,DWORD /*dwBtn*/)
	{
		hWnd = Wnd;
		//dwDefButton = dwBtn;
	}

	bool CreateButton(HWND hParent,CRect rc,HFONT hFont,PCTSTR szTitle)
	{		
		rc.DeflateRect(10,0);
		rc.top = 0;
		rc.bottom = 18;
		
		rloButton.Create(hParent,rc,szTitle,
			WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_PUSHBUTTON|BS_NOTIFY);
		
		ATLASSERT(rloButton.IsWindow());

		rloButton.SetFont(hFont);

		return rloButton.IsWindow() == TRUE;
	}

/*	void Move(int dy)
	{
		CRect rc;
		GetClientRect(hWnd,&rc);
		MapWindowPoints(hWnd,GetParent(hWnd),(LPPOINT)&rc,2);
		SetWindowPos(hWnd,NULL,rc.left,rc.top+dy,0,0,
			SWP_NOZORDER|SWP_NOSIZE);
		//::SendMessage(hWnd,WM_MOVE,0,MAKELPARAM(rc.left,rc.top+dy));
	}
*/
};	//end of Rollout

/////////////////////////////////////////////////////
// CRolloutHolder - panes manager
//
//

class CRolloutHolder
{
private:
	int xMax;

public:

	CRolloutHolder()
	{
		xMax = 0;
	}
	
	~CRolloutHolder()
	{
		for(int i = 0;i < m_RolloutCtrls.GetSize();i++)
		{
			delete m_RolloutCtrls[i];
		}
	}

protected:
	CSimpleArray<Rollout*> m_RolloutCtrls;

	//Return the maximum width among all controls
	int GetMaxWidth() const
	{
		CRect r;
		::GetWindowRect(m_RolloutCtrls[0]->hWnd,&r);
		int maxim = r.Width();
		for(int i = 1;i < m_RolloutCtrls.GetSize();i++)
		{
			::GetWindowRect(m_RolloutCtrls[i]->hWnd,&r);
			if (maxim < r.Width())
				maxim = r.Width();
		}

		return maxim;
	}

public:
	// Return -1 if not found
	int FindByHandle(HWND hWndRollout) const
	{
		for(int i = 0;i < m_RolloutCtrls.GetSize();i++)
		{
			if (hWndRollout == m_RolloutCtrls[i]->hWnd)
				return i;
		}
		return -1;
	}
	
	// Return -1 if not found
	int FindByBtnHandle(HWND hBtnWnd) const
	{
		for(int i = 0;i < m_RolloutCtrls.GetSize();i++)
		{
			if (hBtnWnd == m_RolloutCtrls[i]->rloButton)
				return i;
		}
		return -1;
	}

	//Return -1, if error occur or number of controls in array
	int AddRollout(HWND hParent,HWND hWndRollout,PCTSTR szTitle,
		const CRect& rc,DWORD dwDefBtn = 0)
	{
		CWindow w(hWndRollout);

		_DASSERT(w.IsWindow()){
			return -1;
		}
		
		const DWORD style = w.GetWindowLongPtr(GWL_STYLE);
		const bool required = (style & WS_CHILD) == WS_CHILD;
		const bool nrequired = (style & DS_CONTROL) == DS_CONTROL &&
			(style & DS_MODALFRAME) == DS_MODALFRAME;

		ATLASSERT(required);
		ATLASSERT(!nrequired);

		_DASSERT(::IsWindow(hParent)){
			return -1;
		}

		Rollout* r = new Rollout(hWndRollout,dwDefBtn);
		const bool res = m_RolloutCtrls.Add(r) == TRUE;
		
		if (res){
			const int iCurIndx = m_RolloutCtrls.GetSize()-1;

			// Create button for rollout
			// see CreateButton function in Rollout struct
			if (m_RolloutCtrls[iCurIndx]->CreateButton(hParent,rc,
				AtlGetDefaultGuiFont(),szTitle)){
				xMax = GetMaxWidth();
			}
			return iCurIndx;
		}

		return -1;
	}

	//Return true, if control was deleted
	bool RemoveRollout(HWND hWndRollout)
	{
		_DASSERT(::IsWindow(hWndRollout)){
			return false;
		}

		const int idx = FindByHandle(hWndRollout);
		if (idx == -1)
			return false;

		return RemoveRollout(idx);
	}

	//Remove rollout by index
	bool RemoveRollout(int nIndex)
	{
		_DASSERT(nIndex >= 0 && nIndex < m_RolloutCtrls.GetSize()){
			return false;
		}

		HWND hWnd = m_RolloutCtrls[nIndex]->hWnd;

		_DASSERT(::IsWindow(hWnd)){
			return false;
		}

		delete m_RolloutCtrls[nIndex];
		const bool res = m_RolloutCtrls.RemoveAt(nIndex) == TRUE;
		if (res){
			xMax = GetMaxWidth();
		}

		return res;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -