단일 문서에 다중 뷰 추가

SDI 화면에서 여러개의 View를 생성하기

MFC(Microsoft Foundation Class) 라이브러리로 만든 SDI(단일 문서 인터페이스) 응용 프로그램에서 각 문서 형식은 단일 뷰 형식과 연결되어 있습니다. 일부 경우 문서의 현재 뷰를 새 뷰로 전환하는 기능을 설정하는 것이 좋습니다.

   단일 문서의 다중 뷰 구현에 대한 추가 절차는 CDocument::AddView와 MFC 샘플 ENROLLCOLLECT를 참조하십시오.

이 기능은 CView에서 파생된 새 클래스와 뷰를 동적으로 전환하는 추가 코드를 기존 MFC 응용 프로그램에 추가하여 구현할 수 있습니다.

이 기능을 구현하는 단계는 다음과 같습니다.

이 항목의 나머지 부분에서는 다음과 같이 가정합니다.

  • CWinApp에서 파생된 개체 이름은 CMyWinApp이고 CMyWinApp는 MYWINAPP.H와 MYWINAPP.CPP에서 선언 및 정의합니다.
  • CNewViewCView에서 파생된 새 개체의 이름이고 CNewView는 NEWVIEW.H 및 NEWVIEW.CPP에서 선언 및 정의합니다.

기존 응용 프로그램 클래스 수정

뷰 간에 전환하는 응용 프로그램에서 뷰와 뷰 전환 메서드를 저장하려면 멤버 변수를 추가하여 응용 프로그램 클래스를 수정해야 합니다.

MYWINAPP.H의 CMyWinApp 선언에 다음 코드를 추가합니다.

CView* m_pOldView;
CView* m_pNewView;
CView* SwitchView( );

새 멤버 변수 m_pOldViewm_pNewView는 현재 뷰와 새로 만든 뷰를 가리킵니다. 새 메서드(SwitchView)는 사용자가 요청할 때 뷰를 전환합니다. 메서드 본문은 이 항목 뒤 부분에 나오는 전환 함수 구현에서 설명합니다.

응용 프로그램을 마지막으로 수정할 때 전환 함수에 사용된 Windows 메시지(WM_INITIALUPDATE)를 정의하는 새 헤더 파일이 포함되어야 합니다.

MYWINAPP.CPP의 include 섹션에 다음 줄을 삽입합니다.

#include <AFXPRIV.H>

변경 내용을 저장하고 다음 단계를 계속합니다.

새 뷰 클래스 만들기 및 수정

클래스 뷰의 New Class 명령을 사용하면 편리하게 새 뷰 클래스를 만들 수 있습니다. 이 클래스에 대한 유일한 요구 사항은 CView에서 파생되어야 한다는 것입니다. 이 새로운 클래스를 응용 프로그램에 추가합니다. 프로젝트에 새 클래스를 추가하는 방법에 대한 자세한 내용은 클래스 추가를 참조하십시오.

프로젝트에 클래스를 추가한 후 일부 뷰 클래스 멤버의 액세스 가능성을 변경해야 합니다.

생성자와 소멸자에 대한 액세스 지정자를 protected에서 public로 변경하여 NEWVIEW.H를 수정합니다. 이렇게 하면 클래스가 동적으로 생성 및 소멸되고 뷰가 표시되기 전에 뷰의 모양을 수정할 수 있습니다.

변경 내용을 저장하고 다음 단계를 계속합니다.

새 뷰 만들기 및 연결

새 뷰를 만들고 연결하려면 응용 프로그램 클래스의 InitInstance 함수를 수정해야 합니다. 이 함수를 수정하면 새 뷰 개체를 만드는 새 코드를 추가한 다음 두 개의 기존 뷰 개체를 사용하여 m_pOldViewm_pNewView 모두를 초기화합니다.

새 뷰는 InitInstance 함수 내에 만들어지므로 새 뷰와 기존 뷰는 응용 프로그램이 소멸될 때까지 지속됩니다. 그러나, 응용 프로그램에서는 간단하게 새 뷰를 동적으로 만들 수 있습니다.

ProcessShellCommand를 호출한 후 다음 코드를 삽입합니다.

...
CView* pActiveView = ((CFrameWnd*) m_pMainWnd)->GetActiveView();
m_pOldView = pActiveView;
m_pNewView = (CView*) new CNewView;
CDocument* pCurrentDoc = ((CFrameWnd*)m_pMainWnd)->GetActiveDocument();
// Initialize a CCreateContext to point to the active document.
// With this context, the new view is added to the document
// when the view is created in CView::OnCreate().
CCreateContext newContext;
newContext.m_pNewViewClass = NULL;
newContext.m_pNewDocTemplate = NULL;
newContext.m_pLastView = NULL;
newContext.m_pCurrentFrame = NULL;
newContext.m_pCurrentDoc = pCurrentDoc;
// The ID of the initial active view is AFX_IDW_PANE_FIRST.
// Incrementing this value by one for additional views works
// in the standard document/view case but the technique cannot
// be extended for the CSplitterWnd case.
UINT viewID = AFX_IDW_PANE_FIRST + 1;
CRect rect(0, 0, 0, 0); // Gets resized later.
// Create the new view. In this example, the view persists for
// the life of the application. The application automatically
// deletes the view when the application is closed.
m_pNewView->Create(NULL, "AnyWindowName", WS_CHILD, rect, m_pMainWnd, viewID, &newContext);
// When a document template creates a view, the WM_INITIALUPDATE
// message is sent automatically. However, this code must
// explicitly send the message, as follows.
m_pNewView->SendMessage(WM_INITIALUPDATE, 0, 0);
...

변경 내용을 저장하고 다음 단계를 계속합니다.

