Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to create a window in general?

How to create a window in general?

Scheduled Pinned Locked Moved C / C++ / MFC
c++adobehelptutorialquestion
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jethro63
    wrote on last edited by
    #1

    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

    M 2 Replies Last reply
    0
    • J Jethro63

      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

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • J Jethro63

        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

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • M Mark Salsbery

          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

          J Offline
          J Offline
          Jethro63
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • J Jethro63

            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

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            Even with the WS_OVERLAPPEDWINDOW style added? Can you post the line(s) of code calling Create()? Mark

            J 1 Reply Last reply
            0
            • M Mark Salsbery

              Even with the WS_OVERLAPPEDWINDOW style added? Can you post the line(s) of code calling Create()? Mark

              J Offline
              J Offline
              Jethro63
              wrote on last edited by
              #6

              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

              M 1 Reply Last reply
              0
              • J Jethro63

                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

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                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

                J 1 Reply Last reply
                0
                • M Mark Salsbery

                  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

                  J Offline
                  J Offline
                  Jethro63
                  wrote on last edited by
                  #8

                  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

                  M 2 Replies Last reply
                  0
                  • J Jethro63

                    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

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • J Jethro63

                      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

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #10

                      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.

                      J 1 Reply Last reply
                      0
                      • M Mark Salsbery

                        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.

                        J Offline
                        J Offline
                        Jethro63
                        wrote on last edited by
                        #11

                        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

                        P 1 Reply Last reply
                        0
                        • J Jethro63

                          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

                          P Offline
                          P Offline
                          prasad_som
                          wrote on last edited by
                          #12

                          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 of CWnd.

                          Prasad Notifier using ATL | Operator new[],delete[][^]

                          J 1 Reply Last reply
                          0
                          • P prasad_som

                            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 of CWnd.

                            Prasad Notifier using ATL | Operator new[],delete[][^]

                            J Offline
                            J Offline
                            Jethro63
                            wrote on last edited by
                            #13

                            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

                            P 2 Replies Last reply
                            0
                            • J Jethro63

                              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

                              P Offline
                              P Offline
                              prasad_som
                              wrote on last edited by
                              #14

                              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[][^]

                              1 Reply Last reply
                              0
                              • J Jethro63

                                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

                                P Offline
                                P Offline
                                prasad_som
                                wrote on last edited by
                                #15

                                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[][^]

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups