我有一个自定义控件,我试图在其中实现此网站上显示的网格控件https://www.codeproject.com/Articles/8/MFC-Grid-control。 https://social.msdn.microsoft.com/Forums/vstudio/en-US/035a3e74-596d-4534-8543-2e6ee545d582/how-to-use-grid-control-in-mfc
我实现了以下代码
// MFCApplication6Dlg.cpp:实现文件 //
import { flatMap } from 'rxjs/operators';
import { throwError } from 'rxjs';
...
saveExtrudedHeight(extrudedHeight): Observable<any> {
const requestPayload = {
extrudedHeight
};
return this.authService.onTokenChange()
.pipe(flatMap(
(token: NbAuthJWTToken) => {
if (token.isValid()) {
this.user = token.getPayload().user;
this.payload = token.getPayload();
const email = this.payload.user['email'];
requestPayload.email = email;
// Make the changes here to send the email as the Request Payload.
return this.http.post('/extrudedHeight/save', requestPayload, httpOptions)
.pipe(
catchError(this.handleError)
);
} else {
throwError('Something went wrong!');
}
}
));
}
// MFCApplication6Dlg.h:头文件 //
#include "stdafx.h"
#include "MFCApplication6.h"
#include "MFCApplication6Dlg.h"
#include "afxdialogex.h"
#include "CellRange.h"
#include "GridCell.h"
#include "GridCellBase.h"
#include "GridCtrl.h"
#include "GridDropTarget.h"
#include "InPlaceEdit.h"
#include "MemDC.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CMFCApplication6Dlg dialog
CMFCApplication6Dlg::CMFCApplication6Dlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_MFCAPPLICATION6_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCApplication6Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_CUSTOM1, m_pGrid);
}
BEGIN_MESSAGE_MAP(CMFCApplication6Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()
// CMFCApplication6Dlg message handlers
BOOL CMFCApplication6Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX< strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CRect rect;
GetClientRect(rect);
m_pGrid.Create(rect, this, 100);
m_pGrid.SetEditable(true);
m_pGrid.SetTextBkColor(RGB(0xFF, 0xFF, 0xE0));//yellow background
m_pGrid.SetRowCount(8); //
m_pGrid.SetColumnCount(8); //
m_pGrid.SetFixedRowCount(1); //
m_pGrid.SetFixedColumnCount(1); //
for (int row = 0; row < m_pGrid.GetRowCount(); row++)
for (int col = 0; col < m_pGrid.GetColumnCount(); col++)
{
GV_ITEM Item;
Item.mask = GVIF_TEXT | GVIF_FORMAT;
Item.row = row;
Item.col = col;
m_pGrid.SetRowHeight(row, 25); //set row heigh
m_pGrid.SetColumnWidth(0, 64); //set column width
m_pGrid.SetColumnWidth(col, 64); //
if (row == 0 && col == 0) //
{
Item.nFormat = DT_CENTER | DT_WORDBREAK;
Item.strText.Format(_T("data table"), col);
}
else if (row < 1) //
{
Item.nFormat = DT_CENTER | DT_WORDBREAK;
Item.strText.Format(_T(" program%d"), col);
}
else if (col < 1) //
{
if (row < m_pGrid.GetRowCount())
{
Item.nFormat = DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
Item.strText.Format(_T("the %d"), row);
}
}
else
{
Item.nFormat = DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
Item.strText.Format(_T(""), 2);
}
m_pGrid.SetItem(&Item);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CMFCApplication6Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMFCApplication6Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMFCApplication6Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
运行此命令时,出现以下错误
#pragma once
#include "CellRange.h"
#include "GridCell.h"
#include "GridCellBase.h"
#include "GridCtrl.h"
#include "GridDropTarget.h"
#include "InPlaceEdit.h"
#include "MemDC.h"
// CMFCApplication6Dlg dialog
class CMFCApplication6Dlg : public CDialogEx
{
// Construction
public:
CMFCApplication6Dlg(CWnd* pParent = nullptr); // standard constructor
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_MFCAPPLICATION6_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
CGridCtrl m_pGrid;
CSize m_OldSize;
};
任何帮助将不胜感激。
答案 0 :(得分:0)
我没有将自定义控件的类添加为MFCGridCtrl
。完成后,效果很好。