sampleView.cpp
上传用户:hicheon
上传日期:2007-02-06
资源大小:34k
文件大小:5k
源码类别:

ListView/ListBox

开发平台:

Visual C++

  1. // sampleView.cpp : implementation of the CSampleView class
  2. //
  3. #include "stdafx.h"
  4. #include "sample.h"
  5. #include "sampleDoc.h"
  6. #include "sampleView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CSampleView
  14. IMPLEMENT_DYNCREATE(CSampleView, CView)
  15. BEGIN_MESSAGE_MAP(CSampleView, CView)
  16. //{{AFX_MSG_MAP(CSampleView)
  17. ON_WM_CREATE()
  18. ON_WM_SIZE()
  19. ON_COMMAND(IDC_ONLY_SELECTED_ITEMS, OnOnlySelectedItems)
  20. ON_UPDATE_COMMAND_UI(IDC_ONLY_SELECTED_ITEMS, OnUpdateOnlySelectedItems)
  21. //}}AFX_MSG_MAP
  22. // Standard printing commands
  23. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  24. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  25. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CSampleView construction/destruction
  29. CSampleView::CSampleView()
  30. {
  31. // TODO: add construction code here
  32. m_bOnlySelectedItems = FALSE;
  33. }
  34. CSampleView::~CSampleView()
  35. {
  36. }
  37. BOOL CSampleView::PreCreateWindow(CREATESTRUCT& cs)
  38. {
  39. // TODO: Modify the Window class or styles here by modifying
  40. //  the CREATESTRUCT cs
  41. return CView::PreCreateWindow(cs);
  42. }
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CSampleView drawing
  45. void CSampleView::OnDraw(CDC* pDC)
  46. {
  47. CSampleDoc* pDoc = GetDocument();
  48. ASSERT_VALID(pDoc);
  49. // TODO: add draw code for native data here
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CSampleView printing
  53. BOOL CSampleView::OnPreparePrinting(CPrintInfo* pInfo)
  54. {
  55. // default preparation
  56. return DoPreparePrinting(pInfo);
  57. }
  58. void CSampleView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
  59. {
  60. // TODO: add extra initialization before printing
  61. m_lstPPListBox.OnBeginPrinting(pDC, pInfo);
  62. }
  63. void CSampleView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
  64. {
  65. // TODO: add cleanup after printing
  66. m_lstPPListBox.OnEndPrinting(pDC, pInfo);
  67. }
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CSampleView diagnostics
  70. #ifdef _DEBUG
  71. void CSampleView::AssertValid() const
  72. {
  73. CView::AssertValid();
  74. }
  75. void CSampleView::Dump(CDumpContext& dc) const
  76. {
  77. CView::Dump(dc);
  78. }
  79. CSampleDoc* CSampleView::GetDocument() // non-debug version is inline
  80. {
  81. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSampleDoc)));
  82. return (CSampleDoc*)m_pDocument;
  83. }
  84. #endif //_DEBUG
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CSampleView message handlers
  87. int CSampleView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  88. {
  89. if (CView::OnCreate(lpCreateStruct) == -1)
  90. return -1;
  91. // TODO: Add your specialized creation code here
  92. CSampleApp* pApp = (CSampleApp*) AfxGetApp();
  93. CRect rect;
  94. rect.SetRectEmpty();
  95. if( pApp->m_bSingleSelection )
  96. {
  97. if( m_lstPPListBox.Create( WS_CHILD | WS_BORDER | WS_VSCROLL, rect, this, 100 ) == -1 )
  98. return -1;
  99. }
  100. else
  101. {
  102. if( m_lstPPListBox.Create( WS_CHILD | LBS_HASSTRINGS | LBS_MULTIPLESEL
  103. | LBS_EXTENDEDSEL | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT | LBS_USETABSTOPS | WS_VSCROLL,
  104. rect, this, 100 ) == -1 )
  105. return -1;
  106. }
  107. m_lstPPListBox.SetPrintTitle();
  108. m_lstPPListBox.ShowWindow (SW_SHOW);
  109. return 0;
  110. }
  111. void CSampleView::OnSize(UINT nType, int cx, int cy) 
  112. {
  113. CView::OnSize(nType, cx, cy);
  114. // TODO: Add your message handler code here
  115. // resize listbox, too
  116. if( cx > 0 && cy > 0 )
  117. m_lstPPListBox.MoveWindow( 0, 0, cx, cy );
  118. }
  119. void CSampleView::OnInitialUpdate() 
  120. {
  121. CView::OnInitialUpdate();
  122. // TODO: Add your specialized code here and/or call the base class
  123. m_lstPPListBox.AddString( "da...da...da...da...");
  124. m_lstPPListBox.AddString( "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890xyzp");
  125. for( int i = 0; i <= 100; i++ )
  126. {
  127. CString sItem;
  128. sItem.Format( "%d", i );
  129. sItem = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" + sItem;
  130. m_lstPPListBox.AddString( sItem );
  131. }
  132. m_lstPPListBox.SetFocus();
  133. m_lstPPListBox.SetCurSel( 0 );
  134. }
  135. void CSampleView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
  136. {
  137. // TODO: Add your specialized code here and/or call the base class
  138. m_lstPPListBox.OnPrint(pDC, pInfo);
  139. // CView::OnPrint(pDC, pInfo);
  140. }
  141. void CSampleView::OnOnlySelectedItems() 
  142. {
  143. // TODO: Add your command handler code here
  144. if( m_lstPPListBox.GetOnlySelectedItems() )
  145. {
  146. m_lstPPListBox.SetOnlySelectedItems( false );
  147. m_bOnlySelectedItems = FALSE;
  148. }
  149. else
  150. {
  151. m_lstPPListBox.SetOnlySelectedItems( true );
  152. m_bOnlySelectedItems = TRUE;
  153. }
  154. }
  155. void CSampleView::OnUpdateOnlySelectedItems(CCmdUI* pCmdUI) 
  156. {
  157. // TODO: Add your command update UI handler code here
  158. pCmdUI->SetCheck( m_bOnlySelectedItems );
  159. }