How to create a window in general?
-
Greetings: I am trying to figure out how to create a very general window in MFC out of thin air. Without getting into the grim details, I have a simple MFC application - no Doc/View. When the app launches, a naked CMainFrame is displayed with no child window over it. It is colored with COLOR_APPWORKSPACE. This is the way we wanted it for a variety of reasons. Now, there is a menu item that must create a very simple window - like a minimal CView - and displays it in the Main Frame as a child window. I do NOT want to maximize this new window to fit the entire client area of the main frame. I would like the new window to have boarders and a caption and the option to maximize but not much else. I cannot seem to make this happen under MFC. Can anybody help? Thank you in advance. Mark
-
Greetings: I am trying to figure out how to create a very general window in MFC out of thin air. Without getting into the grim details, I have a simple MFC application - no Doc/View. When the app launches, a naked CMainFrame is displayed with no child window over it. It is colored with COLOR_APPWORKSPACE. This is the way we wanted it for a variety of reasons. Now, there is a menu item that must create a very simple window - like a minimal CView - and displays it in the Main Frame as a child window. I do NOT want to maximize this new window to fit the entire client area of the main frame. I would like the new window to have boarders and a caption and the option to maximize but not much else. I cannot seem to make this happen under MFC. Can anybody help? Thank you in advance. Mark
MFC windows are created in two steps - one for the C++ CWnd object and one for the Windows HWND.
// step 1
CWnd *pWnd = new CWnd();
// step 2
pWnd->Create(NULL, _T("Window Name"), WS_CHILD|WS_VISIBLE|WS_OVERLAPPEDWINDOW, CRect(10,10,100,100),
pParentWnd, ID_CHILDWND, NULL);If you want the window to do something useful you'll probably want to derive your own window class from CWnd. If you want to use extended window styles you can use the CreateEx() method. Destruction of windows is generally two steps as well (CFrameWnd-derived windows are an exception).
// step 1
pWnd->DestroyWindow();
// step 2
delete pWnd;-- modified at 17:48 Thursday 28th December, 2006 Changed the WS_ styles.
-
Greetings: I am trying to figure out how to create a very general window in MFC out of thin air. Without getting into the grim details, I have a simple MFC application - no Doc/View. When the app launches, a naked CMainFrame is displayed with no child window over it. It is colored with COLOR_APPWORKSPACE. This is the way we wanted it for a variety of reasons. Now, there is a menu item that must create a very simple window - like a minimal CView - and displays it in the Main Frame as a child window. I do NOT want to maximize this new window to fit the entire client area of the main frame. I would like the new window to have boarders and a caption and the option to maximize but not much else. I cannot seem to make this happen under MFC. Can anybody help? Thank you in advance. Mark
Try this style in the sample code I posted (I changed it there as well) in the Create() call: WS_CHILD|WS_VISIBLE|WS_OVERLAPPEDWINDOW This will help you see the window better :-D
-
Try this style in the sample code I posted (I changed it there as well) in the Create() call: WS_CHILD|WS_VISIBLE|WS_OVERLAPPEDWINDOW This will help you see the window better :-D
-
Hi Mark: Thanks for your response. Its working except the window is displaying without a caption. I'm getting only a rectangular frame with no caption and no way to move it around. I AM able to resize it though... Mark
Even with the WS_OVERLAPPEDWINDOW style added? Can you post the line(s) of code calling Create()? Mark
-
Even with the WS_OVERLAPPEDWINDOW style added? Can you post the line(s) of code calling Create()? Mark
Hi Mark: Sure, here it is:
CRuntimeClass* pRTC; pRTC = RUNTIME_CLASS( COneView ); pWindow = (CWnd *)pRTC->CreateObject( ) ; pWindow->Create( (LPCTSTR)m_strTypeOneView, TEXT("One Little View"), WS_CHILD | WS_VISIBLE | WS_OVERLAPPEDWINDOW, CRect(0, 0, 400, 300), this, 4567 );
The "mstrTypeOneView" string contains the result returned from an earlier call to AfxRegisterWndClass:try { m_strTypeOneView = AfxRegisterWndClass( 0, ::LoadCursor(NULL, IDC_CROSS), (HBRUSH)reinterpret_cast(COLOR_BACKGROUND+1), ::LoadIcon(NULL, IDI_APPLICATION)); } catch (CResourceException* pEx) { AfxMessageBox(_T("Couldn't register class! (Already registered?)")); pEx->Delete(); }
I'm sure that the window registration is working because I have made alterations to the background color and the ICON and the window responds as expected. Thanks, Mark -
Hi Mark: Sure, here it is:
CRuntimeClass* pRTC; pRTC = RUNTIME_CLASS( COneView ); pWindow = (CWnd *)pRTC->CreateObject( ) ; pWindow->Create( (LPCTSTR)m_strTypeOneView, TEXT("One Little View"), WS_CHILD | WS_VISIBLE | WS_OVERLAPPEDWINDOW, CRect(0, 0, 400, 300), this, 4567 );
The "mstrTypeOneView" string contains the result returned from an earlier call to AfxRegisterWndClass:try { m_strTypeOneView = AfxRegisterWndClass( 0, ::LoadCursor(NULL, IDC_CROSS), (HBRUSH)reinterpret_cast(COLOR_BACKGROUND+1), ::LoadIcon(NULL, IDI_APPLICATION)); } catch (CResourceException* pEx) { AfxMessageBox(_T("Couldn't register class! (Already registered?)")); pEx->Delete(); }
I'm sure that the window registration is working because I have made alterations to the background color and the ICON and the window responds as expected. Thanks, MarkJethro63 wrote:
The "mstrTypeOneView" string contains the result returned from an earlier call to AfxRegisterWndClass:
How much earlier. It's not safe to store the returned pointer. It should be copied into your own buffer (a CString is easiest). The title bar isn't hidden under a toolbar is it? If so, adjust the CRect top and left params. Mark
-
Jethro63 wrote:
The "mstrTypeOneView" string contains the result returned from an earlier call to AfxRegisterWndClass:
How much earlier. It's not safe to store the returned pointer. It should be copied into your own buffer (a CString is easiest). The title bar isn't hidden under a toolbar is it? If so, adjust the CRect top and left params. Mark
Hi Mark: "m_strTypeOneView" is a CString member of CMainFrame. The call to AfxRegisterWindClass is carried out in CMainFrame::OnCreate. The string returned is copied to "m_strTypeOneView" at that point. So, I believe that the returned class name is protected and not volatile. I thought of your second point shortly after I sent my last reply to you and moved the upper-left coordinates of the window to 40,40. The window moves but does not change in appearance. Mark
-
Hi Mark: "m_strTypeOneView" is a CString member of CMainFrame. The call to AfxRegisterWindClass is carried out in CMainFrame::OnCreate. The string returned is copied to "m_strTypeOneView" at that point. So, I believe that the returned class name is protected and not volatile. I thought of your second point shortly after I sent my last reply to you and moved the upper-left coordinates of the window to 40,40. The window moves but does not change in appearance. Mark
Jethro63 wrote:
"m_strTypeOneView" is a CString member of CMainFrame. The call to AfxRegisterWindClass is carried out in CMainFrame::OnCreate. The string returned is copied to "m_strTypeOneView" at that point. So, I believe that the returned class name is protected and not volatile.
:doh: Cool sorry about that :)
Jethro63 wrote:
I thought of your second point shortly after I sent my last reply to you and moved the upper-left coordinates of the window to 40,40. The window moves but does not change in appearance.
Hmm That's just not cool! What if you create it using the default class (pass NULL for 1st param of Create())? In my little MFC tester app (that I use to test code I post here) I have this: pWnd->Create(NULL, _T("Window Name"), WS_CHILD|WS_VISIBLE|WS_OVERLAPPEDWINDOW, CRect(50,50,300,200), this, 12345, NULL); And it creates as intended - caption, system menu, max/minimize buttons, etc.
-
Hi Mark: "m_strTypeOneView" is a CString member of CMainFrame. The call to AfxRegisterWindClass is carried out in CMainFrame::OnCreate. The string returned is copied to "m_strTypeOneView" at that point. So, I believe that the returned class name is protected and not volatile. I thought of your second point shortly after I sent my last reply to you and moved the upper-left coordinates of the window to 40,40. The window moves but does not change in appearance. Mark
CRuntimeClass* pRTC; pRTC = RUNTIME_CLASS( COneView ); pWindow = (CWnd *)pRTC->CreateObject( ) ; I haven't looked at the MFC source code in a while but if COneView is CView-derived then maybe some style flags are getting stripped off by MFC since views are meant to be used in a frame.
-
CRuntimeClass* pRTC; pRTC = RUNTIME_CLASS( COneView ); pWindow = (CWnd *)pRTC->CreateObject( ) ; I haven't looked at the MFC source code in a while but if COneView is CView-derived then maybe some style flags are getting stripped off by MFC since views are meant to be used in a frame.
I think you might be right about that and I am going to quickly try to create a different view derived directly from CWnd. Unfortunately, I have to go and put my son to bed now, so I will not be able to continue this thread this evening (unless much later). Thanks again for your advice. I will let you know what happens next. Mark
-
I think you might be right about that and I am going to quickly try to create a different view derived directly from CWnd. Unfortunately, I have to go and put my son to bed now, so I will not be able to continue this thread this evening (unless much later). Thanks again for your advice. I will let you know what happens next. Mark
Jethro63 wrote:
I am going to quickly try to create a different view derived directly from CWnd.
You may like to derive it from
CView
instead ofCWnd
.Prasad Notifier using ATL | Operator new[],delete[][^]
-
Jethro63 wrote:
I am going to quickly try to create a different view derived directly from CWnd.
You may like to derive it from
CView
instead ofCWnd
.Prasad Notifier using ATL | Operator new[],delete[][^]
Hi Prasad. Thank you for your response. The original window that I am having trouble creating was derived from CView. The suggestion that Mark Salsbery is making is that this could be the problem. I am going to try creating a window derived from CWnd, just to see what happens. Cheers, Mark
-
Hi Prasad. Thank you for your response. The original window that I am having trouble creating was derived from CView. The suggestion that Mark Salsbery is making is that this could be the problem. I am going to try creating a window derived from CWnd, just to see what happens. Cheers, Mark
Jethro63 wrote:
The suggestion that Mark Salsbery is making is that this could be the problem
Mark was refering to certain styles like
WS_OVERLAPPEDWINDOW
, would be inappropriate for view class.Prasad Notifier using ATL | Operator new[],delete[][^]
-
Hi Prasad. Thank you for your response. The original window that I am having trouble creating was derived from CView. The suggestion that Mark Salsbery is making is that this could be the problem. I am going to try creating a window derived from CWnd, just to see what happens. Cheers, Mark
Probably, this is last post this year. Its friday 11:37 PM here. Can continue on your problem on tuesday. Happy new year.:)
Prasad Notifier using ATL | Operator new[],delete[][^]