📄 gridsvw5.cpp
字号:
void COwnerDrawnComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = 20;
}
BEGIN_MESSAGE_MAP(COwnerDrawnComboBox, CGXComboBoxWnd)
//{{AFX_MSG_MAP(COwnerDrawnComboBox)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL COwnerDrawnComboBox::OnCommand(WPARAM wParam, LPARAM lParam)
{
#if _MFC_VER < 0x0300
UINT nNotification = HIWORD(lParam);
HWND hCtl = (HWND) LOWORD(lParam);
#else
UINT nNotification = HIWORD(wParam);
HWND hCtl = (HWND) lParam;
#endif
if (hCtl == m_hWnd) // is it really this wnd?
{
if (nNotification == CBN_SELCHANGE
|| nNotification == CBN_EDITCHANGE)
{
TRACE("Combobox changed\n");
}
}
return CGXComboBoxWnd::OnCommand(wParam, lParam);
}
/////////////////////////////////////////////////////////////////////////////
// CGridSample5View
BEGIN_MESSAGE_MAP(CGridSample5View, CMyGridView)
//{{AFX_MSG_MAP(CGridSample5View)
ON_COMMAND(ID_VIEW_USERACTIONS, OnViewUseractions)
ON_COMMAND(ID_VIEW_SPLITTERVIEW, OnViewSplitterview)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGridSample5View construction/destruction
CGridSample5View::CGridSample5View()
{
// TODO: add construction code here
}
CGridSample5View::~CGridSample5View()
{
}
BOOL CGridSample5View::ConnectParam()
{
// Note: this method is copied from CGridSampleView
//
BOOL bNew = FALSE;
// Retrive the zero-based worksheet-id if used as worksheet
if (GetParentTabWnd(this, TRUE))
m_nViewID = GetParentTabViewID(this);
// check if it is a new pane in a splitter window
CSplitterWnd* pSplitterWnd = GetParentDynamicSplitter(this, TRUE);
if (pSplitterWnd != NULL)
{
CGXGridView *pView = (CGXGridView *) pSplitterWnd->GetPane(0, 0);
if (pView != this)
m_nViewID = pView->GetViewID();
}
// check if parameter-object exist (in document)
if (GetDocument()->GetParam(m_nViewID) == NULL)
{
// create a parameter-object on the heap
GetDocument()->SetParam(m_nViewID, new CGXGridParam);
bNew = TRUE; // this view needs initializing
}
// connect parameter-object with grid
SetParam((CGXGridParam*) GetDocument()->GetParam(m_nViewID), FALSE);
return bNew;
}
void CGridSample5View::SetupControls()
{
// Register all controls for the view
// First, the simple control
RegisterControl(IDS_CTRL_SIMPLE, new CSimpleButton(this));
// With its MsgText to display when the button is pressed
GetParam()->GetStylesMap()->AddUserAttribute(IDS_ATTR_MSGTEXT);
// the Message Text is specified in OnInitialUpdate!
// In OnInitialUpdate, I do set the default value for this attribute
// by calling style->SetUserAttribute(IDS_ATTR_MSGTEXT, "You pressed the button");
RegisterControl(IDS_CTRL_EDITBTN, new CBitmapBtnEdit(this, IDS_CTRL_EDITBTN));
RegisterControl(IDS_CTRL_ARROWHEADER, new CArrowRowHeader(this));
RegisterControl(GX_IDS_CTRL_COMBOBOX, new CMyComboBox(this, 2001, 2002, 0));
// CGXWndWrapper allows you to register any CWnd you want.
CGXTabBeam* pBeam = new CGXTabBeam;
pBeam->Create(WS_CHILD | WS_CLIPSIBLINGS, CRect(0,0,0,0), this, IDS_CTRL_TABBEAM);
pBeam->InsertTab(FALSE, "Tab 1");
pBeam->InsertTab(FALSE, "Tab 2");
pBeam->InsertTab(FALSE, "Tab 3");
pBeam->AdjustTabSize(0);
pBeam->AdjustTabSize(1);
pBeam->AdjustTabSize(2);
pBeam->DisplayScrollBtns(FALSE);
RegisterControl(IDS_CTRL_TABBEAM, new CGXWndWrapper(this, pBeam, TRUE, TRUE, TRUE));
// ownerdrawn CComboBox with CBS_DROPDOWNLIST style
{
CGXComboBoxWnd* pWnd = new COwnerDrawnComboBox(this);
pWnd->Create(WS_VSCROLL | CBS_DROPDOWNLIST | CBS_OWNERDRAWFIXED, IDS_CTRL_OWNERDRAWNDROPDOWN);
RegisterControl(IDS_CTRL_OWNERDRAWNDROPDOWN, pWnd);
for (int n = 0; n < 20; n++)
pWnd->AddString((LPCTSTR) VGAColorsArray[n]);
}
}
/////////////////////////////////////////////////////////////////////////////
// CGridSample5View drawing
void CGridSample5View::OnInitialUpdate()
{
BOOL bNew = ConnectParam();
CMyGridView::OnInitialUpdate(); // Creates all objects and links them to the grid
// Register all controls for the view
SetupControls();
if (bNew)
{
// Don't create undo-information for the following commands
GetParam()->EnableUndo(FALSE);
// (at the end of this procedure, I will reenable it)
// Apply Arrow-Header-Control to Row-Header-Style
RowHeaderStyle()
.SetControl(IDS_CTRL_ARROWHEADER)
.SetEnabled(FALSE); // disables usage as current cell
// Apply Dropdown-List-Control to Column-Header-Style
ColHeaderStyle()
.SetControl(GX_IDS_CTRL_CBS_DROPDOWNLIST)
.SetInterior(RGB(192,192,192))
.SetHorizontalAlignment(DT_CENTER)
.SetWrapText(FALSE)
.SetVerticalAlignment(DT_VCENTER)
.SetFont(CGXFont().SetBold(TRUE))
.SetChoiceList("Combo Box\none\ntwo\nthree\nfour\nfive\nsix\nseven\neight")
.SetValue("Combo Box")
.SetDraw3dFrame(gxFrameRaised);
SetRowHeight(0, 0, 30);
// Cell (0,0) is treated like a normal column header
// So, I do need to reset the style for the cell explicitly.
SetStyleRange(CGXRange(0,0), CGXStyle()
.SetControl(GX_IDS_CTRL_HEADER)
.SetEnabled(FALSE) // disables usage as current cell
.SetValue("")
);
// Turn off selecting whole columns when clicking on a column header
GetParam()->EnableSelection((WORD) (GX_SELFULL & ~GX_SELCOL & ~GX_SELTABLE));
// Apply default message text for simple control to standard style
StandardStyle()
.SetUserAttribute(IDS_ATTR_MSGTEXT, "You pressed the button");
// Number of rows and columns
SetRowCount(100);
SetColCount(20);
// Turn off displaying vertical and horizontal lines
CGXProperties* pProp = GetParam()->GetProperties();
pProp->SetDisplayVertLines(FALSE);
pProp->SetDisplayHorzLines(FALSE);
pProp->SetPrintRowHeaders(FALSE); // no row headers when printing
// Setup the table of controls
SetRowHeight(5, 25, Height_LPtoDP(GX_NYHEIGHT)*2);
// GX_NYHEIGHT is the logical height of a line
SetColWidth(2, 2, Width_LPtoDP(GX_NXAVGWIDTH)*16);
SetColWidth(3, 3, Width_LPtoDP(GX_NXAVGWIDTH)*16);
// GX_NXAVGWIDTH is the average logical width of a char
// Fill table
// Background and interior borders
SetStyleRange(CGXRange(5,2,24,3), CGXStyle()
.SetBorders(gxBorderAll, CGXPen().SetStyle(PS_DOT))
.SetInterior(RGB(192, 192, 192))
);
// Alignment
SetStyleRange(CGXRange(5,2,24,2), CGXStyle()
.SetVerticalAlignment(DT_VCENTER)
.SetHorizontalAlignment(DT_CENTER)
.SetFont(CGXFont().SetBold(TRUE))
);
// Headers
SetStyleRange(CGXRange(5,2,5,3), CGXStyle()
.SetBorders(gxBorderBottom, CGXPen().SetWidth(2))
.SetBorders(gxBorderTop, CGXPen().SetWidth(2))
.SetFont(CGXFont().SetBold(TRUE).SetSize(12))
.SetEnabled(FALSE) // disables usage as current cell
.SetInterior(RGB(192,192,192)) // light grey
.SetHorizontalAlignment(DT_CENTER)
.SetVerticalAlignment(DT_VCENTER)
);
SetStyleRange(CGXRange(5,2), CGXStyle().SetValue("Name"));
SetStyleRange(CGXRange(5,3), CGXStyle().SetValue("Control"));
SetStyleRange(CGXRange(5,2,24,2), CGXStyle().SetEnabled(FALSE));
// thick borders
SetStyleRange(CGXRange(5,3,24,3), CGXStyle().SetBorders(gxBorderLeft, CGXPen().SetWidth(2)));
SetStyleRange(CGXRange(5,2,24,2), CGXStyle().SetBorders(gxBorderLeft, CGXPen().SetWidth(2)));
SetStyleRange(CGXRange(5,3,24,3), CGXStyle().SetBorders(gxBorderRight, CGXPen().SetWidth(2)));
SetStyleRange(CGXRange(24,2,24,3), CGXStyle().SetBorders(gxBorderBottom, CGXPen().SetWidth(2)));
// controls
SetStyleRange(CGXRange(6,2), CGXStyle().SetValue("CSimpleButton"));
SetStyleRange(CGXRange(6,3), CGXStyle()
.SetControl(IDS_CTRL_SIMPLE)
.SetHorizontalAlignment(DT_CENTER)
.SetVerticalAlignment(DT_VCENTER)
.SetInterior(RGB(192,192,192))
.SetFont(CGXFont().SetBold(TRUE))
.SetValue("Simple")
);
SetCoveredCellsRowCol(6, 4, 7, 5);
SetStyleRange(CGXRange(6,4), "<- You can specify the message text with Format->Cells->User");
SetStyleRange(CGXRange(7,2), CGXStyle().SetValue("CGXEditControl"));
SetStyleRange(CGXRange(7,3), CGXStyle()
.SetControl(GX_IDS_CTRL_EDIT)
.SetValue("Editable Text")
);
SetStyleRange(CGXRange(8,2), CGXStyle().SetValue("CGXSpinEdit"));
SetStyleRange(CGXRange(8,3), CGXStyle()
.SetControl(GX_IDS_CTRL_SPINEDIT)
.SetValue("0")
);
SetStyleRange(CGXRange(9,2), CGXStyle().SetValue("CGXHotSpotEdit"));
SetStyleRange(CGXRange(9,3), CGXStyle()
.SetControl(GX_IDS_CTRL_HOTSPOT)
.SetValue("Editable Text")
);
SetStyleRange(CGXRange(10,2), CGXStyle().SetValue("CGXCheckBox"));
SetStyleRange(CGXRange(10,3), CGXStyle()
.SetControl(GX_IDS_CTRL_CHECKBOX)
.SetChoiceList("Click Me!")
);
SetStyleRange(CGXRange(11,3), CGXStyle()
.SetControl(GX_IDS_CTRL_CHECKBOX3D)
.SetChoiceList("Click Me!")
.SetInterior(RGB(192,192,192))
);
SetStyleRange(CGXRange(12,2), CGXStyle().SetValue("CGXPushbutton"));
SetStyleRange(CGXRange(12,3), CGXStyle()
.SetControl(GX_IDS_CTRL_PUSHBTN)
.SetChoiceList("Push Me!")
);
SetStyleRange(CGXRange(13,2), CGXStyle().SetValue("CGXStatic"));
SetStyleRange(CGXRange(13,3), CGXStyle()
.SetControl(GX_IDS_CTRL_STATIC)
.SetValue("Text")
);
SetCoveredCellsRowCol(14,2,15,2);
SetStyleRange(CGXRange(14,2), CGXStyle().SetValue("CGXRadioButton"));
SetCoveredCellsRowCol(14,3,15,3);
SetStyleRange(CGXRange(14,3), CGXStyle()
.SetControl(GX_IDS_CTRL_RADIOBTN)
.SetChoiceList("one\ntwo\nthree\n")
);
SetCoveredCellsRowCol(16,2,17,2);
SetCoveredCellsRowCol(16,3,17,3);
SetStyleRange(CGXRange(16,3), CGXStyle()
.SetControl(GX_IDS_CTRL_RADIOBTN3D)
.SetChoiceList("one\ntwo\nthree\n")
.SetInterior(RGB(192,192,192))
);
SetCoveredCellsRowCol(18,2,19,2);
SetStyleRange(CGXRange(18,2), CGXStyle().SetValue("CGXListBox"));
SetCoveredCellsRowCol(18,3,19,3);
SetStyleRange(CGXRange(18,3), CGXStyle()
.SetControl(GX_IDS_CTRL_LISTBOX)
.SetChoiceList("one\ntwo\nthree\n")
);
SetStyleRange(CGXRange(20,2), CGXStyle().SetValue("CGXComboBox"));
SetStyleRange(CGXRange(20,3), CGXStyle()
.SetControl(GX_IDS_CTRL_COMBOBOX)
.SetChoiceList("one\ntwo\nthree\nfour\n")
.SetValue("one")
);
SetStyleRange(CGXRange(21,2), CGXStyle().SetValue("CBitmapBtnEdit"));
SetStyleRange(CGXRange(21,3), CGXStyle()
.SetControl(IDS_CTRL_EDITBTN)
.SetValue("Editable Text with button")
);
SetStyleRange(CGXRange(22,2), CGXStyle().SetValue("CGXWndWrapper"));
SetStyleRange(CGXRange(22,3), CGXStyle()
.SetControl(IDS_CTRL_TABBEAM)
);
SetStyleRange(CGXRange(23,2), CGXStyle().SetValue("COwnerDrawnComboBox"));
SetStyleRange(CGXRange(23,3), CGXStyle()
.SetControl(IDS_CTRL_OWNERDRAWNDROPDOWN)
);
// Instructions
SetCoveredCellsRowCol(1, 1, 3, 5);
SetStyleRange(CGXRange(1,1),
CGXStyle()
.SetWrapText(TRUE)
.SetEnabled(FALSE)
.SetFont(CGXFont().SetFaceName("Times New Roman"))
.SetInterior(RGB(255,251,240)) // Off-white
.SetHorizontalAlignment(DT_CENTER)
.SetVerticalAlignment(DT_VCENTER)
.SetControl(GX_IDS_CTRL_STATIC)
.SetBorders(gxBorderAll, CGXPen().SetWidth(2))
.SetValue(szInstruct));
// Enable creation of undo-information for user interactions
GetParam()->EnableUndo(TRUE);
}
// Position the current cell
SetCurrentCell(4, 1, FALSE /* avoid immediate updating */);
// Enable Update-Hint-Mechanism
EnableHints();
}
/////////////////////////////////////////////////////////////////////////////
// CGridSample5View diagnostics
#ifdef _DEBUG
void CGridSample5View::AssertValid() const
{
CMyGridView::AssertValid();
}
void CGridSample5View::Dump(CDumpContext& dc) const
{
CMyGridView::Dump(dc);
}
CGridSampleDoc* CGridSample5View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGridSampleDoc)));
return (CGridSampleDoc*) m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGridSample5View message handlers
// Menu handler for View->Splitter View
void CGridSample5View::OnViewSplitterview()
{
CDocument* pDoc = GetDocument();
CMyMultiDocTemplate* pTemplate
= (CMyMultiDocTemplate*) ((CGridSampleApp*) AfxGetApp())->m_pSplitterTemplate;
pTemplate->SetViewClass(GetRuntimeClass());
CMDIChildWnd* pNewFrame
= (CMDIChildWnd*) pTemplate->CreateNewFrame(GetDocument(), NULL);
if (pNewFrame == NULL)
return; // not created
ASSERT(pNewFrame->IsKindOf(RUNTIME_CLASS(CSplitterMDIChildWnd)));
CSplitterWnd& splitter = (CSplitterWnd&)
((CSplitterMDIChildWnd *) pNewFrame)->m_wndSplitter;
CGridSample5View* pView = (CGridSample5View*)
splitter.GetPane(0, 0);
// Set view id to active tab view id
pView->m_nViewID = m_nViewID;
pTemplate->InitialUpdateFrame(pNewFrame, pDoc);
pNewFrame->GetActiveView();
ASSERT(pView);
}
// Menu handler for View->User Actions...
void CGridSample5View::OnViewUseractions()
{
/*
// just some performance checking
((CMDIChildWnd*) GetParentFrame())->MDIMaximize();
OnViewZoomin();
OnViewZoomin();
OnViewZoomin();
OnViewZoomout();
OnViewZoomout();
OnViewZoomout();
OnViewZoomout();
OnViewZoomout();
OnViewZoomin();
OnViewZoomin();
OnViewZoomin();
OnViewZoomin();
OnViewZoomin();
OnViewZoomout();
OnViewZoomout();
OnViewZoomout();
ProcessKeys(this, WM_CHAR, 20, 1, 0);
*/
// Note: this method is copied from CGridSampleView
//
// Shows a dialog with some attributes of the parameter-object
// where you can experiment with some attributes
// such as allowing the user to track columns, select cells
// or use the grid as a listbox.
// Transfer Current Cell's Data to grid
if (!TransferCurrentCell())
return;
CUserActionsDialog dlg(GetParam());
if (dlg.DoModal() == IDOK)
{
// Redraw the grid
Redraw();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -