Solved

CStatic class varibales used for the create() function.

Posted on 2004-08-26
13
397 Views
Last Modified: 2013-11-20
I made a class based upon CStatic and added a Create() function.
When I used the class like this:

CMyStatic* mS = (CMyStatic*)new CMyStatic;
DWORD dwStyle = WS_CHILD | WS_VISIBLE | SS_BITMAP | SS_CENTERIMAGE;
BOOL bRet =      mS->Create(      NULL,  // LPCTSTR lpszClassName
                                     str,
                                dwStyle,
              CRect(10,10,26,26),
                                       this,
                                1001,
                              NULL );      // CCreateContext* pContext
      
It compiles and runs, however the control is not visible.
I suspect the the problem is the variables  LPCTSTR lpszClassName and CCreateContext* pContext are the problem.

My questions:  what are these variables ?  
What is the lpszClassName and do I need a CCreateContext ?

Thanks.

class CMyStatic : public CStatic
{
// Construction
public:
      CMyStatic();

// Attributes
public:

// Operations
public:

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CMyStatic)
      public:
      virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
      //}}AFX_VIRTUAL

// Implementation
public:
      virtual ~CMyStatic();

      // Generated message map functions
protected:
      //{{AFX_MSG(CMyStatic)
      //}}AFX_MSG

      DECLARE_MESSAGE_MAP()
};
0
Comment
Question by:hansw77041
  • 5
  • 4
  • 3
13 Comments
 
LVL 2

Accepted Solution

by:
carribus earned 500 total points
Comment Utility
Technically, if you are deriving a static class, you don't need to override the Create() function unless you want to handle the specifics of the creation of the window.

CCreateContext is used when MFC creates the view (and associated frame window) that are associated with a specific document - i.e. the MFC Document/View architecture. In your case, you must pass NULL (or 0) as the CCreateContext* parameter.

The lpszClassName is used when you register a new window type. Say for example you are creating a brand new window control, and wish it to be known as a MFC_YourcustomWndControl window class, when you call the non-specialised Create() function (which you are doing in the above code), you can specific the class name of "MFC_YourcustomwndControl" as the class name.

IMHO, you should just remove the Create() function your CMyStatic class and use the one provided in CStatic.

0
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
You need also to call

ms->ShowWindow(SW_SHOWNORMAL);
0
 

Author Comment

by:hansw77041
Comment Utility
Thanks for the info.
H'mm well if I tried NOT using the Create() and the control is not visible.
I also tried adding the class name but that makes the code crash ...

Well you answered my questions, but I'm still not able to make the control visible... any ideas ?

0
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
Well, I think you rushed to accept an answer :o)
0
 
LVL 2

Expert Comment

by:carribus
Comment Utility
could you send the revised declaration of your CMyStatic class please... and the code that creates it...
0
 
LVL 2

Expert Comment

by:carribus
Comment Utility
ShowWindow(SW_SHOWNORMAL); will have no effect, since he is already passing the WS_VISIBLE style to the creation of the window.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:hansw77041
Comment Utility
I tried using ShowWindow() a long time agi, and it does not work.

If I removed the create(), then calling ShowWindow() crashes the code...

this does not work..

CMyStatic* mS = (CMyStatic*)new CMyStatic;
 mS->ShowWindow(SW_NORMAL);


class CMyStatic : public CStatic
{
// Construction
public:
      CMyStatic();

// Attributes
public:

// Operations
public:

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CMyStatic)
      public:
      //}}AFX_VIRTUAL

// Implementation
public:
      virtual ~CMyStatic();

      // Generated message map functions
protected:
      //{{AFX_MSG(CMyStatic)
      //}}AFX_MSG

      DECLARE_MESSAGE_MAP()
};



0
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
CMyStatic* mS = (CMyStatic*)new CMyStatic;
 mS->ShowWindow(SW_NORMAL);

of course it doesn't

ShowWindow requires that a HWND exists.

CMyStatic* mS = (CMyStatic*)new CMyStatic;
BOOL bCreate = mS->Create( str,
                           dwStyle,
            CRect(10,10,26,26), this, 1001
);
if(bCreate)
 mS->ShowWindow(SW_NORMAL);
0
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
Oh, I just saw you have a bitmap (SS_BITMAP)
In this case you need to load a bitmap to be displayed

mS->SetBitmap(::LoadBitmap(GetModuleInstance(0), MAKEINTRESOURCE(IDB_MYBITMAP)));
0
 
LVL 2

Expert Comment

by:carribus
Comment Utility
Ok, now that you've removed your own Create() definition, the CStatic::Create() function should be available to you again. If I remember correctly, it is prototyped as follows:

virtual BOOL Create(LPCTSTR lpszText, DWORD dwStyle, const RECT& rc, CWnd* pParent, UINT nID);

Ok, so in your code, your creation code should look something similar to:

CMyStatic* pMS = new CMyStatic;

// - the CRect parameter merely indicates position and dimensions
// - nSomeID should be a value between 1 and 0xFFFF
pMS->Create("Test Caption", WS_VISIBLE | WS_CHILD, CRect(0,0,100,20), this, nSomeID);

PS. you will only need to call pMS->ShowWindow(SW_SHOW) if you don't specific the WS_VISIBLE style.

Try that out...
0
 

Author Comment

by:hansw77041
Comment Utility
carribus,
Thanks, that did the trick.
Best regards
Hans W
PS wish I has more point to offer...
0
 
LVL 2

Expert Comment

by:carribus
Comment Utility
Thank you :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
configuration management 2 99
repeateFront java challenge 31 87
lucky13 challenge 11 111
twoTwo  challenge 35 86
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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

11 Experts available now in Live!

Get 1:1 Help Now