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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using DC in added class (outside CView class)

Using DC in added class (outside CView class)

Scheduled Pinned Locked Moved C / C++ / MFC
c++helplearning
7 Posts 3 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.
  • A Offline
    A Offline
    AglaiaMasaki
    wrote on last edited by
    #1

    Hello. I can use CClientDC and its TextOut function in C~~View class. It's quite simple like this:

    void C~~View::OnLButtonDown(UINT nFlags, CPoint point)
    {
    CClientDC dc(this);
    dc.TextOut(x, y, "..");
    }

    Yet when I use this function in my 'added' class, which is not derived class of CView, an error occurs. like this:

    void CMyclass::memfunc()
    {
    CClientDC dc(this);
    dc.TextOut(x, y, "..");
    }

    So I changed the code like this:

    \[C~~View.cpp\]
    

    void C~~View::OnLButtonDown(UINT nFlags, CPoint point)
    {
    CClientDC dc(this);
    CMyclass myclass;
    myclass.memfunc(dc);
    }
    [CMyclass.cpp]
    void CMyclass::memfunc(CClientDC dc)
    {
    dc.TextOut(x, y, "..");
    }

    And errors still occur. Of course, I linked the header file of my class to C~~View.cpp. I want to display Text or Images on window with code in 'my class'. Please give me advice or ideas. Thank you.

    May the sky bring you a full measure of health and prosperity.

    CPalliniC C 2 Replies Last reply
    0
    • A AglaiaMasaki

      Hello. I can use CClientDC and its TextOut function in C~~View class. It's quite simple like this:

      void C~~View::OnLButtonDown(UINT nFlags, CPoint point)
      {
      CClientDC dc(this);
      dc.TextOut(x, y, "..");
      }

      Yet when I use this function in my 'added' class, which is not derived class of CView, an error occurs. like this:

      void CMyclass::memfunc()
      {
      CClientDC dc(this);
      dc.TextOut(x, y, "..");
      }

      So I changed the code like this:

      \[C~~View.cpp\]
      

      void C~~View::OnLButtonDown(UINT nFlags, CPoint point)
      {
      CClientDC dc(this);
      CMyclass myclass;
      myclass.memfunc(dc);
      }
      [CMyclass.cpp]
      void CMyclass::memfunc(CClientDC dc)
      {
      dc.TextOut(x, y, "..");
      }

      And errors still occur. Of course, I linked the header file of my class to C~~View.cpp. I want to display Text or Images on window with code in 'my class'. Please give me advice or ideas. Thank you.

      May the sky bring you a full measure of health and prosperity.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      You should pass the device context by reference, i.e. change your function definition (remember to change the declaration too):

      void CMyclass::memfunc(CClientDC & dc)
      {
      dc.TextOut(x, y, "..");
      }

      :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      A 1 Reply Last reply
      0
      • A AglaiaMasaki

        Hello. I can use CClientDC and its TextOut function in C~~View class. It's quite simple like this:

        void C~~View::OnLButtonDown(UINT nFlags, CPoint point)
        {
        CClientDC dc(this);
        dc.TextOut(x, y, "..");
        }

        Yet when I use this function in my 'added' class, which is not derived class of CView, an error occurs. like this:

        void CMyclass::memfunc()
        {
        CClientDC dc(this);
        dc.TextOut(x, y, "..");
        }

        So I changed the code like this:

        \[C~~View.cpp\]
        

        void C~~View::OnLButtonDown(UINT nFlags, CPoint point)
        {
        CClientDC dc(this);
        CMyclass myclass;
        myclass.memfunc(dc);
        }
        [CMyclass.cpp]
        void CMyclass::memfunc(CClientDC dc)
        {
        dc.TextOut(x, y, "..");
        }

        And errors still occur. Of course, I linked the header file of my class to C~~View.cpp. I want to display Text or Images on window with code in 'my class'. Please give me advice or ideas. Thank you.

        May the sky bring you a full measure of health and prosperity.

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        If you check the documentation you see that CClientDC's constructor[^] gets a CWnd (or derivate) pointer, my guess would be that your own class has nothing to do with CWnd. Explain a bit more what you want to do.

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

        modified on Monday, February 23, 2009 8:59 AM

        A 1 Reply Last reply
        0
        • CPalliniC CPallini

          You should pass the device context by reference, i.e. change your function definition (remember to change the declaration too):

          void CMyclass::memfunc(CClientDC & dc)
          {
          dc.TextOut(x, y, "..");
          }

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          A Offline
          A Offline
          AglaiaMasaki
          wrote on last edited by
          #4

          Thank you so much! You saved me! :)

          May the sky bring you a full measure of health and prosperity.

          CPalliniC 1 Reply Last reply
          0
          • A AglaiaMasaki

            Thank you so much! You saved me! :)

            May the sky bring you a full measure of health and prosperity.

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            You are welcome. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • C Code o mat

              If you check the documentation you see that CClientDC's constructor[^] gets a CWnd (or derivate) pointer, my guess would be that your own class has nothing to do with CWnd. Explain a bit more what you want to do.

              > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

              modified on Monday, February 23, 2009 8:59 AM

              A Offline
              A Offline
              AglaiaMasaki
              wrote on last edited by
              #6

              What I meant was : In MFC single document, I want to show Text on main window. And I also want this function to be operated in my 'own' class(which is a derived class of CWnd), not in CView class. Pallini's answer is the best solution to my question (how nice of him/her :)). It works well. And now I've found another solution.

              void CMyclass::memfunc()
              {
              AfxGetApp()->m_pMainWnd->GetDC()->TextOut(x, y, "..");
              }

              Thank you for your kind answer.

              May the sky bring you a full measure of health and prosperity.

              C 1 Reply Last reply
              0
              • A AglaiaMasaki

                What I meant was : In MFC single document, I want to show Text on main window. And I also want this function to be operated in my 'own' class(which is a derived class of CWnd), not in CView class. Pallini's answer is the best solution to my question (how nice of him/her :)). It works well. And now I've found another solution.

                void CMyclass::memfunc()
                {
                AfxGetApp()->m_pMainWnd->GetDC()->TextOut(x, y, "..");
                }

                Thank you for your kind answer.

                May the sky bring you a full measure of health and prosperity.

                C Offline
                C Offline
                Code o mat
                wrote on last edited by
                #7

                I see. Sorry if i misunderstood anything, good luck with your project. :)

                > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

                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