1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with OnDraw Fn

Discussion in 'MFC' started by nutty boy, Apr 14, 2007.

  1. nutty boy

    nutty boy New Member

    Joined:
    Apr 9, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Hi Guys
    I wrore a simple VC "single document" interface program with MS VS_2005
    when i wrote the following code

    Code:
    [COLOR=Red]void CFirstView::OnDraw(CDC* pDC)
    {
    	CFirstDoc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	if (!pDoc)
    		return;
         [COLOR=Blue] CString hello_string="Hi This is my 1st Document View";[/COLOR]
    	// TODO: add draw code for native data here
    	[COLOR=Blue]pDC->TextOutW(0,0,hello_string,hello_string.GetLength());[/COLOR]
    	
    }[/COLOR]
    and copiled my program i get the error message below

    Code:
    [COLOR=DarkOrange]c:\documents and settings\falcon eyes\my documents\visual studio 2005\projects\first\first\firstview.cpp(54) 
    :error C2440:'initializing' : cannot convert from 'const char [32]' to 'ATL::CStringT<BaseType,StringTraits>'
    1>with
    1>[
    1>BaseType=wchar_t,
    1>StringTraits=StrTraitMFC_DLL<wchar_t>
    1>]
    1>Constructor for class 'ATL::CStringT<BaseType,StringTraits>' is declared 'explicit'
    1>with
    1>[
    1>BaseType=wchar_t,
    1>StringTraits=StrTraitMFC_DLL<wchar_t>[/COLOR]
    does any one know why i'm getting an error message and what's the solution
     
    Last edited by a moderator: Apr 15, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    You need to be providing the TCHAR

    There are 2 way to get out of it.
    1. Have the hello_string declared as TCHAR array
    2. Instead of passing the CString use the GetBuffer function to have the char array and then after the function returns use the ReleaseBuffer.
     
  3. nutty boy

    nutty boy New Member

    Joined:
    Apr 9, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Hi shabbir
    1st thanks for ur reply 2nd could u please give me more details
    what u mean by TCHAR array if u have any document about that or any usefull websites link

    and how can i declare my string as TCHAR array

    alsow

    could u pls write the code for how to pass a string using the GetBuffer
    Forgiveme i'm still learning how to program
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    TCHAR is nothing but declaring char array. Something Like TCHAR *str ="Your string"; or even TCHAR str[] ="Your String";

    For GetBuffer it should be something like
    Code:
    CString hello_string="Hi This is my 1st Document View";
    pDC->TextOutW(0,0,hello_string.GetBuffer(),hello_string.GetLength()  );
    hello_string.ReleaseBuffer();
    I thought I was clear enough with the above post and so not gave any example.
     
  5. nutty boy

    nutty boy New Member

    Joined:
    Apr 9, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    thanks for concern
     
  6. nutty boy

    nutty boy New Member

    Joined:
    Apr 9, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Pls shabbir
    when i wrote the code

    TCHAR hello_string="Hi This is my 1st Document View";
    // TODO: add draw code for native data here
    pDC->TextOutW(0,0,hello_string,32);
    I got this error message

    error C2440: 'initializing' : cannot convert from 'const char [32]' to 'TCHAR'
    is there's some wrong i did
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Try making the TCHAR const or as of now you can go for something like const char as well because you dont need to confuse what is T there.
     
  8. nutty boy

    nutty boy New Member

    Joined:
    Apr 9, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    I changed the properties of my project as below
    General -> charcter set -> not set
    and i used TextOutA function and it works although there's warnning message
    Thanks
     

Share This Page