У меня есть пользовательский элемент управления, в котором я пытаюсь реализовать элемент управления сетки 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: файл реализации
//
#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 dialogCMFCApplication6Dlg::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 hereCRect 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);
}
// MFCApplication6Dlg.h: заголовочный файл
//
#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;
};
Когда я запускаю это, я получаю следующую ошибку
atlTraceUtil - Warning: dialog creation failed, so application is terminating unexpectedly.
atlTraceUtil - Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.
The thread 0x3a58 has exited with code 0 (0x0).
The thread 0x3d58 has exited with code 0 (0x0).
The thread 0x22a8 has exited with code 0 (0x0).
The program '[14088] MFCApplication6.exe' has exited with code 0 (0x0).
Любая помощь будет оценена.
Я не добавил класс для пользовательского элемента управления, как MFCGridCtrl
, Как только я это сделал, все работает отлично.
Других решений пока нет …