Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Change CPen attributes

Posted on 1998-10-06
7
Medium Priority
?
516 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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 150 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

721 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