CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 34
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    [RESOLVED] about fonts

    on my label class:
    Code:
    private:
          CHOOSEFONT chFont;
    public:
    CHOOSEFONT getFont()
        {
            return chFont;
        }
    
        void setFont(const CHOOSEFONT font)
        {
            chFont=font;
            clrTextColor=chFont.rgbColors;
            HFONT hFont=CreateFontIndirect(chFont.lpLogFont);
            SendMessage(hwnd, WM_SETFONT,(WPARAM) hFont, TRUE);//setting the control font for resize the control
            if(blnAutoSize==true)//the autosize is true
                setAutoSize(true);
        }
    and see how i show the font dialog(using the label font):
    Code:
    CHOOSEFONT ShowSelectFont(const CHOOSEFONT cf1)
    {
        CHOOSEFONT cf = {sizeof(CHOOSEFONT)};
        cf=cf1;
        HWND hwnd=GetForegroundWindow();
    
        cf.Flags = CF_EFFECTS | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
        cf.hwndOwner = hwnd;
        ChooseFont(&cf);
        return cf;
    }
    the lpLogFont, CHOOSEFONT class member, it's a pointer. the problem is the font dialog isn't showed. what i belive is that i lose the pointer between the getFont() and ShowSelectFont(). what anyone can tell me?

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: about fonts

    1. CHOOSEFONT is a structure, NOT a pointer.
    2. You need to check the return value from ChooseFont(&cf). If it is 0 (FALSE), you should call CommDlgExtendedError(); to see what was wrong.
    3. You didn't show how you create CHOOSEFONT structure in the first place. Did you set its lStructSize member? To what?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about fonts

    Quote Originally Posted by VladimirF View Post
    1. CHOOSEFONT is a structure, NOT a pointer.
    2. You need to check the return value from ChooseFont(&cf). If it is 0 (FALSE), you should call CommDlgExtendedError(); to see what was wrong.
    3. You didn't show how you create CHOOSEFONT structure in the first place. Did you set its lStructSize member? To what?
    1 - the CHOOSEFONT don't but the lpLogFont member is
    3 - thanks. i didn't notice that :P
    now the code works fine.

    anotherthing: please see these function:
    Code:
    CHOOSEFONT ShowSelectFont()
    {
        HWND hwnd=GetForegroundWindow();
        CHOOSEFONT cf = {sizeof(CHOOSEFONT)};
        LOGFONT* lf = new LOGFONT;
        HDC hdc=GetDC(hwnd);
    
        HFONT hlf =(HFONT) GetCurrentObject(hdc,OBJ_FONT);
    
        GetObject(hlf,sizeof(LOGFONT),lf);
        cf.Flags = CF_EFFECTS | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
        cf.hwndOwner = hwnd;
        cf.lpLogFont = lf;
        cf.rgbColors =GetTextColor(hdc);
        ChooseFont(&cf);
        ReleaseDC(hwnd,hdc);
        cf.lpLogFont = lf;
        return cf;
    }
    the lf use 'new', but i'm not using the 'delete'. so it's a memory leak. but honestly i can't use the 'delete', or i lose the lf data. can you correct me these?
    Last edited by Cambalinho; July 20th, 2015 at 11:52 AM.

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

    Re: about fonts

    Quote Originally Posted by Cambalinho View Post
    1 - the CHOOSEFONT don't but the lpLogFont member is
    3 - thanks. i didn't notice that :P
    now the code works fine.

    anotherthing: please see these function:
    Code:
    CHOOSEFONT ShowSelectFont()
    {
        HWND hwnd=GetForegroundWindow();
        CHOOSEFONT cf = {sizeof(CHOOSEFONT)};
        LOGFONT* lf = new LOGFONT;
        HDC hdc=GetDC(hwnd);
    
        HFONT hlf =(HFONT) GetCurrentObject(hdc,OBJ_FONT);
    
        GetObject(hlf,sizeof(LOGFONT),lf);
        cf.Flags = CF_EFFECTS | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
        cf.hwndOwner = hwnd;
        cf.lpLogFont = lf;
        cf.rgbColors =GetTextColor(hdc);
        ChooseFont(&cf);
        ReleaseDC(hwnd,hdc);
        cf.lpLogFont = lf;
        return cf;
    }
    the lf use 'new', but i'm not using the 'delete'. so it's a memory leak. but honestly i can't use the 'delete', or i lose the lf data. can you correct me these?
    But why do you pass the CHOOSEFONT from a function as the return value?
    Why not just pass it to the function as a parameter?
    Like (reference)
    Code:
    BOOL ShowSelectFont(CHOOSEFONT& chooseFont )
    or (pointer)
    Code:
    BOOL ShowSelectFont(CHOOSEFONT* chooseFont )
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about fonts

    Quote Originally Posted by VictorN View Post
    But why do you pass the CHOOSEFONT from a function as the return value?
    Why not just pass it to the function as a parameter?
    Like (reference)
    Code:
    BOOL ShowSelectFont(CHOOSEFONT& chooseFont )
    or (pointer)
    Code:
    BOOL ShowSelectFont(CHOOSEFONT* chooseFont )
    honestly, by my experience, is more easy my way. but thinking more... i belive it's a opinion... it depends on programmer way

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: about fonts

    Think RAII
    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)

  7. #7
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about fonts

    Quote Originally Posted by 2kaud View Post
    Think RAII
    i'm sorry... what means RAII?

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

    Re: about fonts

    Resource Acquisition Is Initialisation. See http://stackoverflow.com/questions/2...alization-raii and many other internet articles. If you program with c++ this is something with which you really should be familiar.
    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,396

    Re: about fonts

    Quote Originally Posted by Cambalinho View Post
    i'm sorry... what means RAII?
    A very fast search with Google shows you https://www.google.ch/search?q=RAII&..._sm=0&ie=UTF-8 where every from the first 10 items explains you what it is!
    Victor Nijegorodov

  10. #10
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about fonts

    i never ear about RAII, now i more or less understand it.
    but i continue with my last question\confusion: why i can't use 'delete'?
    the rule is: when we use the 'new' we must use the 'delete', when isn't needed. but i can't, in these case, delete the lf or i lose the data

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: about fonts

    Quote Originally Posted by Cambalinho View Post
    i never ear about RAII, now i more or less understand it.
    but i continue with my last question\confusion: why i can't use 'delete'?
    the rule is: when we use the 'new' we must use the 'delete', when isn't needed. but i can't, in these case, delete the lf or i lose the data
    So you don't understand RAII. The new and the corresponding delete go in a class - with delete in the destructor so memory is not 'lost'.
    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)

  12. #12
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about fonts

    Quote Originally Posted by 2kaud View Post
    So you don't understand RAII. The new and the corresponding delete go in a class - with delete in the destructor so memory is not 'lost'.
    understood... thanks for all
    Last edited by Cambalinho; July 20th, 2015 at 12:58 PM.

  13. #13
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] about fonts

    correct me anotherthing about fonts color and control backcolor : when i put the child control transparent(regions in pixel way) a see 1 color arround the letters(not a backcolor)... why these happens?
    Name:  font name incorrect.gif
Views: 58
Size:  1.4 KB

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: [RESOLVED] about fonts

    Font has nothing to do with color.

    SetTextColor function
    SetBkColor function
    SetBkMode function
    Last edited by Igor Vartanov; July 22nd, 2015 at 01:33 AM.
    Best regards,
    Igor

  15. #15
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] about fonts

    Quote Originally Posted by Igor Vartanov View Post
    i continue with that white color
    what i can change is instead be white, been black. but i can't avoid that color

Page 1 of 3 123 LastLast

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