📄 vdbview.cpp
字号:
// VDBView.cpp : implementation of the CVDBView class
//
#include "stdafx.h"
#include "VDB.h"
#include "VDBDoc.h"
#include "VDBView.h"
#include <afxtempl.h> // for CArray
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CVDBView
IMPLEMENT_DYNCREATE(CVDBView, CFormView)
BEGIN_MESSAGE_MAP(CVDBView, CFormView)
//{{AFX_MSG_MAP(CVDBView)
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
ON_BN_CLICKED(IDC_BUTTON_CREATEHTML, OnButtonCreatehtml)
ON_LBN_SELCHANGE(IDC_LIST_TABLENAME, OnSelchangeListTablename)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CVDBView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CVDBView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CVDBView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVDBView construction/destruction
CVDBView::CVDBView()
: CFormView(CVDBView::IDD)
{
//{{AFX_DATA_INIT(CVDBView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
bCreate = false;
}
CVDBView::~CVDBView()
{
}
void CVDBView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVDBView)
DDX_Control(pDX, IDC_BUTTON_CONNECT, m_btnConn);
DDX_Control(pDX, IDC_BUTTON_CREATEHTML, m_btnCreateHTML);
//}}AFX_DATA_MAP
}
BOOL CVDBView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CVDBView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
m_btnConn.SetWindowPos(&wndTop, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
pDoc = (CVDBDoc *) GetDocument();
CListCtrl *pList;
CEdit *pEdit;
if (bCreate)
return;
pEdit = (CEdit *) GetDlgItem(IDC_EDIT_SERVER);
pEdit->SetWindowText(_T("")); // 服务器名
pEdit = (CEdit *) GetDlgItem(IDC_EDIT_USERNAME);
pEdit->SetWindowText(_T("")); // 用户名
pEdit = (CEdit *) GetDlgItem(IDC_EDIT_PASSWORD);
// pEdit->SetWindowText(strPassword); // 密码
pEdit = (CEdit *) GetDlgItem(IDC_EDIT_CATALOG);
pEdit->SetWindowText(_T("")); // 数据库名
/* 初始化表的详细列表 */
pList = (CListCtrl *) GetDlgItem(IDC_LIST_COLUMNS);
pList->SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
pList->InsertColumn(0, "说明", LVCFMT_LEFT, 150);
pList->InsertColumn(0, "自增", LVCFMT_LEFT, 40);
pList->InsertColumn(0, "默认值", LVCFMT_LEFT, 100);
pList->InsertColumn(0, "空", LVCFMT_LEFT, 30);
pList->InsertColumn(0, "长度", LVCFMT_LEFT, 50);
pList->InsertColumn(0, "类型", LVCFMT_LEFT, 70);
pList->InsertColumn(0, "列名", LVCFMT_LEFT, 100);
pList->InsertColumn(0, "", LVCFMT_LEFT, 10);
bCreate = true;
}
/////////////////////////////////////////////////////////////////////////////
// CVDBView printing
BOOL CVDBView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CVDBView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* pInfo)
{
// TODO: add extra initialization before printing
CSize sizeTotal(2100, 2970);
CSize sizePage(sizeTotal.cx, sizeTotal.cy);
CSize sizeLine(sizeTotal.cx, sizeTotal.cy);
SetScrollSizes(MM_LOMETRIC, sizeTotal, sizePage, sizeLine);
bool bPrintEnd = false;
aryPrintInfo.RemoveAll();
const Height = 2970; // 纸高度
const Width = 2100; // 纸宽度
long y, // 纵坐标
top = 150, // 上边距
bottom = 250, // 下边距
left = 100, // 左边距
right = 200, // 右边距
line = 54; // 行间距
long lColumn1 = 400,
lColumn2 = 300,
lColumn3 = 250,
lColumn4 = 850,
lCellSpace = 10;
CString strTableName;
structPrintInfo myPrintInfo;
int nSelCount;
int nStep;
/* 获取选择信息 */
CListBox *pBox;
pBox = (CListBox *) GetDlgItem(IDC_LIST_TABLENAME);
nSelCount = pBox->GetSelCount();
CArray<int, int> aryListBoxSel;
if (nSelCount)
{
aryListBoxSel.SetSize(nSelCount);
pBox->GetSelItems(nSelCount, aryListBoxSel.GetData());
}
else
{
nSelCount = pBox->GetCount();
aryListBoxSel.SetSize(nSelCount);
for (nStep = 0; nStep < nSelCount; nStep ++)
aryListBoxSel[nStep] = nStep;
}
myPrintInfo.nTableIndex = 0;
myPrintInfo.nIndex = 0;
aryPrintInfo.Add(myPrintInfo);
y = -top;
y -= line; // 打印标题
_RecordsetPtr pRS_Column;
pRS_Column.CreateInstance("ADODB.Recordset");
for (nStep = 0; nStep < nSelCount; nStep ++)
{
y -= line; // 隔行
pBox->GetText(aryListBoxSel[nStep], strTableName);
myPrintInfo.nIndex = 0;
/* 当不够打三行的时候新建一页 */
if (y < bottom - Height + line + line + line)
{
myPrintInfo.nTableIndex = nStep;
aryPrintInfo.Add(myPrintInfo);
y = -top;
y -= line;
}
y -= line; // 打印表名
pRS_Column = pDoc->m_pConnection->Execute("Select c.name as TableName, a.name as ColumnName, b.name as TypeName, a.length as Length, a.scale as Scale, a.isnullable as IsNullAble from syscolumns a, systypes b, sysObjects c where a.xtype = b.xtype and a.id = c.id and c.xtype='U' and c.name = '" + _bstr_t(strTableName) + "'", NULL, adCmdText);
/* 打印表头 */
while (!pRS_Column->adoEOF)
{
y -= line; // 打印内容
/* 如果到最底,新建一页 */
if (y < bottom - Height)
{
myPrintInfo.nTableIndex = nStep;
aryPrintInfo.Add(myPrintInfo);
y = -top;
y -= line; // 页码
y -= line; // 表名
y -= line;
}
myPrintInfo.nIndex ++;
pRS_Column->MoveNext();
}
y -= line;
}
pInfo->SetMaxPage(aryPrintInfo.GetSize());
}
void CVDBView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CVDBView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: add customized printing code here
/* 设置页面大小 */
CSize sizeTotal(2100, 2970);
CSize sizePage(sizeTotal.cx, sizeTotal.cy);
CSize sizeLine(sizeTotal.cx, sizeTotal.cy);
SetScrollSizes(MM_LOMETRIC, sizeTotal, sizePage, sizeLine);
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP_KEYS);
CDC memDc;
memDc.CreateCompatibleDC(pDC);
CBitmap * obmp = memDc.SelectObject(&bmp);
BITMAP bm;
bmp.GetBitmap(&bm);
/* 设置变量 */
const Height = 2970; // 纸高度
const Width = 2100; // 纸宽度
long x, // 横坐标
y, // 纵坐标
top = 150, // 上边距
bottom = 250, // 下边距
left = 100, // 左边距
right = 200, // 右边距
line = 54; // 行间距
long lColumn0 = 30, // 第 0 列
lColumn1 = 350, // 第一列
lColumn2 = 220, // 第二列
lColumn3 = 100, // 第三列
lColumn4 = 50, // 第四列
lColumn5 = 330,
lColumn6 = 100,
lColumn7 = 950,
lCellSpace = 10; // 表格间距
CFont hNewFont;
CString strPrint;
BOOL bKeys;
CRect rc;
CVDBDoc* pDoc = (CVDBDoc *) GetDocument();
/* 开始打印 */
CString strFileName;
CString strServer;
CString strCatalog;
CString strTableName;
/* 获取选择范围 */
int nSelCount;
int nStep;
CEdit *pEdit;
pEdit = (CEdit *) GetDlgItem(IDC_EDIT_SERVER);
pEdit->GetWindowText(strServer);
pEdit = (CEdit *) GetDlgItem(IDC_EDIT_CATALOG);
pEdit->GetWindowText(strCatalog);
CListBox *pBox;
pBox = (CListBox *) GetDlgItem(IDC_LIST_TABLENAME);
nSelCount = pBox->GetSelCount();
CArray<int, int> aryListBoxSel;
if (nSelCount)
{
aryListBoxSel.SetSize(nSelCount);
pBox->GetSelItems(nSelCount, aryListBoxSel.GetData());
}
else
{
nSelCount = pBox->GetCount();
aryListBoxSel.SetSize(nSelCount);
for (nStep = 0; nStep < nSelCount; nStep ++)
aryListBoxSel[nStep] = nStep;
}
/* 打印页标 */
x = left;
y = -top;
strPrint.Format("第 %d 页 共 %d 页", pInfo->m_nCurPage, pInfo->GetMaxPage());
hNewFont.CreateFont(-23,
0,
0,
0,
FW_NORMAL,
0,
0,
0,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE,
"宋体");
pDC->SelectObject(&hNewFont);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextAlign(TA_RIGHT);
pDC->TextOut((Width - right), y, strPrint);
hNewFont.DeleteObject();
/* 如果是第一页,打印数据库标题 */
if (pInfo->m_nCurPage == 1)
{
strPrint = strServer + " - " + strCatalog;
hNewFont.CreateFont(-46,
0,
0,
0,
FW_NORMAL,
0,
0,
0,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE,
"楷体_gb2312");
pDC->SelectObject(&hNewFont);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextAlign(TA_CENTER);
pDC->TextOut((Width - right + left)/ 2, y, strPrint);
hNewFont.DeleteObject();
y -= line;
}
_variant_t varValue;
_RecordsetPtr pRS_Column, pRS_Column_Detail;
pRS_Column.CreateInstance("ADODB.Recordset");
pRS_Column_Detail.CreateInstance("ADODB.Recordset");
for (nStep = aryPrintInfo[pInfo->m_nCurPage - 1].nTableIndex; nStep < nSelCount; nStep ++)
{
x = left;
y -= line;
pBox->GetText(aryListBoxSel[nStep], strTableName);
if (y < bottom - Height + line + line + line)
return;
/* 打印表名 */
hNewFont.CreateFont(-34,
0,
0,
0,
FW_NORMAL,
0,
0,
0,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE,
"黑体");
pDC->SelectObject(&hNewFont);
pDC->SetTextAlign(TA_LEFT);
strPrint = strTableName;
pDC->TextOut(x + lCellSpace, y, strPrint);
hNewFont.DeleteObject();
/* 左侧竖线 */
pDC->MoveTo(x, y + 10);
pDC->LineTo(x, y - line + 10);
/* 右侧竖线 */
pDC->MoveTo(Width - right, y + 10);
pDC->LineTo(Width - right, y - line + 10);
/* 上面横线 */
pDC->MoveTo(left, y + 10);
pDC->LineTo(Width - right, y + 10);
/* 下面横线 */
pDC->MoveTo(left, y -line + 10);
pDC->LineTo(Width - right, y - line + 10);
y -= line;
pRS_Column = pDoc->m_pConnection->Execute(_bstr_t("Select a.status as Status, a.cdefault as cdefault, a.id as id, a.colorder as colorder, c.name as TableName, a.name as ColumnName, b.name as TypeName, a.length as Length, a.scale as Scale, a.isnullable as IsNullAble from syscolumns a, systypes b, sysObjects c where a.xtype = b.xtype and a.id = c.id and c.xtype='U' and c.name = '" + strTableName + "' order by a.colorder"), NULL, adCmdText);
if (aryPrintInfo[pInfo->m_nCurPage - 1].nTableIndex == nStep)
{
pRS_Column->Move(aryPrintInfo[pInfo->m_nCurPage - 1].nIndex);
}
/* 打印表头 */
hNewFont.CreateFont(-34,
0,
0,
0,
FW_BOLD,
0,
0,
0,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE,
"宋体");
pDC->SelectObject(&hNewFont);
pDC->SetTextAlign(TA_LEFT);
pDC->MoveTo(x, y + 10);
pDC->LineTo(x, y - line + 10);
x += lColumn0;
strPrint = "列名";
pDC->TextOut(x + lCellSpace, y, strPrint);
pDC->MoveTo(x, y + 10);
pDC->LineTo(x, y - line + 10);
x += lColumn1;
strPrint = "类型";
pDC->TextOut(x + lCellSpace, y, strPrint);
pDC->MoveTo(x, y + 10);
pDC->LineTo(x, y - line + 10);
x += lColumn2;
strPrint = "长度";
pDC->TextOut(x + lCellSpace, y, strPrint);
pDC->MoveTo(x, y + 10);
pDC->LineTo(x, y - line + 10);
x += lColumn3;
strPrint = "空";
pDC->TextOut(x + lCellSpace, y, strPrint);
pDC->MoveTo(x, y + 10);
pDC->LineTo(x, y - line + 10);
x += lColumn4;
strPrint = "默认值";
pDC->TextOut(x + lCellSpace, y, strPrint);
pDC->MoveTo(x, y + 10);
pDC->LineTo(x, y - line + 10);
x += lColumn5;
strPrint = "自增";
pDC->TextOut(x + lCellSpace, y, strPrint);
pDC->MoveTo(x, y + 10);
pDC->LineTo(x, y - line + 10);
x += lColumn6;
strPrint = "说明";
pDC->TextOut(x + lCellSpace, y, strPrint);
pDC->MoveTo(x, y + 10);
pDC->LineTo(x, y - line + 10);
pDC->MoveTo(Width - right, y + 10);
pDC->LineTo(Width - right, y - line + 10);
pDC->MoveTo(left, y - line + 10);
pDC->LineTo(Width - right, y - line + 10);
hNewFont.DeleteObject();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -