CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2015
    Posts
    503

    DrawText - Problem with background color

    Hello,

    I have the following code:
    I am trying to change the background colour for the rectangle (of the DrawText), but the background colour only comes when there is text in the rectangle.

    Code:
    void EmfCellListRend::DrawRectangle(EmfCellListRendDrawingParams* pParams, TDC& dc, CEmfCellList *pCelllist, int nEmfUnit, bool bDisplayRight)
     {
    	ILogicalLayerManager&	rLogLayManager  = DataManager::GetInstance().GetLogicalLayer();
    
    	// The font is not changing when i use the owl dc to mfc dc.
    	// Need to check why !!!
    	CDC* pDC		= CDC::FromHandle(dc.GetHDC());
    	CFont* pCurrent = pDC->GetCurrentFont();
    	
    	// TODO_PRIYA_EMF3: Choosing a new font (unrelated to dc context works)
    	CFont font;
    	font.CreatePointFont(80, _T("Verdana"));
    
    	CFont* pOldFont = pDC->SelectObject(&font);
    	SetBkMode(dc,		OPAQUE);
    	SetBkColor(dc,		TColor(RGB(0, 255, 0)));
    
    	tstring sEmfUnit	=_T(TXPower2::GetEmfUnitString(TXPower2::GetCurrentEmfUnit()));
    	tstring sFinaText	=_T("EMF Cell list ");
    	sFinaText			+=sEmfUnit;
    	for (int i = 0; i < (int)pParams->GetNumCovCells(); i++)
    	{
    		const MultiTechCell* pObjCell((const MultiTechCell*)rLogLayManager.Find(OT_MULTI_TECH_CELL, pCelllist[i].m_CellKey));
    		tstring		sNewLine		= _T("\n");
    		TgNetType	eTechType		= 	pObjCell->GetActiveTechnologyMode();
    		CString		strTechType		= MAIN_NET_TYPE_STRINGS_SWITCH( eTechType );
    		std::string str(strTechType, strTechType.GetLength());
    		tstring		sTechtype		= _T(str);
    		tstring		sCellId			= _T(pObjCell->GetID());
    
    		const double dCurrentEmf_dBVm = pCelllist[i].m_dCellEMF_dBVm;
    		ASSERT(dCurrentEmf_dBVm > -200.0);// If dCurrentEmf_dBVm is <= -200.0, we should probably force dCurrentEmf_dBVm to be -200 also ??
    
    		// Convert dBV/m to dBmW/m2 if necessary
    		TXPower2 helper(dCurrentEmf_dBVm, EMFUNIT::dBVm);
    		const EMFUNIT emfUnit	= TXPower2::GetCurrentEmfUnit();					// The EMF unit chosen by the user in the preferences
    		const double dEmf		= static_cast<float>(helper.EMFLevelAs( emfUnit ));	// Either dBV/m or dBmW/m2
    
    		sFinaText+=sNewLine+sTechtype+" "+sCellId+"  "+_T(std::to_string(dEmf));
    	}
    
    	HWND hwnd = WindowFromDC(dc);
    	CRect rect;
    	GetClientRect(hwnd, &rect);
    
    	RECT rc={ 0, 0, 400, 0 };
    	UINT format = DT_LEFT | DT_TOP | DT_EDITCONTROL | DT_WORDBREAK;
    	DrawText(dc, sFinaText.c_str(), -1, &rc, format | DT_CALCRECT);
    
    	// Adjust co-ordinates of rectangle, to fit the parent window and shift 
    	// rectangle to right.
    	// From the mouse co-ordinates, check if co-ordinates fall on
    	// right corner, choose to display on left and vice versa.
    	if( bDisplayRight)
    	{
    		int long temp = rc.right - rc.left;
    		rc.left = rect.right - temp;
    		rc.right = rect.right;
    		rc.top = rect.top;
    	}
    
    	DrawText(dc, sFinaText.c_str(), -1, &rc, format);
    
    	pDC->SelectObject(pCurrent) ;
    
    	DeleteDC(dc);
    
    	dc.RestorePen();
    Is there anyway to change for the full rectangle ?

    thankyou very much for the comments and help

  2. #2
    Join Date
    May 2015
    Posts
    503

    Re: DrawText - Problem with background color

    Btw, Now the background color changes for the entire rectangle with the followig code change.


    Code:
    void EmfCellListRend::DrawRectangle(EmfCellListRendDrawingParams* pParams, TDC& dc, CEmfCellList *pCelllist, int nEmfUnit, bool bDisplayRight)
     {
    	ILogicalLayerManager&	rLogLayManager  = DataManager::GetInstance().GetLogicalLayer();
    
    	// The font is not changing when i use the owl dc to mfc dc.
    	// Need to check why !!!
    	CDC* pDC		= CDC::FromHandle(dc.GetHDC());
    	CFont* pCurrent = pDC->GetCurrentFont();
    	
    	// TODO_PRIYA_EMF3: Choosing a new font (unrelated to dc context works)
    	CFont font;
    	font.CreatePointFont(80, _T("Verdana"));
    
    	CFont* pOldFont = pDC->SelectObject(&font);
    	SetBkMode(dc,		OPAQUE);
    	SetBkColor(dc,		TColor(RGB(50, 151, 151)));
    
    	tstring sEmfUnit	=_T(TXPower2::GetEmfUnitString(TXPower2::GetCurrentEmfUnit()));
    	tstring sFinaText	=_T("EMF Cell list ");
    	sFinaText			+=sEmfUnit;
    	for (int i = 0; i < (int)pParams->GetNumCovCells(); i++)
    	{
    		const MultiTechCell* pObjCell((const MultiTechCell*)rLogLayManager.Find(OT_MULTI_TECH_CELL, pCelllist[i].m_CellKey));
    		tstring		sNewLine		= _T("\n");
    		TgNetType	eTechType		= 	pObjCell->GetActiveTechnologyMode();
    		CString		strTechType		= MAIN_NET_TYPE_STRINGS_SWITCH( eTechType );
    		std::string str(strTechType, strTechType.GetLength());
    		tstring		sTechtype		= _T(str);
    		tstring		sCellId			= _T(pObjCell->GetID());
    
    		const double dCurrentEmf_dBVm = pCelllist[i].m_dCellEMF_dBVm;
    		ASSERT(dCurrentEmf_dBVm > -200.0);// If dCurrentEmf_dBVm is <= -200.0, we should probably force dCurrentEmf_dBVm to be -200 also ??
    
    		// Convert dBV/m to dBmW/m2 if necessary
    		TXPower2 helper(dCurrentEmf_dBVm, EMFUNIT::dBVm);
    		const EMFUNIT emfUnit	= TXPower2::GetCurrentEmfUnit();					// The EMF unit chosen by the user in the preferences
    		const double dEmf		= static_cast<float>(helper.EMFLevelAs( emfUnit ));	// Either dBV/m or dBmW/m2
    
    		sFinaText+=sNewLine+sTechtype+" "+sCellId+"  "+_T(std::to_string(dEmf));
    	}
    
    	HWND hwnd = WindowFromDC(dc);
    	CRect rect;
    	GetClientRect(hwnd, &rect);
    
    	RECT rc={ 0, 0, 400, 0 };
    	UINT format = DT_LEFT | DT_TOP | DT_EDITCONTROL | DT_WORDBREAK;
    	DrawText(dc, sFinaText.c_str(), -1, &rc, format | DT_CALCRECT);
    
    	// Adjust co-ordinates of rectangle, to fit the parent window and shift 
    	// rectangle to right.
    	// From the mouse co-ordinates, check if co-ordinates fall on
    	// right corner, choose to display on left and vice versa.
    	if( bDisplayRight)
    	{
    		int long temp = rc.right - rc.left;
    		rc.left = rect.right - temp;
    		rc.right = rect.right;
    		rc.top = rect.top;
    	}
    	HBRUSH brush = CreateSolidBrush(RGB(50, 151, 151));
    	FillRect(dc, &rc, brush);
    	DeleteObject(brush);
    	DrawText(dc, sFinaText.c_str(), -1, &rc, format);
    
    	pDC->SelectObject(pCurrent) ;
    
    	DeleteDC(dc);
    
    	dc.RestorePen();
    Not sure if the code is correct. But works ok. I plan to add boarders lines to rectangle ... (need to check that

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured