Solved

CStatic class varibales used for the create() function.

Posted on 2004-08-26
13
398 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
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 55
Get filename and folder into excel 7 74
List out all word 7 291
Making an alias 7 95
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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

770 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