📄 9925.txt
字号:
}
if (nCol < nFirstCol) // 移到GRID的左边时换上一行
{
nCol = nCols - 1;
nRow--;
}
if (nRow < nFirstRow)
// 移到GRID的上端时换下端
nRow = nRows - 1;
if (nRow >= nRows)
// 移到GRID的下端时换上端
nRow = nFirstRow;
//设置移动后GRID的当前单元
pParent->m_bEventLockout = (nCol != nOldCol); //only allow one change event
pGrid->SetNumProperty("Row", nRow);
pParent->m_bEventLockout = FALSE;
pGrid->SetNumProperty("Col",nCol); // generate Row/Col change event
return TRUE;
}
改造后的Grid 类CGridEntry :
CGridEntry 类是CFormView的派生类,数据成员m_pGrid指向GRID控制,实现可直
接在GRID单元上输入的"电子表格"。m_edit是CMyEdit 类对象。成员函数
SizeToFit()用于调整编辑窗口大小,使其与网格单元一致。成员函数PositionEdit()
用于调整编辑窗口位置,接收新的输入焦点。函数FindCellPosition 实现根据网
格的行列号,返回单元的左上角坐标。
今后使用CGridEntry 类,仅仅需在App studio 中定制
class CGridEntry : public CFormView
{
DECLARE_DYNCREATE(CGridEntry)
protected:
CGridEntry();
public:
enum { IDD = IDD_GRIDENTRY };
//对话框资源ID值
CVBControl* m_pGrid;
CMyEdit
m_edit;
public:
BOOL
m_bInitialized;
protected:
void SizeToFit();
void PositionEdit();
CPoint FindCellPosition(int nRow, int nCol);
virtual void DoDataExchange(CDataExchange* pDX);
virtual void OnInitialUpdate();
// first time after construct
afx_msg void OnRowColChangeGrid(UINT, int, CWnd*, LPVOID);
afx_msg void OnChangeGridEdit();
afx_msg void OnClickGrid(UINT, int, CWnd*, LPVOID);
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(CGridEntry, CFormView)
BEGIN_MESSAGE_MAP(CGridEntry, CFormView)
ON_VBXEVENT(VBN_ROWCOLCHANGE, IDC_GRID, OnRowColChangeGrid)
ON_EN_CHANGE(IDC_GRIDEDIT, OnChangeGridEdit)
ON_VBXEVENT(VBN_CLICK, IDC_GRID, OnClickGrid)
END_MESSAGE_MAP()
CGridEntry::CGridEntry(): CFormView(CGridEntry::IDD)
{
m_pGrid = NULL;
m_bInitialized = FALSE;
}
void CGridEntry::OnInitialUpdate()
// first time after construct
{
// 动态划分一个控件的子集,并使m_edit从属于CGridEntry对象
m_edit.SubclassDlgItem(IDC_GRIDEDIT, this);
CFormView::OnInitialUpdate();
//设置GRID控制窗口的风格包含 WS_CLIPSIBLINGS风格
HWND hWndGrid = m_pGrid->GetSafeHwnd();
DWORD dwStyles = ::GetWindowLong(hWndGrid, GWL_STYLE);
::SetWindowLong(hWndGrid, GWL_STYLE, dwStyles | WS_CLIPSIBLINGS);
if (!m_bInitialized)
{
m_bEventLockout = TRUE;
int nRows = (int)m_pGrid->GetNumProperty("Rows");
int nCols = (int)m_pGrid->GetNumProperty("Cols");
char buf[80];
m_pGrid->SetNumProperty("Col", 0); //设置GRID的行号
for (int nRow = 1; nRow < nRows; nRow++)
{
wsprintf(buf, "%d", nRow);
m_pGrid->SetNumProperty("Row", nRow);
m_pGrid->SetStrProperty("Text", buf);
}
m_pGrid->SetNumProperty("Row", 0); //设置GRID的列号
for (int nCol = 1; nCol < nCols; nCol++)
{
wsprintf(buf, "%c", 'A' + nCol - 1);
m_pGrid->SetNumProperty("Col", nCol);
m_pGrid->SetStrProperty("Text", buf);
}
for (nRow = 1; nRow < nRows; nRow++)
{
//在此添加初始GRID单元的代码
}
m_pGrid->SetNumProperty("Row", 1);
m_pGrid->SetNumProperty("Col", 1);
// force edit to reposition
m_bEventLockout = FALSE;
}
SizeToFit();
PositionEdit();
}
void CGridEntry::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_VBControl(pDX, IDC_GRID, m_pGrid);
}
void CGridEntry::OnRowColChangeGrid(UINT, int, CWnd*, LPVOID)
{
PositionEdit();
}
#define TWIPS_PER_INCH 1440
void CGridEntry::SizeToFit()
{
m_pGrid->SetNumProperty("LeftCol", m_pGrid->GetNumProperty("FixedCols"));
m_pGrid->SetNumProperty("TopRow", m_pGrid->GetNumProperty("FixedRows"));
//ptSize 为GRID的逻辑尺寸
CPoint ptSize = FindCellPosition((int)m_pGrid->GetNumProperty("Rows"),(int)
m_pGrid->GetNumProperty("Cols"));
if (ptSize.x != (int)m_pGrid->GetNumProperty("Width") ||
ptSize.y != (int)m_pGrid->GetNumProperty("Height"))
{//GRID的大小与逻辑尺寸不一致时,调用Move 方法重新设置GRID的大小及尺寸
CRect rect;
rect.left = (int)m_pGrid->GetNumProperty("Left");
rect.top
= (int)m_pGrid->GetNumProperty("Top");
rect.right = rect.left + ptSize.x;
rect.bottom = rect.top + ptSize.y;
m_pGrid->Move(rect);
ptSize.x += 2 * rect.left;
ptSize.y += 2 * rect.top;
//设置滚动条
SetScrollSizes(MM_TEXT, (CSize)ptSize);
}
}
// 返回单元左上角的相对逻辑坐标,
// 当 nRow == maxRow and nCol == maxCol 时,返回值即为GRID的大小
CPoint CGridEntry::FindCellPosition(int nRow, int nCol)
{
ASSERT(nRow>= 0 && nRow <= m_pGrid->GetNumProperty("Rows"));
ASSERT(nCol >= 0 && nCol <= m_pGrid->GetNumProperty("Cols"));
CPoint ptPos(0, 0);
// Get left edge of requested cell by summing widths of previous cells
int nLeftCol = (int)m_pGrid->GetNumProperty("LeftCol");
int nLeftFix = (int)m_pGrid->GetNumProperty("FixedCols");
for (int i = 0; i < nCol; i++)
{
//累加列宽度
if (i < nLeftFix || i >= nLeftCol) // only count displayed cells
ptPos.x += (int)m_pGrid->GetNumProperty("ColWidth", i);
}
// Get top edge of requested cell by summing Heights of previous cells
int nTopRow = (int)m_pGrid->GetNumProperty("TopRow");
int nTopFix = (int)m_pGrid->GetNumProperty("FixedRows");
for (i = 0; i < nRow; i++ i++)
{ //累加行高度
if (i < nTopFix || i >= nTopRow)
// only count displayed cells
ptPos.y += (int)m_pGrid->GetNumProperty("RowHeight", i);
}
// ptPos 为逻辑twips单位,需转变为点象素单位
CClientDC dc(this);
ptPos.x = MulDiv(ptPos.x, dc.GetDeviceCaps(LOGPIXELSX), TWIPS_PER_INCH);
ptPos.y = MulDiv(ptPos.y, dc.GetDeviceCaps(LOGPIXELSY), TWIPS_PER_INCH);
ptPos.x += nCol;
// add one pixel per column for gap
ptPos.y += nRow;
// add one pixel per row
for gap
return ptPos;
}
void CGridEntry::PositionEdit()
{
//移动编辑窗口到适当位置,并从GRID单元取出文本添充当前编辑窗口
int nRow = (int)m_pGrid->GetNumProperty("Row");
int nCol = (int)m_pGrid->GetNumProperty("Col");
CPoint ptPos = FindCellPosition(nRow, nCol);
// ptPos为相对坐标需加上GRID的左上角坐标
ptPos.x += (int)m_pGrid->GetNumProperty("Left");
ptPos.y += (int)m_pGrid->GetNumProperty("Top");
CClientDC dc(this);
int nWidth= MulDiv((int)m_pGrid->GetNumProperty("ColWidth", nCol),
(int)dc.GetDeviceCaps(LOGPIXELSX), TWIPS_PER_INCH) + 2;
int nHeight= MulDiv((int)m_pGrid->GetNumProperty("RowHeight", nRow),
(int)dc.GetDeviceCaps(LOGPIXELSY), TWIPS_PER_INCH) + 2;
//移动编辑窗口位置及调整其大小,并添充当前编辑窗口
m_edit.MoveWindow(ptPos.x, ptPos.y, nWidth, nHeight, TRUE);
CString s = m_pGrid->GetStrProperty("Text");
m_edit.SetWindowText(s);
// Set to reflect cell's data
m_edit.ShowWindow(SW_SHOW);
m_edit.SetSel(0, -1);
m_edit.Invalidate();
}
void CGridEntry::OnChangeGridEdit()
{
CString str;
BOOL bValid;
int nValue = GetDlgItemInt(IDC_GRIDEDIT, &bValid, TRUE);
if (!bValid)
return;
// must be a valid int in order to copy.
m_edit.GetWindowText(str);
m_pGrid->SetStrProperty("Text", str);
int nRow = (int)m_pGrid->GetNumProperty("Row");
int nCol = (int)m_pGrid->GetNumProperty("Col");
}
void CGridEntry::OnClickGrid(UINT, int, CWnd*, LPVOID)
{
SizeToFit();
PositionEdit();
}
****************************************************************
VC++程序中用对话框的形式显示HTML文件
zhou daibing
----在安装了IE4后,可以在程序中用对话框的形式显示HTML文件,如弹出用
HTML写的帮助文件等等,如同直接用浏览器,但又与浏览器风格不同。
----其实现如下:
//在头文件或.cpp文件的开头
包含文件urlmon.h,定义函数
/////
#include "urlmon.h"
typedef HRESULT STDAPICALLTYPE SHOWHTMLDIALOGFN
(HWND hwndParent, IMoniker
*pmk, VARIANT *pvarArgIn, TCHAR* pchOptions,
VARIANT *pvArgOut);
//////
//函数显示对话框,成功返回TRUE,失败返回FALSE
BOOL ShowHtml()
{
HINSTANCE hinstMSHTML = LoadLibrary
(TEXT("MSHTML.DLL")); //装载动态连接库
WCHAR url[]=L"HTTP://www.ccw.com.cn";
//此地址名称可直接用html文件名代替
if(hinstMSHTML)//装载动态连接库成功
{
SHOWHTMLDIALOGFN *pfnShowHTMLDialog;
pfnShowHTMLDialog = (SHOWHTMLDIALOGFN*)
GetProcAddress(hinstMSHTML,
TEXT ("ShowHTMLDialog"));
if(pfnShowHTMLDialog)
{
IMoniker *moniker=NULL;
//
if( FAILED(CreateURLMoniker(NULL,
(LPWSTR)url,&moniker ) ))
{
FreeLibrary(hinstMSHTML);
return FALSE;
}
//调用ShowHTMLDialog函数显示URL上的HTML文件
pfnShowHTMLDialog(m_hWnd,moniker,NULL,NULL,NULL);
if(moniker!=NULL)
moniker->Release();
//显示成功,返回TRUE
return TRUE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -