Solved

Change CPen attributes

Posted on 1998-10-06
7
508 Views
Last Modified: 2013-11-20
How can I change the attributes of my CPen object without creating a new pen?
The reason for this is, I have a global CPen object:
       CPen  gPen(2,1,RGB(255,0,0));
Many places in my application requires me to change the pen style & color and select the same pen? How can I change the style & color for gPen?
I do not want to create a new pen because that defeat my purpose to have just one global pen.  Thanks much.
0
Comment
Question by:tpnguyen
7 Comments
 
LVL 8

Expert Comment

by:MikeP090797
Comment Utility
I'm afraid this is simply impossible. You can specify it's attributes only upon creation. I don't see how this interferes with making it a global object. You can use CPen::Create to recreate the pen with other attributes when you need to change it
0
 

Author Comment

by:tpnguyen
Comment Utility
Hi Mike,

Let say I have a generic viewport class that has this member function to change the pen.
Below is a simple example to illustrate what I want to do (note that, style can be different type other than int, but since this is good to simplify the case).   mypDC is a CDC pointer and a protected member of class Viewport, and I already successful make it stay around for entire my application.  

void MyViewport::ChangePen (int  style) {
    switch (style) {
         case VPT_SOLID:
                 CPen pen(SOLID_LINE, 1, RGB(255,0,0);
                 mypDC->SelectObject(&pen);                
         case VPT_DOTDOT:
                // select another pen
         case VPT DASH:
               // select another pen
         default
                 break;
}

As soon as I leave this function,  CPen object go away, and I can not draw.  If I want to make my pen stay around,  I need to make it global (a protected member of my Viewport class).   If if I do that, how can I change its attributes?  I hope this may clear what my my intention was.   Thanks for your inputs.
0
 

Author Comment

by:tpnguyen
Comment Utility
Hi Mike,

It is me again.   The more I think about it.  I think you're probably right.  It may not be possible.    I think I may have found a way to design my class differently.   Anyway, thanks for your inputs.
ps:  I am stil fairly new to this system.  How can I close this one (I mean accepting it)?  You get 50 points.
Thanks,
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:thresher_shark
Comment Utility
Wait for Mike to re-post an "answer."  You can then grade the answer by selecting an option from the list, something like

A: Excellent
B: Very Good
C: Average...

That sort of thing.  Just pick the grade you think he deserves.
0
 
LVL 8

Accepted Solution

by:
MikeP090797 earned 50 total points
Comment Utility
I'm answering the question, just give me the mark you want, and accept the question
0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
Why don't you declare a member variable to your class with a pointer to the active pen:

// <yourfile.h>

class xxx {
 ...
 CPen *m_pCurrentPen;
 ...
}


// yourfile.cpp

void MyViewport::ChangePen (int  style) {

          if(m_pCurrentPen!=NULL)
                    delete m_pCurrentPen;

          switch (style) {
               case VPT_SOLID:
                       m_pCurrentPen=new CPen(PS_SOLID, 1, RGB(255,0,0);
                       mypDC->SelectObject(m_pCurrentPen);
                       break;
               case VPT_DOTDOT:
                      // select another pen
               case VPT DASH:
                     // select another pen
               default
                       break;
      }


Of course, you'd have to initialize m_pCurrentPen to NULL in the constructor of your class
and delete it if it is not NULL in the destructor.

BTW: (quote from online docs)
"An application should not call DeleteDC if objects have been selected into the device context. Objects must first be selected out of the device context before it it is deleted. "
(end quote)

So you should first save the pen which was initially selected for your DC, store it somewhere,
and before destroying you DC select this pen again.
Otherwise you may have some GPF's.
0
 

Author Comment

by:tpnguyen
Comment Utility
Thank you very much.  It really helped!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now