www.pudn.com > LayeredBitmapCtrl_demo.zip > LayeredBitmapCtrl.h
//******************************************************************* // FILE: LayeredBitmapCtrl.h - header file // // AUTHOR: Brian H. Conte - email(GhostsOfRage@yahoo.com) // // COMPONENTS: CLayerInfo - Stores information about a layer. // CLayeredBitmapCtrl - CStatic derived class that provides // functionality for bitmap layers, including bitmaps // with a specified transparent color. // // COPYRIGHT: ©2003-2004, All Rights Reserved // // This code may be used in compiled form in any way you // desire. This file may be redistributed unmodified by // any means PROVIDING it is not sold for profit without // the authors written consent, and providing that this // notice and the authors name and all copyright notices // remains intact. // // This file is provided "as is" with no expressed or // implied warranty. The author accepts no liability // for any damage/loss of business that this product may // cause. // // COMMENTS: This code allows you to add/remove/show/hide bitmap // layers on a static control. The first layer is not // usually set to transparent because it is used as the // background. All of the other layers should have the // transparent flag set, otherwise the first layer will // not be visible. The exception to this is if you have // more than one background layer and you only set the // show flag for one or the other. This code was written // to support 24-bit bitmap images only. The standard // template library (STL) is used for the vector support. // // HISTORY: // 09/25/2003 - Initial release. // 10/02/2003 - Fixed a couple of resource leaks. // 10/10/2003 - Added ability to move layers around with // the mouse without changing the order of // the layers. // 10/10/2003 - Modified the drawing functions so that // the selected layer can be moved, while // the other layers above remain in the // same position. // 11/16/2003 - Added a Create function to allow the control // to be dynamically create on a CView, etc. // 03/14/2004 - Added a focus rectangle for the currently // selected layer. Thanks to X.Q.Wang for // the suggestion. // 04/27/2004 - Added methods to set/get the description, // transparent color, and location of the // layer after it has already been added to // the control. // 06/09/2004 - Added code so that only the portion of // the layers above the tracked layer are redrawn, // to give the appearance that the tracked // layer is moving underneath all of the layers // above. // 06/10/2004 - Added the flag m_bShowOnTopWhileTracking which // speeds up the drawing process because none of // layers above the tracked layer need to be // redrawn. The m_bShowOnTopWhileTracking // flag is defaulted to true, but the flag // can be changed at anytime. // 06/17/2004 - Changed to version 2.0 since the drawing // code had been modified so much as well // the enhancements. // 12/07/2004 - Added code to change the background color // if the control. Another addition is the // ability to use the system color for the // control's background that supports system // color changes. Thanks to Michel Wassink // for the suggestions! // 12/08/2004 - Changed to version 2.01 to reflect the // latest changes. //******************************************************************* #if !defined(AFX_LAYEREDBITMAPCTRL_H__C25D3F03_0010_410F_9EC1_44A2726B2BD5__INCLUDED_) #define AFX_LAYEREDBITMAPCTRL_H__C25D3F03_0010_410F_9EC1_44A2726B2BD5__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #includeusing namespace std; //******************************************************************* // CLASS: - CLayerInfo // COMMENTS: - This class contains all of the necessary elements // for a layer object. The comparison functions are // only provided for sorting the vector in the // CLayeredBitmapCtrl control. That is why the only // element that is compared is the layer index. //******************************************************************* class CLayerInfo { // Construction/Destruction public: // Constructors CLayerInfo(); CLayerInfo( const CLayerInfo &src ); // Copy constructor // Destructor virtual ~CLayerInfo(); // Implementation public: // Copies the source layer object into this layer object. CLayerInfo &Copy( const CLayerInfo &src ); // Assigns the contents of one layer object to another. CLayerInfo &operator=( const CLayerInfo &src ); // Compares the layer index of the source layer object with the // layer index of this layer object. These comparison functions // are used by the vector for sorting purposes. bool operator==( const CLayerInfo &layerInfo ) const; bool operator<( const CLayerInfo &layerInfo ) const; bool operator>( const CLayerInfo &layerInfo ) const; // Here are some static bitmap utility functions that can be used independently of the layers. // Copies the source bitmap into the destination bitmap. static bool CopyBitmap( const CBitmap &bmpSrc, CBitmap &bmpDest ); // Copies a section from an existing source bitmap into a new destination bitmap. static bool CopyBitmapSection( CBitmap &bmpSrc, CBitmap &bmpDest, CRect &rectSection, CDC *pDC = NULL ); // Allocates and initializes a BITMAPINFO structure. static BITMAPINFO *PrepareRGBBitmapInfo( int nWidth, int nHeight ); // Gets the color from a specific location within a bitmap. static COLORREF GetColorFromBitmap( const CPoint &ptLocation, CBitmap &bitmap ); // Attributes public: // Unique layer identifier, so that the layer can be found within the vector. int m_nLayerID; // Indicates which level this layer is at. // The vector is sorted by this index. (0 = bottom layer). int m_nLayerIndex; // This is the description that will be displayed in the tool tip. CString m_strLayerDesc; // Indicates whether or not this layer will be displayed or hidden. bool m_bVisible; // Indicates that this layer contains a region which is used to determine if // the mouse is within the layer. bool m_bUseRgn; // Indicates that this layer can be moved with the mouse. bool m_bTrackingEnabled; // Indicates that this layer is currently being tracked. bool m_bTracking; // Indicates that this layer will be displayed above all visible layers while being tracked. // This allows the tracked layer to be displayed faster instead of having to redraw each section // of the above layers that intersect with the tracked layer. bool m_bShowOnTopWhileTracking; // Indicates the point within the layer that the left mouse button was first pressed. CPoint m_ptTrackingStart; // Indicates that this layer contains transparent pixels. bool m_bTransparent; // Indicates the transparent color. COLORREF m_colTransparent; // Instead of setting the transparent color manually, you can specify // the location within the bitmap to get the transparent color. CPoint m_ptTransparentPixel; // Specifies where you want to put this layer on the CLayeredBitmapCtrl window. CPoint m_ptLocation; // Indicates whether or not this layer will display a focus rectangle while the // left mouse button is pressed. bool m_bFocusRectangleEnabled; // Indicates that the focus rectangle should be displayed. bool m_bShowFocusRectangle; // Indicates the color of the focus rectangle. COLORREF m_colFocusRectangle; // This is the bitmap for the current layer. CBitmap m_bmp; // This is the region for the current layer. (The region does not have to be created). CRgn m_rgn; }; //******************************************************************* // CLASS: - CLayeredBitmapCtrl // COMMENTS: - This is a CStatic derived class that provides the // ability to add/remove/show/hide bitmap layers on // a static control. The first layer is not usually // set to transparent because it is used as the // background. All of the other layers should have the // transparent flag set, otherwise the first layer will // not be visible. The exception to this is if you have // more than one background layer and you only set the // show flag for one or the other. //******************************************************************* class CLayeredBitmapCtrl : public CStatic { // Construction public: CLayeredBitmapCtrl(); // Attributes public: // This is the bitmap that is displayed on the screen within the control. CBitmap m_bmpCombined; protected: // This vector contains all of the layer objects even if they are not displayed. vector m_vecLayerInfo; // Indicates if tool tips are to be shown if the mouse is within a layer's region. bool m_bShowToolTips; // Indicates that the tool tip control has been initialized. bool m_bToolTipsInitialized; // Indicates that tool tips will be hidden while moving a layer with the mouse. bool m_bHideTrackingToolTip; // String that is displayed in the tool tip. CString m_strToolTip; // Tool tip control that is displayed if the mouse is within a layer's region. CToolTipCtrl m_toolTip; // Indicates if the parent of this control is a dialog or a view type window. // This way we can determine which system color to use (COLOR_BTNFACE = CDialog, // COLOR_WINDOW = CView). bool m_bIsParentDlg; // Indicates if the system color is used for the background color of the control. bool m_bUseSysColor; // Indicates the control's background color. COLORREF m_colCtrlBG; // Operations public: // Initializes the tool tip control. void InitToolTips(); // Enable or disable tool tips. void ShowToolTips( bool bShow ); // Indicates whether or not tool tips are enabled. bool ToolTipsEnabled() const; // Specifies whether or not to use the system color for the background of the control. void UseSysColor( bool bUseSysColor, bool bRedraw = false ); // Indicates if the control is using the system color. bool UsingSysColor() const; // Allows the user to change the background color of the control. void SetCtrlBGColor( COLORREF colCtrlBG, bool bRedraw = false ); // Gets the current background color of the control. COLORREF GetCtrlBGColor(); // Adds a layer object to the vector. bool AddLayer( CLayerInfo &layerInfo ); // Removes a layer object from the vector, based on the layer ID. bool RemoveLayer( int nLayerID ); // Allows the layer objects to be re-indexed based on their position in the vector. // This is necessary if layers have been removed. bool ReindexLayers(); // Swaps the indices of two layer objects. bool SwapLayers( int nFirstLayerID, int nSecondLayerID ); // Moves the specified layer to the top of all other layers. bool MakeTopLayer( int nLayerID ); // Indicate that this layer is to be displayed or hidden. bool SetLayerVisibility( int nLayerID, bool bVisible ); // Sets the layer transparency flag and color if necessary. // The transparent color is defaulted to black. bool SetLayerTransparency( int nLayerID, bool bTransparent = false, COLORREF colTransparent = RGB(0, 0, 0) ); // Sets the layer transparency flag and location of the transparent pixel // within the layer object's bitmap. bool SetLayerTransparency( int nLayerID, bool bTransparent, CPoint &ptTransparentPixel ); // Gets the transparent flag of the specified layer object. bool GetLayerTransparency( int nLayerID ); // Gets the transparent color of the specified layer object. COLORREF GetLayerTransparencyColor( int nLayerID ); // Sets the description for the specified layer object. bool SetLayerDescription( int nLayerID, CString &strLayerDesc ); // Gets the description of the specified layer object. bool GetLayerDescription( int nLayerID, CString &strLayerDesc ); // Sets the tracking enabled flag of a layer object. bool SetLayerTracking( int nLayerID, bool bTrackingEnabled ); // Gets the tracking enabled flag of a layer object. bool GetLayerTracking( int nLayerID ); // Sets the show on top while tracking flag of a layer object. bool SetLayerShowOnTopWhileTracking( int nLayerID, bool bShowOnTopWhileTracking ); // Gets the show on top while tracking flag of a layer object. bool GetLayerShowOnTopWhileTracking( int nLayerID ); // Sets the upper-left location point of a layer object. bool SetLayerLocation( int nLayerID, const CPoint &ptNewLocation ); // Gets the upper-left location point of a layer object. bool GetLayerLocation( int nLayerID, CPoint &ptLocation ); // Gets the width and height of a layer object. bool GetLayerSize( int nLayerID, CSize &size ); // Sets the focus rectangle enabled flag of a layer object. bool SetLayerEnableFocusRectangle( int nLayerID, bool bEnabled ); // Gets the focus rectangle enabled flag of a layer object. bool GetLayerEnableFocusRectangle( int nLayerID ); // Sets the focus rectangle color of a layer object. bool SetLayerFocusRectangleColor( int nLayerID, COLORREF colFocusRectangle ); // Sets the focus rectangle color of a layer object. COLORREF GetLayerFocusRectangleColor( int nLayerID ); // Gets the layer object's index. int GetLayerIndex( int nLayerID ); // Calculates the center offset for a layer, based on the control's // client rect. The second implementation can only be used if the // layer object already exists in the vector. bool CalculateCenterOffset( CLayerInfo *pLayerInfo ); bool CalculateCenterOffset( int nLayerID ); // Creates a region composed of the non-transparent pixels of the // layer's bitmap. The second implementation can only be used if the // layer object already exists in the vector. bool CreateNonTransparentRgn( CLayerInfo *pLayerInfo ); bool CreateNonTransparentRgn( int nLayerID ); // Determines if the bounding rectangle for two layers intersect. // The second implementation can only be used if the layer object // already exists in the vector. bool DoLayersIntersect( CLayerInfo *pLayerInfo1, CLayerInfo *pLayerInfo2, CRect *pRectIntersect = NULL ); bool DoLayersIntersect( int nLayerID1, int nLayerID2, CRect *pRectIntersect = NULL ); // Sorts the layer objects based on the layer index. // Note: There are no checks to make sure that there is only // one layer object per index. bool SortLayers(); // Combines all of the visible layer into one bitmap the will be // displayed within the OnPaint function. bool CombineLayers(); // Draws the specified layer onto a bitmap. bool DrawLayerOnBitmap( CBitmap *pBmpBackground, CLayerInfo *pLayerInfo ); // This function makes sure that the layers are sorted and combined // before drawing them onto the screen. bool ShowVisibleLayers(); // Creates a temporary focus rectangle layer at the specified location // with the specified color. CLayerInfo *CreateFocusLayer( CDC *pDC, int nX, int nY, int nWidth, int nHeight, COLORREF colFocus ); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CLayeredBitmapCtrl) public: virtual BOOL PreTranslateMessage(MSG* pMsg); //}}AFX_VIRTUAL protected: // Attempts to locate a layer object within the vector that contains // a matching layer ID. CLayerInfo *FindLayer( int nLayerID ); // Implementation public: // Destructor virtual ~CLayeredBitmapCtrl(); // Allows the control to be created dynamically. BOOL Create( DWORD dwExStyle, const RECT &rect, CWnd *pParentWnd, UINT nID ); // Generated message map functions protected: //{{AFX_MSG(CLayeredBitmapCtrl) afx_msg void OnPaint(); afx_msg void OnMouseMove(UINT nFlags, CPoint point); afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); afx_msg void OnSysColorChange(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_LAYEREDBITMAPCTRL_H__C25D3F03_0010_410F_9EC1_44A2726B2BD5__INCLUDED_)