Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Very weird change font problem

Very weird change font problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++toolshelpquestionlearning
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    econy
    wrote on last edited by
    #1

    A dialog based program, I put a function to draw a string on the screen. the function is member function of TestDlg.h; But if I put the function in TestDlg.cpp, the screen show some weird messy characters. Then I put the same function in another cpp file, most of this file are global functions. JTest.cpp, with Jtest.h. Weird thing is it works, screen can show correct characters. Situation like:

    TestDlg.cpp
    IMPLEMENT_DYNAMIC (CTestDlg,CDialog)
    ...
    void TestDlg::DrawInfoLabel(void)
    {
    CDC* pDC;
    CRect rect(562,448,688,467);
    CString lebelStr;

    HDC hDC;					
    LOGFONT lf;		
    HFONT FontNew, FontOld;		
    
    pDC = GetDC();
    hDC = pDC->GetSafeHdc();
    
    memset(&lf, 0, sizeof(LOGFONT));	
    lf.lfHeight = -14 \* GetDeviceCaps(hDC,LOGPIXELSY)/72;
    lf.lfWeight = 0;
    //WCHAR lffn\[\] = TEXT("SimSun(TrueType)"); 
    WCHAR lffn\[\] = TEXT("FZYaoTi(OpenType)"); 
    wcscpy(lf.lfFaceName,lffn);
    lf.lfCharSet = GB2312\_CHARSET;
    FontNew = CreateFontIndirect(&lf);
    FontOld = (HFONT)SelectObject(hDC,FontNew);
    
    pDC->SetTextColor(RGB(0xFF,0x00,0x00));
    SetBkMode(hDC,TRANSPARENT);
    
    DrawText(hDC,\_T("??"),-1,rect,DT\_CENTER|DT\_VCENTER|DT\_SINGLELINE);
    
    SelectObject(hDC,FontOld);
    DeleteObject(FontNew);
    ReleaseDC(pDC);
    

    }

    In JTest.cpp

    void TestDlg::DrawInfoLabel(void)
    {
    ....
    }

    Every line is same, only difference is file name. I feel it is the following statement affect the screen output. IMPLEMENT_DYNAMIC (CTestDlg,CDialog)

    // Microsoft Visual C++ generated resource script.
    //
    #include "resource.h"

    #define APSTUDIO_READONLY_SYMBOLS
    /////////////////////////////////////////////////////////////////////////////
    //
    // Generated from the TEXTINCLUDE 2 resource.
    //
    #include "afxres.h"
    #include "newres.h"

    /////////////////////////////////////////////////////////////////////////////
    #undef APSTUDIO_READONLY_SYMBOLS

    /////////////////////////////////////////////////////////////////////////////
    // English (U.S.) resources

    #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
    #ifdef _WIN32
    LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
    #endif //_WIN32

    #ifdef APSTUDIO_INVOKED
    /////////////////////////////////////////////////////////////////////////////
    //
    // TEXTINCLUDE
    //

    1 TEXTINCLUDE
    BEGIN
    "resource.h\0"
    END

    2 TEXTINCLUDE
    BEGIN
    "#include ""afxres.h""\r\n"
    "#include ""newres.h""\r\n"
    "\0"
    END
    ........

    The above is .rc file. Hope someone can explain t

    L 1 Reply Last reply
    0
    • E econy

      A dialog based program, I put a function to draw a string on the screen. the function is member function of TestDlg.h; But if I put the function in TestDlg.cpp, the screen show some weird messy characters. Then I put the same function in another cpp file, most of this file are global functions. JTest.cpp, with Jtest.h. Weird thing is it works, screen can show correct characters. Situation like:

      TestDlg.cpp
      IMPLEMENT_DYNAMIC (CTestDlg,CDialog)
      ...
      void TestDlg::DrawInfoLabel(void)
      {
      CDC* pDC;
      CRect rect(562,448,688,467);
      CString lebelStr;

      HDC hDC;					
      LOGFONT lf;		
      HFONT FontNew, FontOld;		
      
      pDC = GetDC();
      hDC = pDC->GetSafeHdc();
      
      memset(&lf, 0, sizeof(LOGFONT));	
      lf.lfHeight = -14 \* GetDeviceCaps(hDC,LOGPIXELSY)/72;
      lf.lfWeight = 0;
      //WCHAR lffn\[\] = TEXT("SimSun(TrueType)"); 
      WCHAR lffn\[\] = TEXT("FZYaoTi(OpenType)"); 
      wcscpy(lf.lfFaceName,lffn);
      lf.lfCharSet = GB2312\_CHARSET;
      FontNew = CreateFontIndirect(&lf);
      FontOld = (HFONT)SelectObject(hDC,FontNew);
      
      pDC->SetTextColor(RGB(0xFF,0x00,0x00));
      SetBkMode(hDC,TRANSPARENT);
      
      DrawText(hDC,\_T("??"),-1,rect,DT\_CENTER|DT\_VCENTER|DT\_SINGLELINE);
      
      SelectObject(hDC,FontOld);
      DeleteObject(FontNew);
      ReleaseDC(pDC);
      

      }

      In JTest.cpp

      void TestDlg::DrawInfoLabel(void)
      {
      ....
      }

      Every line is same, only difference is file name. I feel it is the following statement affect the screen output. IMPLEMENT_DYNAMIC (CTestDlg,CDialog)

      // Microsoft Visual C++ generated resource script.
      //
      #include "resource.h"

      #define APSTUDIO_READONLY_SYMBOLS
      /////////////////////////////////////////////////////////////////////////////
      //
      // Generated from the TEXTINCLUDE 2 resource.
      //
      #include "afxres.h"
      #include "newres.h"

      /////////////////////////////////////////////////////////////////////////////
      #undef APSTUDIO_READONLY_SYMBOLS

      /////////////////////////////////////////////////////////////////////////////
      // English (U.S.) resources

      #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
      #ifdef _WIN32
      LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
      #endif //_WIN32

      #ifdef APSTUDIO_INVOKED
      /////////////////////////////////////////////////////////////////////////////
      //
      // TEXTINCLUDE
      //

      1 TEXTINCLUDE
      BEGIN
      "resource.h\0"
      END

      2 TEXTINCLUDE
      BEGIN
      "#include ""afxres.h""\r\n"
      "#include ""newres.h""\r\n"
      "\0"
      END
      ........

      The above is .rc file. Hope someone can explain t

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hi, This sounds like it might be related to code inline expansion. Where are you calling the function DrawInfoLabel()? Are you calling this from the WM_PAINT handler? Best Wishes, -David Delaune

      E 1 Reply Last reply
      0
      • L Lost User

        Hi, This sounds like it might be related to code inline expansion. Where are you calling the function DrawInfoLabel()? Are you calling this from the WM_PAINT handler? Best Wishes, -David Delaune

        E Offline
        E Offline
        econy
        wrote on last edited by
        #3

        I call that function just in a general member function.

        CTestDlg
        {
        public:
        void refreshInfoLabel(void);

        }

        void CTestDlg::refreshInfoLabel(void)
        {
        DrawInfoLabel()
        }

        L 1 Reply Last reply
        0
        • E econy

          I call that function just in a general member function.

          CTestDlg
          {
          public:
          void refreshInfoLabel(void);

          }

          void CTestDlg::refreshInfoLabel(void)
          {
          DrawInfoLabel()
          }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Hi, It is generally not recommended to draw outside of the WM_PAINT/WM_ERASEBKGND handlers. When you draw outside of WM_PAINT you need to invalidate the window rectangle/region so that the subsystem knows to redraw it. InvalidateRect function[^] InvalidateRgn function[^] Best Wishes, -David Delaune

          E 1 Reply Last reply
          0
          • L Lost User

            Hi, It is generally not recommended to draw outside of the WM_PAINT/WM_ERASEBKGND handlers. When you draw outside of WM_PAINT you need to invalidate the window rectangle/region so that the subsystem knows to redraw it. InvalidateRect function[^] InvalidateRgn function[^] Best Wishes, -David Delaune

            E Offline
            E Offline
            econy
            wrote on last edited by
            #5

            I wonder if WM_PAINT message handler could display messages immediately. or even I draw it in a member function, it would refresh in OnPaint() executed?

            L 1 Reply Last reply
            0
            • E econy

              I wonder if WM_PAINT message handler could display messages immediately. or even I draw it in a member function, it would refresh in OnPaint() executed?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              econy wrote:

              I wonder if WM_PAINT message handler could display messages immediately. or even I draw it in a member function, it would refresh in OnPaint() executed?

              No. There is no guarantee that when your WM_PAINT message fires that the subsystem will redraw window contents unless the rectangle/region/window is marked invalid. This is an OS optimization to prevent excessive redrawing. (However with DWM enabled[^] the rules do change a bit) Read all of this so that in the future you can help others: Painting and Drawing[^] Best Wishes, -David Delaune

              E 1 Reply Last reply
              0
              • L Lost User

                econy wrote:

                I wonder if WM_PAINT message handler could display messages immediately. or even I draw it in a member function, it would refresh in OnPaint() executed?

                No. There is no guarantee that when your WM_PAINT message fires that the subsystem will redraw window contents unless the rectangle/region/window is marked invalid. This is an OS optimization to prevent excessive redrawing. (However with DWM enabled[^] the rules do change a bit) Read all of this so that in the future you can help others: Painting and Drawing[^] Best Wishes, -David Delaune

                E Offline
                E Offline
                econy
                wrote on last edited by
                #7

                Thanks

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups