Solved

Change CPen attributes

Posted on 1998-10-06
7
514 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1323035
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
ID: 1323036
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
ID: 1323037
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:thresher_shark
ID: 1323038
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
ID: 1323039
I'm answering the question, just give me the mark you want, and accept the question
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1323040
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
ID: 1323041
Thank you very much.  It really helped!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 137
Shell script to login to remote m/c and execute commands 10 81
mapAB Challlenge 35 186
Excel file not created as expected 7 111
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

759 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