Solved

CStatic class varibales used for the create() function.

Posted on 2004-08-26
13
401 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
[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
  • 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
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!

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 54
maxBlock challenge 30 151
method notes when mouse over in eclipse 5 96
withoutTen challenge 14 142
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

752 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