전환 함수 구현

이전 단계에서 새 뷰 개체를 만들고 초기화하는 코드를 추가하였습니다. 마지막 작업은 전환 메서드 SwitchView를 구현하는 것입니다.

응용 프로그램 클래스의 구현 파일(MYWINAPP.CPP) 끝에 다음 메서드 정의를 추가합니다.

CView* CMyWinApp::SwitchView( )
{
   CView* pActiveView =
      ((CFrameWnd*) m_pMainWnd)->GetActiveView();
   CView* pNewView= NULL;
   if(pActiveView == m_pOldView)
      pNewView= m_pNewView;
   else
      pNewView= m_pOldView;
   // Exchange view window IDs so RecalcLayout() works.
   #ifndef _WIN32
   UINT temp = ::GetWindowWord(pActiveView->m_hWnd, GWW_ID);
   ::SetWindowWord(pActiveView->m_hWnd, GWW_ID, ::GetWindowWord(pNewView->m_hWnd, GWW_ID));
   ::SetWindowWord(pNewView->m_hWnd, GWW_ID, temp);
   #else
   UINT temp = ::GetWindowLong(pActiveView->m_hWnd, GWL_ID);
   ::SetWindowLong(pActiveView->m_hWnd, GWL_ID, ::GetWindowLong(pNewView->m_hWnd, GWL_ID));
   ::SetWindowLong(pNewView->m_hWnd, GWL_ID, temp);
   #endif
   pActiveView->ShowWindow(SW_HIDE);
   pNewView->ShowWindow(SW_SHOW);
   ((CFrameWnd*) m_pMainWnd)->SetActiveView(pNewView);
   ((CFrameWnd*) m_pMainWnd)->RecalcLayout();
   pNewView->Invalidate();
   return pActiveView;
}

변경 내용을 저장하고 다음 단계를 계속합니다.

“단일 문서에 다중 뷰 추가”의 58개의 댓글

  1. Awesome blog! Do you have any suggestions
    for aspiring writers? I’m hoping to start my own blog soon but I’m a little lost on everything.
    Would you advise starting with a free platform like WordPress or
    go for a paid option? There are so many options out there that I’m completely confused ..
    Any tips? Bless you!

  2. I simply want to mention I’m newbie to blogs and definitely liked you’re web blog. Likely I’m want to bookmark your blog . You actually have terrific articles and reviews. With thanks for sharing with us your blog.

  3. I simply want to tell you that I am just very new to blogging and site-building and actually enjoyed your website. Likely I’m likely to bookmark your blog . You surely come with great stories. Appreciate it for sharing your blog.

  4. Thanks for enabling me to acquire new ideas about pcs. I also have belief that one of the best ways to maintain your mobile computer in primary condition is a hard plastic case, or maybe shell, that will fit over the top of the computer. These kinds of protective gear will be model specific since they are made to fit perfectly across the natural outer shell. You can buy these directly from the seller, or via third party places if they are intended for your notebook, however its not all laptop could have a spend on the market. All over again, thanks for your suggestions.

  5. Thanks for your suggestions. One thing we’ve noticed is banks along with financial institutions know the dimensions and spending routines of consumers and also understand that most of the people max out and about their credit cards around the vacations. They wisely take advantage of this fact and begin flooding your current inbox as well as snail-mail box by using hundreds of 0 APR credit cards offers just after the holiday season comes to an end. Knowing that in case you are like 98 of all American community, you’ll rush at the possiblity to consolidate credit debt and switch balances for 0 interest rates credit cards.

  6. Hey would you mind sharing which blog platform you’re working with? I’m going to start my own blog in the near future but I’m having a difficult time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something unique. P.S Sorry for being off-topic but I had to ask!

  7. I’m curious to find out what blog platform you happen to be working with? I’m having some small security problems with my latest site and I would like to find something more risk-free. Do you have any recommendations?

  8. I simply want to say I am new to blogging and site-building and truly enjoyed your web site. Very likely I’m want to bookmark your blog post . You actually come with really good well written articles. Thanks for sharing with us your website.

  9. I was just seeking this info for some time. After six hours of continuous Googleing, at last I got it in your site. I wonder what is the lack of Google strategy that don’t rank this kind of informative sites in top of the list. Usually the top websites are full of garbage.

  10. Greetings! I know this is kinda off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having problems finding one? Thanks a lot!

  11. This is the proper weblog for anyone who wants to search out out about this topic. You notice a lot its nearly arduous to argue with you (not that I really would need…HaHa). You undoubtedly put a brand new spin on a topic thats been written about for years. Nice stuff, just nice!

  12. I intended to compose you one very little observation in order to thank you yet again for all the beautiful basics you have provided above. This has been certainly generous with people like you to supply extensively what numerous people could have offered for an e-book to earn some bucks for themselves, particularly since you could have tried it if you ever desired. Those suggestions additionally served as the fantastic way to fully grasp that other people have a similar zeal much like my own to know the truth many more pertaining to this matter. I know there are numerous more fun sessions ahead for many who check out your blog.

  13. The luxury proposed might be incomparable; citizens are never fail to looking for bags is a Native goals. The idea numerous insert goals uniquely to push diversity with visibility during the travel and leisure arena. Hotels Discounts

  14. I’m more than happy to discover this site. I need to to thank you for your time due to this wonderful read!! I definitely enjoyed every bit of it and I have you saved to fav to see new stuff on your site.

  15. I’m amazed, I have to admit. Rarely do I encounter a blog that’s both educative and engaging, and let me tell you, you’ve hit the nail on the head. The problem is an issue that not enough men and women are speaking intelligently about. I’m very happy I came across this during my search for something relating to this.

댓글 남기기