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. Keeping track of fonts, pens, device contexts, etc.

Keeping track of fonts, pens, device contexts, etc.

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
4 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
    Anthony Appleyard
    wrote on last edited by
    #1

    I wrote a program which includes these declarations:-

    class PEN{public: HDC d; HPEN pen,old;
    PEN(HDC dc,COLORREF col);
    ~PEN();};

    /*-----*/ /* the device context dc must still exist when the pen is deleted */

    PEN::PEN(HDC dc,COLORREF col){ // constructor
    d=dc;
    pen=(HPEN)CreatePen(0,1,col);
    old=(HPEN)SelectObject(dc,pen);};
    PEN::~PEN(){ // destructor
    SelectObject(d,old);
    DeleteObject(pen);};

    /*-----*/ // then I can write for example:- /*-----*/

    if(xhairs) {
    PEN orange(WIdc,RGB(255,128,0)); // crosshairs
    Line(WIdc,0,b/2,(int)(2*a),(int)(b/2));
    Line(WIdc,(int)(a/2),0,(int)(a/2),b);
    Line(WIdc,(int)(a*1.5),0,(int)(a*1.5),b);}

    /*-----*/ and the new pen is automatically cleared away when control leaves the block where the pen was declared. And similarly with fonts etc.

    Richard Andrew x64R 1 Reply Last reply
    0
    • A Anthony Appleyard

      I wrote a program which includes these declarations:-

      class PEN{public: HDC d; HPEN pen,old;
      PEN(HDC dc,COLORREF col);
      ~PEN();};

      /*-----*/ /* the device context dc must still exist when the pen is deleted */

      PEN::PEN(HDC dc,COLORREF col){ // constructor
      d=dc;
      pen=(HPEN)CreatePen(0,1,col);
      old=(HPEN)SelectObject(dc,pen);};
      PEN::~PEN(){ // destructor
      SelectObject(d,old);
      DeleteObject(pen);};

      /*-----*/ // then I can write for example:- /*-----*/

      if(xhairs) {
      PEN orange(WIdc,RGB(255,128,0)); // crosshairs
      Line(WIdc,0,b/2,(int)(2*a),(int)(b/2));
      Line(WIdc,(int)(a/2),0,(int)(a/2),b);
      Line(WIdc,(int)(a*1.5),0,(int)(a*1.5),b);}

      /*-----*/ and the new pen is automatically cleared away when control leaves the block where the pen was declared. And similarly with fonts etc.

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      And your question is?

      The difficult we do right away... ...the impossible takes slightly longer.

      A 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        And your question is?

        The difficult we do right away... ...the impossible takes slightly longer.

        A Offline
        A Offline
        Anthony Appleyard
        wrote on last edited by
        #3

        Is this class declaration likely to be any use to any of you?

        L 1 Reply Last reply
        0
        • A Anthony Appleyard

          Is this class declaration likely to be any use to any of you?

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

          Anthony Appleyard wrote:

          Is this class declaration likely to be any use to any of you?

          Not really, the GDI+ Pen class[^] is already available.

          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