Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CStatic class varibales used for the create() function.

Posted on 2004-08-26
13
Medium Priority
?
409 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 2000 total points
ID: 11901760
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
ID: 11901833
You need also to call

ms->ShowWindow(SW_SHOWNORMAL);
0
 

Author Comment

by:hansw77041
ID: 11901843
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
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!

 
LVL 16

Expert Comment

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

Expert Comment

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

Expert Comment

by:carribus
ID: 11901862
ShowWindow(SW_SHOWNORMAL); will have no effect, since he is already passing the WS_VISIBLE style to the creation of the window.
0
 

Author Comment

by:hansw77041
ID: 11901899
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
ID: 11901933
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
ID: 11901959
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
ID: 11901961
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
ID: 11904719
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
ID: 11910343
Thank you :)
0

Featured Post

Independent Software Vendors: 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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Screencast - Getting to Know the Pipeline
Suggested Courses

972 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