本文介绍了我无法通过双击mfc对话框的按钮直接打开代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 // ListDemoDlg.cpp:实现文件 // #includestdafx.h #includeListDemo.h #includeListDemoDlg.h #ifdef _DEBUG #define new DEBUG_NEW #endif // CListDemoDlg对话框 CListDemoDlg :: CListDemoDlg(CWnd * pParent / * = NULL * /) :CDialog(CListDemoDlg :: IDD,pParent) { m_hIcon = AfxGetApp() - > LoadIcon(IDR_MAINFRAME); } void CListDemoDlg :: DoDataExchange(CDataExchange * pDX) { CDialog: :DoDataExchange(pDX); DDX_Control(pDX,IDC_LIST1,mylist); } BEGIN_MESSAGE_MAP( CListDemoDlg,CDialog) ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}} AFX_MSG_MAP ON_BN_CLICKED(IDC_BUTTON1 ,& CListDemoDlg :: OnBnClickedButton1) END_MESSAGE_MAP() // CListDemoDlg消息处理程序 BOOL CListDemoDlg :: OnInitDialog() { CDialog :: OnInitDialog(); //设置图标对于这个对话框。框架自动执行此操作 //当应用程序的主窗口不是对话框时 SetIcon(m_hIcon,TRUE); //设置大图标 SetIcon(m_hIcon,FALSE); //设置小图标 LVCOLUMN lvColumn; int nCol; lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH; lvColumn.fmt = LVCFMT_LEFT; lvColumn.cx = 100; lvColumn.pszText = _T(产品名称); mylist.InsertColumn(0,& lvColumn); lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH; lvColumn.fmt = LVCFMT_LEFT; lvColumn.cx = 100; lvColumn.pszText = _T(Volume); mylist.InsertColumn(1,& lvColumn); lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH; lvColumn.fmt = LVCFMT_LEFT; lvColumn.cx = 100; lvColumn.pszText = _T(Capital); mylist.InsertColumn(2,& lvColumn); lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH; lvColumn.fmt = LVCFMT_LEFT; lvColumn.cx = 100; lvColumn.pszText = _T(评级); mylist.InsertColumn(3,& lvColumn); mylist.SetExtendedStyle(LVS_EX_FULLROWSELECT); 返回TRUE; //除非你将焦点设置为控件,否则返回TRUE } //如果在对话框中添加最小化按钮,将需要下面的代码 //来绘制图标。对于使用文档/视图模型的MFC应用程序, //这是由框架自动完成的。 void CListDemoDlg :: OnPaint() { if(IsIconic()) { CPaintDC dc(this); //绘画的设备上下文 SendMessage(WM_ICONERASEBKGND,reinterpret_cast< wparam>(dc.GetSafeHdc()),0); //客户端矩形中的中心图标 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; //画出图标 dc.DrawIcon(x,y,m_hIcon); } else { CDialog :: OnPaint(); } } //系统调用此函数获取光标到用户拖动时显示 //最小化窗口。 HCURSOR CListDemoDlg :: OnQueryDragIcon() { 返回static_cast< hcursor>(m_hIcon); } void CListDemo Dlg :: OnBnClickedButton1() { CString ProductName = _T(xyz); CString Volume = _T(1000) ; CString Capital = _T(23); CString Rating = _T(2); LVITEM lvItem; int nItem = 0; lvItem.mask = LVIF_TEXT; lvItem .iItem = 0; lvItem.iSubItem = 0; lvItem.pszText = CT2W(((LPCTSTR)ProductName)); nItem = mylist.InsertItem(& lvItem); for(int i = 0; i< 10; i ++) { nItem = mylist.InsertItem(& lvItem); mylist.SetItemText(nItem,0,CT2W(((LPCTSTR)ProductName))); mylist.SetItemText(nItem, 1,CT2W(((LPCTSTR)卷))); mylist.SetItemText(nItem,2,CT2W(((LPCTSTR)Capital))); mylist。 SetItemText(nItem,3,CT2W(((LPCTSTR)评级))); } UpdateData(FALSE); } 解决方案 HCURSOR CListDemoDlg :: OnQueryDragIcon() { 返回static_cast(m_hIcon); } void CListDemoDlg :: OnBnClickedButton1() { CString ProductName = _T(xyz); CString Volume = _T(1000); CString Capital = _T(23); CString Rating = _T(2 ); LVITEM lvItem; int nItem = 0; lvItem.mask = LVIF_TEXT; lvItem.iItem = 0; lvItem.iSubItem = 0; lvItem.pszText = CT2W(( (LPCTSTR)ProductName)); nItem = mylist.InsertItem(&lvItem); for(int i = 0; i< 10; i ++) { nItem = mylist.InsertItem(&lvItem); mylist.SetItemText(nItem,0,CT2W(((LPCTSTR)ProductName))); mylist.SetItemText(nItem,1,CT2W(((LPCTSTR)卷))); mylist.SetItemText(nItem,2,CT2W(((LPCTSTR)Capital)) ); mylist.SetItemText(nItem,3,CT2W(((LPCTSTR)评级))) ; } UpdateData(FALSE); } // ListDemoDlg.cpp : implementation file//#include "stdafx.h"#include "ListDemo.h"#include "ListDemoDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// CListDemoDlg dialogCListDemoDlg::CListDemoDlg(CWnd* pParent /*=NULL*/): CDialog(CListDemoDlg::IDD, pParent){m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CListDemoDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);DDX_Control(pDX, IDC_LIST1, mylist);}BEGIN_MESSAGE_MAP(CListDemoDlg, CDialog)ON_WM_PAINT()ON_WM_QUERYDRAGICON()//}}AFX_MSG_MAPON_BN_CLICKED(IDC_BUTTON1, &CListDemoDlg::OnBnClickedButton1)END_MESSAGE_MAP()// CListDemoDlg message handlersBOOL CListDemoDlg::OnInitDialog(){CDialog::OnInitDialog();// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE);// Set big iconSetIcon(m_hIcon, FALSE);// Set small iconLVCOLUMN lvColumn;int nCol;lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;lvColumn.fmt = LVCFMT_LEFT;lvColumn.cx = 100;lvColumn.pszText = _T("Product Name");mylist.InsertColumn(0, &lvColumn);lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;lvColumn.fmt = LVCFMT_LEFT;lvColumn.cx = 100;lvColumn.pszText = _T("Volume");mylist.InsertColumn(1, &lvColumn);lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;lvColumn.fmt = LVCFMT_LEFT;lvColumn.cx = 100;lvColumn.pszText = _T("Capital");mylist.InsertColumn(2, &lvColumn);lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;lvColumn.fmt = LVCFMT_LEFT;lvColumn.cx = 100;lvColumn.pszText = _T("Rating");mylist.InsertColumn(3, &lvColumn);mylist.SetExtendedStyle(LVS_EX_FULLROWSELECT);return TRUE; // return TRUE unless you set the focus to a control}// 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 CListDemoDlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, reinterpret_cast<wparam>(dc.GetSafeHdc()), 0);// Center icon in client rectangleint 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 icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}}// The system calls this function to obtain the cursor to display while the user drags// the minimized window.HCURSOR CListDemoDlg::OnQueryDragIcon(){return static_cast<hcursor>(m_hIcon);}void CListDemoDlg::OnBnClickedButton1(){CString ProductName = _T("xyz");CString Volume = _T("1000");CString Capital = _T("23");CString Rating = _T("2");LVITEM lvItem;int nItem=0;lvItem.mask = LVIF_TEXT;lvItem.iItem = 0;lvItem.iSubItem = 0;lvItem.pszText = CT2W(((LPCTSTR)ProductName));nItem = mylist.InsertItem(&lvItem); for(int i=0;i<10;i++) { nItem = mylist.InsertItem(&lvItem);mylist.SetItemText(nItem, 0, CT2W(((LPCTSTR)ProductName)));mylist.SetItemText(nItem, 1, CT2W(((LPCTSTR)Volume)));mylist.SetItemText(nItem, 2, CT2W(((LPCTSTR)Capital)));mylist.SetItemText(nItem, 3, CT2W(((LPCTSTR)Rating))); }UpdateData(FALSE);} 解决方案 HCURSOR CListDemoDlg::OnQueryDragIcon(){return static_cast(m_hIcon);} void CListDemoDlg::OnBnClickedButton1(){CString ProductName = _T("xyz");CString Volume = _T("1000");CString Capital = _T("23");CString Rating = _T("2"); LVITEM lvItem;int nItem=0; lvItem.mask = LVIF_TEXT;lvItem.iItem = 0;lvItem.iSubItem = 0;lvItem.pszText = CT2W(((LPCTSTR)ProductName));nItem = mylist.InsertItem(&lvItem);for(int i=0;i<10;i++){nItem = mylist.InsertItem(&lvItem);mylist.SetItemText(nItem, 0, CT2W(((LPCTSTR)ProductName)));mylist.SetItemText(nItem, 1, CT2W(((LPCTSTR)Volume)));mylist.SetItemText(nItem, 2, CT2W(((LPCTSTR)Capital)));mylist.SetItemText(nItem, 3, CT2W(((LPCTSTR)Rating)));}UpdateData(FALSE);} 这篇关于我无法通过双击mfc对话框的按钮直接打开代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 00:32