#if! defined (AFX_BITMAPDIALOG_H__A76F9E74_DF43_A76F9E74_DF43_11D4_AE27_4854E828E6FD__included _) # define AFX_BITMAPDIALOG_H__A76F9E74_DF43_11D4_AE27_4854E828E6FD__included_
#iF _MSC_VER> = 1000 # prgma overce # endif //_msc_ver> = 1000 // bitmapdialog.h: header file //
/// cbitmapdialog dialog
Class cbitmapdialog: public cdialog {// Constructionpublic: // Common Constructor Void Constructor (LPCTSTSTSTSZRESOURCENAME, UINT NIDRESOURCE, LPCTSTSTSTSZFILENAME, CBITMAP * PBITMAP);
CBITMAPDIALOG (LPCTSTSTSTSZZTEMPLATENAME, CWND * PPARENTWND = NULL, LPCTSTR LPSZRESOURENAME = NULL, UINT NIDRESOURCE = 0, LPCTSTR LPSZFILENAME = NULL, CBITMAP * PBITMAP = NULL);
CBitmapDialog (uint nidTemplate, CWnd * pParentWnd = null, lpctstr lpszresource = null, uint nidResource = 0, lpctstr lpszfilename = null, cbitmap * pbitmap = null);
CBitmapDialog (lpctstr lpszresource = null, uint nidResource = 0, lpctstr lpszfilename = null, cbitmap * pbitmap = null);
// destructor (just release the bitmap ~ cbitmapdialog () {releasebitmap ();
// Bitmap BOOL LoadBitmap (LPCTSTR lpszResourceName, LPCTSTR lpszFilename); // Load from resource or file BOOL LoadBitmap (UINT nIDResource); // Load from resource BOOL CopyBitmapFrom (CBitmap * pBitmap); // Copy of user defined void SetBitmap (CBitmap * pbitmap; // user defined void releasebitmap (); cbitmap * getBitmap () {return m_bmbitmap;}
// transparency void setTransparent (Bool BTransparent); Bool getTransparent () {return m_btransparent;} void settranscolor (colorref color); colorref gettranscolor () {return m_coltrans;}
// Static control transparency void SetStaticTransparent (BOOL bTransparent) {m_bStaticTransparent = bTransparent;} BOOL GetStaticTransparent () {return m_bStaticTransparent;} // Clicking anywhere moves void SetClickAnywhereMove (BOOL bMove) {m_bClickAnywhereMove = bMove;} BOOL GetClickAnywhereMove () {return m_bClickAnywhereMove }
// Dialog Data // {{AFX_DATA (CBITMAPDIALOG) // Note: The classwizard will add data members here //} AFX_DATA
// Overrides // ClassWizard Generated Virtual Function Overrides // {{AFX_VIRTUAL (CBITMAPDIALOG) protected: //}} AFX_VIRTUAL
// ImplementationProtace: void makewindowrgn ();
// Transparency Bool M_BTransparent; Bool M_BStatictTransparent; Hbrush M_brushhollow; ColorRef M_ColTrans;
// Clicking Anywhere Moves Bool M_BClickAnywhereMove;
// bitmap and its dc bool m_bbitmapcreated, m_bbitmapexists; cbitmap * m_bmbitmap;
// Generated message map functions // {{AFX_MSG (CBitmapDialog) afx_msg BOOL OnEraseBkgnd (CDC * pDC); afx_msg void OnLButtonDown (UINT nFlags, CPoint point); afx_msg HBRUSH OnCtlColor (CDC * pDC, CWnd * pWnd, UINT nCtlColor); //}} AFX_MSG DECLARE_MESSAGE_MAP ()};
// {{AFX_INSERT_LOCATION}} // Microsoft Developer Studio Will Insert Additional Declarations Immedierately Before The Previous Line.
#_A76F9E74_DF43_A76F9E74_DF43_A76F9E74_DF43_11D4_AE27_4854E828E6FD__INCLUDED _) / *************************************************************** ********************************************. CPP ** *********************************************************** ******************************************* /// bitmapDialog.cpp: importation file //// Created by David Forrester, January 1, 2001 // Feel Free To Use this code in any way you want.
#include "stdafx.h" #include "bmpdlg.h" #include "bitmapdialog.h" #ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF
/// cbitmapdialog dialog
/ / -------------------------------------------------------------------------------------------- ---------------- // cbitmapdialog :: cbitmapdialog - constructor for cbitmapdialog //// Parameters: // lpsztemplatename - The name of the dialog template resource // nidTemplate - the id of the dialog template resource // pParentWnd - the parent window //// You can leave any or all of the below NULL or 0 to not use it // lpszResourceName - the name of the bitmap resource to load // nIDResource - the ID of The Bitmap Resource to Load // LpszFileName - The FileName of The Bitmap File to Load // Pbitmap - The Bitmap To Use (User Is Responsible For Handling The Bitmap)
// The common constructor for CBitmapDialogvoid CBitmapDialog :: Constructor (LPCTSTR lpszResourceName, UINT nIDResource, LPCTSTR lpszFilename, CBitmap * pBitmap) {m_bTransparent = FALSE; // No transparency m_bStaticTransparent = TRUE; // Static controls are transparent m_bBitmapCreated = FALSE; // Do not automatically release the bitmap m_bBitmapExists = FALSE; // IS there a bitmap m_bClickAnywhereMove = FALSE;? // Clicking anywhere moves // Create a hollow brush used in making static controls transparent m_brushHollow = (HBRUSH) GetStockObject (HOLLOW_BRUSH);
// Load a bitmap from a resource name if (lpszResourceName = NULL!) {LoadBitmap (lpszResourceName, NULL);} // Load a bitmap from a resource ID else if (nIDResource = 0!) {LoadBitmap (nIDResource);} / / Use the passed bitmap else if (pBitmap = 0!) {// NOTE: The user is responsible for handling the bitmap SetBitmap (pBitmap);} // else: No bitmap has been created yet} CBitmapDialog :: CBitmapDialog (LPCTSTR lpszTemplateName , CWnd * pParentWnd, LPCTSTR lpszResourceName, UINT nIDResource, LPCTSTR lpszFilename, CBitmap * pBitmap): CDialog (lpszTemplateName, pParentWnd) {Constructor (lpszResourceName, nIDResource, lpszFilename, pBitmap);}
CBitmapDialog :: CBitmapDialog (UINT nIDTemplate, CWnd * pParentWnd, LPCTSTR lpszResourceName, UINT nIDResource, LPCTSTR lpszFilename, CBitmap * pBitmap): CDialog (nIDTemplate, pParentWnd) {Constructor (lpszResourceName, nIDResource, lpszFilename, pBitmap);}
CBitmapDialog :: CBitmapDialog (LPCTSTR lpszResourceName, UINT nIDResource, LPCTSTR lpszFilename, CBitmap * pBitmap): CDialog () {Constructor (lpszResourceName, nIDResource, lpszFilename, pBitmap);}
/ / -------------------------------------------------------------------------------------------- ---------------- // CBitmapDialog :: loadbitmap - loading a bitmap from a resource or file //// Parameters: // lpszresourcene - the name of the bitmap resource to load. // Set this to NULL if you use lpszFilename.// lpszFilename - the filename of the bitmap file to load.// Set this to NULL if you use lpszResourceName.// nIDResource - the ID of the bitmap resource to load
Bool cbitmapdialog :: loadbitmap (lpctstr lpszresourceename, lpctstr lpszfilename) {// Release the bitmap if it tas created releasebitmap ();
if (! lpszResourceName = NULL) {// Load the bitmap from a resource m_bmBitmap = new CBitmap; (! m_bmBitmap-> LoadBitmap (lpszResourceName)) if return FALSE; // Automatically delete the object m_bBitmapCreated = TRUE; m_bBitmapExists = TRUE;
// Make the window transparent if needed MakeWindowRgn ();} else {// Load the bitmap from a file HBITMAP hbm = (HBITMAP) LoadImage (NULL, lpszFilename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION); if (hbm = = NULL) RETURN FALSE;
// Get the cbitmap Object copybitmapfrom (cbitmap :: fromHandle (HBM)); // m_bmbitmap = cbitmap :: fromHandle (HBM);
Return True;}
BOOL CBitmapDialog :: LoadBitmap (UINT nIDResource) {// Release the bitmap if it was created ReleaseBitmap (); // Load the bitmap m_bmBitmap = new CBitmap; (! M_bmBitmap-> LoadBitmap (nIDResource)) if return FALSE;
// Automatically delete the object m_bbitmapcreated = true; m_bbitmapexists = true;
// make the window transparent if needed makewindowrgn ();
Return True;}
/ / -------------------------------------------------------------------------------------------- ---------------- // CBitmapDialog :: CopybitmapFrom - Sets The Bitmap to a Copy // of a cbitmap.///parameters: // Pbitmap - a Pointer to the bitmap To make a copy of and use.
Bool cBitmapDialog :: CopybitmapFrom (cbitmap * pbitmap) {// Release The bitmap if it tas created releasebitmap ();
// Get The Bitmap Information Bitmap Bmsrc; Pbitmap-> GetBitmap (& BMSRC);
// Get a dc to the Source Bitmap CDC DCSRC; DCSRC.CREATECMOMPALDC (NULL); cBitmap * bmsrcold = dcsrc.selectObject (Pbitmap);
// Create a new bitmap m_bmBitmap = new CBitmap; if return FALSE (m_bmBitmap-> CreateCompatibleBitmap (& dcSrc, bmSrc.bmWidth, bmSrc.bmHeight)!); // Get a DC to the destination bitmap CDC dcDst; dcDst.CreateCompatibleDC (NULL ); Cbitmap * bmdstold = dcdst.selectObject (m_bmbitmap);
// Copy The Bitmap Dcdst.bitblt (0, 0, BMSRC.BMWIDTH, BMSRC.BMHEIGHT, & DCSRC, 0, 0, SRCCOPY);
// Release dcsrc.selectObject (bmsrcold); dcdst.selectObject (bmdstold); dcsrc.deletedc (); dcdst.deletedc ();
// Automatically delete the object m_bbitmapcreated = true; m_bbitmapexists = true;
// make the window transparent if needed makewindowrgn ();
Return True;}
/ / -------------------------------------------------------------------------------------------- ---------------- // CBitmapDialog :: setBitmap - sets the bitmap //// Parameters: // Pbitmap - a Pointer to the bitmap to use./// NOTE: ............................
Void cBitmapDialog :: setBitmap (cbitmap * pbitmap) {// Release the bitmap if it tas created releasebitmap ();
// set the bitmap m_bmbitmap = pbitmap;
// the bitmap exists, but was not created m_bbitmapexists = true;
// make the window transparent if needed makewindowrgn ();
/ / -------------------------------------------------------------------------------------------- ---------------- // CBitmapDialog :: ReleaseBitmap - Releases A Bitmap if it Was // Created Using LoadBitmap OR CopybitmapFrom.
void CBitmapDialog :: ReleaseBitmap () {// Make sure that the bitmap was created using LoadBitmap or CopyBitmapFrom if (m_bBitmapCreated) {// Delete the bitmap m_bmBitmap-> DeleteObject (); delete m_bmBitmap;
// The bitmap Has NOT been created yet m_bbitmapcreated = false;}
M_bbitmapexists = false;} // ----------------------------------------------------------------------------------------------------------------------------------- -------------------- // cbitmapdialog :: setTransparent - sets the dialog's transparent // state.
Void cbitmapdialog :: setTransparent (Bool btransparent) {m_btransparent = btransparent; MakeWindowRgn ();
Void cBitmapDialog :: setTranscolor (ColorRef col) {m_coltrans = col; MakeWindowRGN ();
/ / -------------------------------------------------------------------------------------------- ---------------- // CBitmapDialog :: MakewindowRgn - Makes A Window Region from // The Bitmap and Usees Itness Mode.
Void cBitmapDialog :: MakeWindowRgn () {i (! m_btransparent) {// set the window region to the full of window crect rc; getWindowRect (rc); crgn rgn; rgn.createRectRGN (0, 0, rc.width (), RC . Height ()); setWindowRgn (RGN, TRUE);} else {// set the region to the window Rect minus the client Rect crect rcwnd; getWindowRect (rcwnd);
CRGN RGN; Rgn.createRectrgn (rcwnd.Left, rcwnd.top, rcwnd.right, rcwnd.bottom);
CRECT RCCLIENT; GetClientRect (rcclient); ClientToscreen (rcclient);
CRGN RGNCLIENT; RGNCLIENT.CREATERECTRGN (rcclient.let, rcclient.top, rcclient.right, rcclient.bottom);
// Subtract RGNClient from Rgn Rgn.comBinergn (& RGN, & RGNCLIENT, RGN_XOR);
// Get a dc for the bitmap cdc dcimage; dcimage.createcompatibledc (null); cBitmap * PoldbitMap = DCIMAGE.SELECTOBJECT (m_bmbitmap);
// Get The Bitmap for Width and Height Information Bitmap BM; M_Bmbitmap-> GetBitmap (& BM);
// Get window Width and Height CRECT RC; GetClientRect (RC);
// use the minimum width and height int width = min (bm.bmwidth, rc.width ()); int Height = min (bm.bmheight, rc.Height ());
// USE RLE (RUN-Length) Style Because It Goes Faster. // Row Start Is Where The First Opaque Pixel Is Found. Once // A Transparent Pixel Is Found, a line region is created. // Then Row_Start Becomes The next Next NEXT Opaque pixel. int in_start; // Go through all rows for (int y = 0; y // Go Through All Column for (int x = 0; x // If the last pixel is still opaque, make a region if (row_start = x!) {CRgn rgnAdd;. RgnAdd.CreateRectRgn (rcClient.left row_start, rcClient.top y, rcClient.left x, rcClient.top Y 1); Rgn.comBinergn (& RGN, & Rgnadd, RGN_OR);}} setWindowRgn (RGN, TRUE);}} Begin_MESSAGE_MAP (CBITMAPDIALOG, CDIALOG) / / {{AFX_MSG_MAP (CBITMAPDIALOG) ON_WM_ERASEBKGND () ON_WM_LBUTTONDOWN () ON_WM_CTLCOLOR () //}}} AFX_MSG_MAPEND_MESSAGE_MAP () /// cbitmapdialog message handlers / / -------------------------------------------------------------------------------------------- . BOOL CBitmapDialog :: OnEraseBkgnd (CDC * pDC) {// If no bitmap is loaded, behave like a normal dialog box if (m_bBitmapExists!) Return CDialog :: OnEraseBkgnd (pDC); // Get the client rectangle of the window CRect rc GetClientRect (RC); // Get a dc for the bitmap CDC DCIMAGE; DCIMAGE.CREATECOMPATIBLEDC (PDC); CBITMAP * PoldbitMap = DCIMAGE.SELECTOBJECT (m_bmbitmap); // Get Bitmap Width and Height Bitmap BM; M_Bmbitmap-> GetBitmap (& BM); // use the minimum width and height int width = min (bm.bmwidth, rc.width ()); int Height = min (bm.bmheight, rc.Height ()); // Draw the Bitmap as The window background PDC-> Bitblt (0, 0, rc.width (), rc.height (), & DCImage, 0, 0, srcopy; // Release DCIMage.SelectObject (Poldbitmap); DCIMAGE.DELETEDC (); // Return Value: Nonzero if it Erases the background. Return true; / / -------------------------------------------------------------------------------------------- ---------------- // cbitmapdialog :: ONLBUTTONDOWN - Left mouse button is click // on the window Void cbitmapdialog :: ONLBUTTONDOWN (UINT NFLAGS, CPOINT) {// Todo: Add Your Message Handler Code Here and / or Call Default CDialog :: ONLBUTTONDOWN (NFLAGS, POINT); // Fool Windows Into Thinking That We Clicked on The Caption IF (m_bclickanywheremove) PostMessage (WM_NCLBUTTONDOWN, HTCAPTION, MAKELPAR (Point.x, Point.y);} / / -------------------------------------------------------------------------------------------- ---------------- // CBitmapDialog :: ONCTLCOLOR - SET The Colors for Controls Hbrush CBitmapDialog :: ONCTLCOLOR (CDC * PDC, CWND * PWND, UINT NCTLCOLOR) {Hbrush Hbr = CDIALOG :: ONCTLCOLOR (PDC, PWND, NCTLCOLOR); // Todo: Change Any Attributes of The DC Here // Make static controls transparent if (m_bStaticTransparent && nCtlColor == CTLCOLOR_STATIC) {// Make sure that it's not a slider control char lpszClassName [256]; GetClassName (pWnd-> m_hWnd, lpszClassName, 255); if (strcmp (lpszClassName, TRACKBAR_CLASS) == 0) return CDialog :: OnCtlColor (pDC, pWnd, nCtlColor); pDC-> SetBkMode (TRANSPARENT); return m_brushHollow;} // TODO: return a different brush if the default is not desired return hbr;}