CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29

Thread: Help with mfc

  1. #1
    Join Date
    May 2015
    Posts
    503

    Help with mfc

    Hello All,
    Merry Christmas and Happy new year ..

    I have to again do some changes to legacy MFC code !

    There is some renderer function to draw lines..But now i need to add a rectangle to display the list of cells and their power levels.

    I have added a following function:
    Code:
    void EmfCellListRend::DrawRectangle(EmfCellListRendDrawingParams* pParams, TDC& dc, CEmfCellList *pCelllist)
    {
    	ILogicalLayerManager&	rLogLayManager  = DataManager::GetInstance().GetLogicalLayer();
    
    	//Rectangle(dc.GetHDC(), midPointX - m_markerSize, midPointY - m_markerSize, midPointX + m_markerSize, midPointY + m_markerSize);
    	//GetWindowRect ( dc.GetHDC()
    	//int rectHeight = DrawText( hMemDc, text.c_str(), text.size(), &rc, DT_CALCRECT |DT_WORDBREAK ); 
    	// Get formating rectangle height
    
    	//  Populate the cell list got from the renderer.  
    
    	tstring message = _T("EMF Cell list");
    	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 str3 = _T("\n");
        tstring str4 = _T(pObjCell->GetID());
        tstring str5 = _T(std::to_string(pCelllist[i].m_fRxSignal_dBm));
    
    		message	 +=str3+str4+str5;
    	}
    	HWND hwnd = WindowFromDC(dc);
    
    	RECT rect;
    	HWND bgHandle(0);
    	GetClientRect(hwnd, &rect);
    	SetTextColor(dc, 0x00000000);
    	SetBkMode(dc, TRANSPARENT);
    	rect.left = 40;
    	rect.top = 10;
    	DrawText(dc, message.c_str(), -1, &rect, DT_SINGLELINE | DT_NOCLIP );
    	//DrawText(dc, message.c_str(), -1, &rect, DT_CALCRECT|DT_WORDBREAK );
    	
    	DeleteDC(dc);
    
    	dc.RestorePen();
    }

    The above drawtext somehow doesnot honour new lines..
    Also if i change to below commented Drawtext, it doesnot display anything at all..


    I tried to install and create a new MFC project in my home PC, and both of above DrawText doesnot work !!!... I knew little MFC but never drew with MFC

    Thanks a lot for the help

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,235

    Re: Help with mfc

    At a first look, your sample code contains not any MFC stuff. For example, TDC seems to be a Borland's OWL Library class and further are called just plain WinAPI functions (not MFC). I know, sometimes dealing with legacy code can be a real mess, but no problem, always is a place to make it better. So my advice is the following: when you are dealing with MFC, use as much as possible only MFC stuff. In this particular case, use MFC device context classes (CDC or CClientDC) instead of plain WinAPI functions calls, CString instead std::string, CRect instead of RECT structure. That can further make peogrammer's life much more reasier.

    So far, here are some remarks, based on your sample code.
    • if pass DT_CALCRECT format flag, the function DrawText just adjust the necessary rectangle and draws nothing;
    • if pass DT_SINGLELINE format flag, carriage returns ('\r') and line feeds ('\n') do not break the line;
    • do not call DeleteDC for a device context whose handle was obtained by calling the GetDC.

    See also:

    And here is a simplified example of drawing a text in an MFC application:
    Code:
    void CDemoSdiView::RenderText(CDC* pDC, int left, int top, const CString& strText)
    {
        CRect rc;
        // calculate the necessary rectangle
        pDC->DrawText(strText, rc, DT_LEFT|DT_TOP|DT_CALCRECT);
        // offset the rectangle to top-left coordinates
        rc.OffsetRect(left, top);
        // finally draw the text
        pDC->DrawText(strText, rc, DT_LEFT|DT_TOP);
    }
    
    void CDemoSdiView::OnDraw(CDC* pDC)
    {
        CString strText = _T("Ala\nBala\nPortocala");
        RenderText(pDC, 10, 10, strText);
    }
    Enjoy, and good luck!
    Last edited by ovidiucucu; January 2nd, 2023 at 07:40 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    May 2015
    Posts
    503

    Re: Help with mfc

    @ovidiucucu : Thankyou very much. Sorry i missed the above post.

    Btw, I had my drawtext working with the following code:

    Code:
     void EmfCellListRend::DrawRectangle(EmfCellListRendDrawingParams* pParams, TDC& dc, CEmfCellList *pCelllist)
    {
    	ILogicalLayerManager&	rLogLayManager  = DataManager::GetInstance().GetLogicalLayer();
    
    	tstring message = _T("EMF Cell list");
    	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 std(strTechType, strTechType.GetLength());
    		tstring sTechtype		= _T(std);
    		tstring		sCellId		= _T(pObjCell->GetID());
    		tstring		sEmf		= _T(std::to_string(pCelllist[i].m_dCellEMF_Vm));
    
    		message	 +=sNewLine+sTechtype+" "+sCellId+"  "+sEmf;
    	}
    	HWND hwnd = WindowFromDC(dc);
    	RECT rc={ 0, 0, 200, 0 };
    	UINT format = DT_LEFT | DT_TOP | DT_EDITCONTROL | DT_WORDBREAK;
    	DrawText(dc, message.c_str(), -1, &rc, format | DT_CALCRECT);
    	DrawText(dc, message.c_str(), -1, &rc, format);
    	DeleteDC(dc);
    
    	dc.RestorePen();
    }
    Now this works ok. But the font is too big. Could MFC gurus help me, with these please ? Thankyou very much in advance

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,407

    Re: Help with mfc

    Use HDC function SelectObject to set a preferable font to the device context (dc) you are using to draw,
    You can either a create some new font or just get a used font of some window using WM_GETFONT message
    Victor Nijegorodov

  5. #5
    Join Date
    May 2015
    Posts
    503

    Re: Help with mfc

    Thankyou very much Victor. I tried to extract the HDC from the owl context.

    But somehow it doesnot work
    Code:
    HDC hdc = dc.GetHDC();
    	CFont* pCurrent = hdc.GetCurrentFont();
    
    LOGFONT lf ;
    pCurrent->GetLogFont(&lf) ;
    lf.lfHeight -= -4 ; // make the font larger by 4
    // yopu may need to check what you actually get in the LOGFONT object at this point
    // to make sure you adjust the correct parameter, in the right direction to make the font bigger
    CFont local_font ;
    
    local_font.CreateFontIndirect(&lf) ;
    
    pDC->SelectObject(&local_font) ;
    DrawText(...) ;
    pDC->SelectObject(pCurrent) ;

  6. #6
    Join Date
    May 2015
    Posts
    503

    Re: Help with mfc

    I tried to the following way : But it didnot work ( means font remained unchanged)

    Code:
     void EmfCellListRend::DrawRectangle(EmfCellListRendDrawingParams* pParams, TDC& dc, CEmfCellList *pCelllist)
     {
    	ILogicalLayerManager&	rLogLayManager  = DataManager::GetInstance().GetLogicalLayer();
    
    	CDC* pDC = CDC::FromHandle(dc.GetHDC());
    
    	CFont* pCurrent = pDC->GetCurrentFont();
    
    	LOGFONT lf;
    
    	pCurrent->GetLogFont(&lf) ;
    
    	lf.lfWeight = FW_EXTRABOLD;
    	lf.lfHeight /= 10; // make the font larger by 4
    Not sure the blue coloured line is correct ? (Eventhough it compiles , but the font size is not reduced)

  7. #7
    Join Date
    May 2015
    Posts
    503

    Re: Help with mfc

    @ ovidiucucu, Victor Or Arjay, could you please help with how to convert the owl context TDC to MFC drawing context. The above code doesnot change the font.

  8. #8
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,832

    Re: Help with mfc

    I don't know MFC/OWL, but after you're changed lf don't you need to select that font back into the device context?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,407

    Re: Help with mfc

    Quote Originally Posted by pdk5 View Post
    @ ovidiucucu, Victor Or Arjay, could you please help with how to convert the owl context TDC to MFC drawing context. The above code doesnot change the font.
    1. What is TDC?
    2. Ovidiu already showed you an example (MFC!) to draw the text properly. Why don't you use it?
    Victor Nijegorodov

  10. #10
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,832

    Re: Help with mfc

    TDC is from Borland's C++ Builder OWL (as ovidiucucu said above)
    https://owlnext.sourceforge.net/help...1_1_t_d_c.html

    But you don't mix Owl and MFC in the same project. You either use one or the other!

    @pdk5 - are you using OWL or MFC?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,235

    Re: Help with mfc

    You've probably got the "System" font, which size can be modified only from the system settings. Or can be another font that does not support the size you have provided.
    Anyway it's not a good idea to get the current font from the device context and modify its attributes "on the fly".
    Just create your own font! Here is an example:
    Code:
    void CDemoSdiView::RenderText(CDC* pDC, int left, int top, const CString& strText)
    {
        CFont font;
        font.CreatePointFont(80, _T("Verdana"));
        CFont* pOldFont = pDC->SelectObject(&font);
        int nOldMapMode = pDC->SetMapMode(MM_TEXT);
    
        CRect rc;
        // calculate the necessary rectangle
        pDC->DrawText(strText, rc, DT_LEFT|DT_TOP|DT_CALCRECT);
        // offset the rectangle to top-left coordinates
        rc.OffsetRect(left, top);
        // filally draw the text
        pDC->DrawText(strText, rc, DT_LEFT|DT_TOP);
    
        // restore DC status
        pDC->SetMapMode(nOldMapMode);
        pDC->SelectObject(pOldFont);
    }
    Additional notes:
    • After drawing, do not forget to restore the initial DC status, as shown in the above example. Alternatively, you can use CDC::SaveDC and CDC::RestoreDC for doing that easier.
      Code:
          // save the DC status    
          int nDCStatus = pDC->SaveDC();
          // ...
          // ...
          // restore DC status
          pDC->RestoreDC(nDCStatus);
    • For simplicity, I used in my example CFont::CreatePointFont. If you want to also set other custom attributes, like weight for example, call CFont::CreateFont, CFont::CreateFontIndirect, or CFont::CreatePointFontIndirect. See CFont Class in Microsoft Learn documentation.
    • The fizical size of a text depends on DC type and mapping mode. That's the reason I called CDC::SetMapMode. For more info, have a look at LOGFONT Structure documentation.
    Last edited by ovidiucucu; January 7th, 2023 at 02:40 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  12. #12
    Join Date
    May 2015
    Posts
    503

    Re: Help with mfc

    Thankyou very much. (somehow my email is getting filled with spam and not getting notify when there is response, i need to sort that out.) sorry for the delay.

    ovidiucucu : I tried the new font option and font size is exactly the size i was looking for and it works very well. Thankyou very much.
    I need to tweak somethings, and going ahead also when i have sometime, need to check why the owl and MFC context , was not working
    Btw, i also now need to make sure, that the rectangle is top left if the selected pixel is top down. Just to make sure the text doesnot overwrite the pixel.. But i.e next task.. May be i'll be disturbing you ppl in the forum

    Thankyou again, kaud, victor and ovidiucucu ..Very helpful

  13. #13
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,832

    Re: Help with mfc

    need to check why the owl and MFC context
    Which compiler are you using? MFC is for MS VS. OWL (replaced by VCL in 1997) is for Embarcadero C++ Builder.

    There is now OWLNext which can be used with either C++ Builder or VS providing you have an existing OWL licence.
    https://sourceforge.net/p/owlnext/wiki/Main_Page/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    May 2015
    Posts
    503

    Re: Help with mfc

    Thankyou kaud
    Were using visual studio . Mostly MFC, but there is legacy owl still there. Not sure how VS handles that

  15. #15
    Join Date
    May 2015
    Posts
    503

    Re: Help with mfc

    Btw, Im trying to position the rectangle, on parent dialog at the top right. Then the issue is , i donot know the size of parent dialog.

    Name:  temp.jpg
Views: 559
Size:  7.5 KB
    Last edited by pdk5; January 7th, 2023 at 11:27 AM.

Page 1 of 2 12 LastLast

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