📄 childfrm.cpp
字号:
else if(pView->IsKindOf(RUNTIME_CLASS(CResultView)))
pRichEditCtrl = &m_pResultView->GetRichEditCtrl();
}
if(pRichEditCtrl != NULL)
{
long lStart = -1;
long lEnd = -1;
pRichEditCtrl->GetSel(lStart, lEnd);
m_strLineColInfo.Format(
_T("Ln %d, Col %d"),
pRichEditCtrl->LineFromChar(lStart)+1,
((lEnd-lStart) >= 0 ? lEnd-pRichEditCtrl->LineIndex(-1) :
lStart-pRichEditCtrl->LineIndex(-1))+1
);
}
}
pCmdUI->SetText(m_strLineColInfo);
pCmdUI->Enable(TRUE);
}
void CChildFrame::OnViewPrevResultSet()
{
if(m_nIndex > 1)
{
POSITION pos = m_GridList.FindIndex(m_nIndex-2);
DisplayResults((CMSFlexGrid*)m_GridList.GetAt(pos), --m_nIndex);
}
}
void CChildFrame::OnViewNextResultSet()
{
if(m_nIndex > 0 && m_nIndex < m_nGridCount)
{
POSITION pos = m_GridList.FindIndex(m_nIndex);
DisplayResults((CMSFlexGrid*)m_GridList.GetAt(pos), ++m_nIndex);
}
}
void CChildFrame::OnViewFirstResultSet()
{
if(m_nIndex > 1)
{
m_nIndex = 0;
POSITION pos = m_GridList.FindIndex(m_nIndex);
DisplayResults((CMSFlexGrid*)m_GridList.GetAt(pos), ++m_nIndex);
}
}
void CChildFrame::OnViewLastResultSet()
{
if(m_nIndex > 0 && m_nIndex < m_nGridCount)
{
m_nIndex = m_nGridCount;
POSITION pos = m_GridList.FindIndex(--m_nIndex);
DisplayResults((CMSFlexGrid*)m_GridList.GetAt(pos), ++m_nIndex);
}
}
void CChildFrame::OnUpdateViewPrevResultSet(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_nIndex > 1);
}
void CChildFrame::OnUpdateViewNextResultSet(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_nIndex > 0 && m_nIndex < m_nGridCount);
}
void CChildFrame::OnUpdateViewFirstResultSet(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_nIndex > 1);
}
void CChildFrame::OnUpdateViewLastResultSet(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_nIndex > 0 && m_nIndex < m_nGridCount);
}
void CChildFrame::DisplayResults(CMSFlexGrid* pGridCtrl, const int& nCurrentSet)
{
m_pResultView->GetRichEditCtrl().SetWindowText(NULL);
if(m_nGridCount > 1)
HideResultSets();
if(GetFocus() == m_pResultView)
{
pGridCtrl->SetFocus();
m_pResultView->SetRedraw(TRUE);
}
CRect rect;
m_pResultView->GetWindowRect(&rect);
m_pResultView->m_pGridCtrl = pGridCtrl;
pGridCtrl->MoveWindow(0, 0, rect.Width(), rect.Height());
pGridCtrl->ShowWindow(SW_SHOW);
pGridCtrl->Invalidate();
pGridCtrl->UpdateWindow();
m_pResultView->Invalidate();
m_pResultView->UpdateWindow();
m_strStatusText.Format(_T("Results grid #%d of %d; %d row(s); %d col(s)"),
nCurrentSet, m_nGridCount, pGridCtrl->GetRows()-1, pGridCtrl->GetCols());
m_wndStatusBar.SetPaneText(m_nResultSetPaneNo, m_strStatusText);
}
void CChildFrame::HideResultSets()
{
for(int n = 0; n < m_nGridCount; n++)
{
POSITION pos = m_GridList.FindIndex(n);
if(pos != NULL)
{
CMSFlexGrid* pGrid = (CMSFlexGrid*)m_GridList.GetAt(pos);
if(pGrid != NULL)
pGrid->ShowWindow(SW_HIDE);
}
}
}
void CChildFrame::OnUpdateSqlObjects(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_database.IsOpen() && !m_bExecuting);
}
void CChildFrame::OnUpdateSqlExecute(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_database.IsOpen() && m_pQryView != NULL
&& m_pQryView->GetRichEditCtrl().GetTextLength() && !m_bExecuting);
}
UINT CancelSQLProc(LPVOID lpVoid)
{
TCP* pTCP = (TCP*)lpVoid;
ASSERT(pTCP != NULL);
CChildFrame* pFrame = pTCP->m_pFrame;
ASSERT(pFrame != NULL);
try
{
if(pFrame->m_strDBMS.CompareNoCase(g_szSQLAnyWhere) != 0)
{
if((pFrame->m_pSet != NULL &&
pFrame->m_pSet->m_hstmt != SQL_NULL_HSTMT) ||
(pFrame->m_pThreadExecuteSQL != NULL &&
AfxIsValidAddress(pFrame->m_pThreadExecuteSQL,
sizeof(pFrame->m_pThreadExecuteSQL))) &&
pFrame->m_pThreadExecuteSQL->m_hThread != NULL)
{
pFrame->m_pSet->Cancel();
}
}
}
catch(...)
{
TRACE(_T("Errors occurred.\n"));
}
pFrame->m_bCanceled = TRUE;
if(::IsWindow(pFrame->m_hWnd))
::PostMessage(pFrame->m_hWnd, WM_CANCEL_COMPLETE, 0L, 0L);
return 0L;
}
LONG CChildFrame::OnCancelComplete(UINT wParam, LONG lParam)
{
UNUSED_ALWAYS(wParam);
UNUSED_ALWAYS(lParam);
if(::IsWindow(m_hWnd))
CloseAfterCancelIfNecessary();
return 0L;
}
void CChildFrame::CloseAfterCancelIfNecessary()
{
if(m_bCloseMainFrameAfterCancel)
{
m_bCloseMainFrameAfterCancel = FALSE;
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
if(pFrame != NULL)
pFrame->SendMessage(WM_CLOSE);
}
else if(m_bDisconnectAllAfterCancel)
{
m_bDisconnectAllAfterCancel = FALSE;
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
if(pFrame != NULL)
pFrame->OnFileDisconnectAll();
}
else if(m_bCloseAfterCancel)
{
m_bCloseAfterCancel = FALSE;
SendMessage(WM_CLOSE);
}
}
void CChildFrame::CancelQuery()
{
if(!m_bExecuting)
CloseAfterCancelIfNecessary();
else if(!m_bCanceling && !m_bExecuteCompleteHandled)
{
m_bCanceling = TRUE;
m_strPaneTextZero = _T("Attempting to cancel query batch...");
m_wndStatusBar.SetPaneText(0, m_strPaneTextZero);
m_TCP.m_pFrame = this;
m_pThreadCancelSQL = AfxBeginThread(CancelSQLProc, &m_TCP);
ASSERT(m_pThreadCancelSQL != NULL);
m_pThreadCancelSQL->m_pMainWnd = this;
}
}
void CChildFrame::OnSqlCancel()
{
m_bCloseAfterCancel = FALSE;
CancelQuery();
}
void CChildFrame::OnUpdateSqlCancel(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_database.IsOpen() && m_bExecuting
&& !m_bCanceling && m_bSQLCancelSupported);
}
void CChildFrame::OnUpdateCombo(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_database.IsOpen() &&
m_bIsTSQLSupported && !m_bExecuting);
}
BOOL CChildFrame::SelectDatabase()
{
BOOL bRet = TRUE;
try
{
CloseRecordset();
if(!m_pSet->ExecDirect(CString("select db_name() dbname")))
bRet = FALSE;
else
{
if(!m_pSet->IsBOF())
{
m_pSet->GetFieldValue((short)0, m_strDBName);
m_comboBox.SelectString(-1, m_strDBName);
}
}
CloseRecordset();
}
catch(CDBException* e)
{
bRet = FALSE;
if(e)
{
CString sMsg;
if(!e->m_strError.IsEmpty())
sMsg.Format(_T("%s%s"), (LPCTSTR)e->m_strError, (LPCTSTR)e->m_strStateNativeOrigin);
else
sMsg = e->m_strStateNativeOrigin;
e->Delete();
TRACE(_T("%s\n"), (LPCTSTR)sMsg);
}
}
return bRet;
}
void CChildFrame::SetExecutionTime()
{
DWORD dwDiff = ::GetTickCount() - m_dwStart;
COleDateTimeSpan span(0, 0, 0, dwDiff/1000);
CString sBuff;
sBuff.Format(_T("Exec time: %02d:%02d:%02d"),
span.GetHours(), span.GetMinutes(), span.GetSeconds());
m_strExecutionTime = sBuff;
m_wndStatusBar.SetPaneText(1, m_strExecutionTime);
}
void CChildFrame::OnUpdateFileMruFile1(CCmdUI* pCmdUI)
{
theApp.OnUpdateFileMruFile1(pCmdUI);
}
int CChildFrame::GetThreadPriority(const CString& sPriority)
{
int nThreadPriority = THREAD_PRIORITY_LOWEST;
if(!sPriority.CompareNoCase(g_szTimeCritical))
nThreadPriority = THREAD_PRIORITY_TIME_CRITICAL;
else if(!sPriority.CompareNoCase(g_szHighest))
nThreadPriority = THREAD_PRIORITY_HIGHEST;
else if(!sPriority.CompareNoCase(g_szAboveNormal))
nThreadPriority = THREAD_PRIORITY_ABOVE_NORMAL;
else if(!sPriority.CompareNoCase(g_szNormal))
nThreadPriority = THREAD_PRIORITY_NORMAL;
else if(!sPriority.CompareNoCase(g_szBelowNormal))
nThreadPriority = THREAD_PRIORITY_BELOW_NORMAL;
else if(!sPriority.CompareNoCase(g_szLowest))
nThreadPriority = THREAD_PRIORITY_LOWEST;
else
nThreadPriority = THREAD_PRIORITY_LOWEST;
return nThreadPriority;
}
void CChildFrame::SetCommandTimeOut(const int& lCmdTimeOut)
{
if(m_database.IsOpen())
{
try
{
m_database.SetQueryTimeout(lCmdTimeOut);
m_nQueryTimeOut = lCmdTimeOut;
}
catch(...)
{
TRACE(_T("Errors occurred.\n"));
}
}
}
void CChildFrame::SetCacheSize(const int& nCacheSize)
{
if(m_pSet != NULL)
{
try
{
#ifdef _SET_ROWSET_SIZE
if(!m_bExecuting)
{
m_pSet->SetRowsetSize(nCacheSize);
m_nCacheSize = nCacheSize;
}
#endif
}
catch(...)
{
TRACE(_T("Errors occurred.\n"));
}
}
}
void CChildFrame::SetThreadPriority(const CString& sThreadPriority)
{
m_nThreadPriority = GetThreadPriority(sThreadPriority);
}
LONG CChildFrame::OnExecProcComplete(UINT wParam, LONG lParam)
{
UNUSED_ALWAYS(lParam);
OnExecutionComplete(wParam, lParam);
m_bProcText = FALSE;
return 0L;
}
LONG CChildFrame::OnExecutionComplete(UINT wParam, LONG lParam)
{
UNUSED_ALWAYS(lParam);
if(::IsWindow(m_hWnd) && !m_bExecuteCompleteHandled)
{
m_bExecuteCompleteHandled = TRUE;
CloseRecordset();
if(m_bProcText)
{
if(m_strProcText.length())
{
if(m_strStatusText.CompareNoCase(g_szMessages) != 0)
{
m_pResultView->GetRichEditCtrl().SetWindowText(m_strProcText.c_str());
m_pResultView->GetRichEditCtrl().SetSel(0, 0); // Scroll up!
m_strStatusText.Format(
_T("# of lines of text: %d"),
m_pResultView->GetRichEditCtrl().GetLineCount()
);
m_pResultView->Invalidate();
m_pResultView->UpdateWindow();
}
}
}
else
{
if(m_nGridCount)
{
SetResultSetInfo();
m_ThreadParam.m_pGridCtrl->SetRedraw(TRUE);
m_ThreadParam.m_pGridCtrl->Invalidate();
m_ThreadParam.m_pGridCtrl->UpdateWindow();
m_pResultView->SetRedraw(TRUE);
m_pResultView->Invalidate();
m_pResultView->UpdateWindow();
if(m_strStatusText.CompareNoCase(g_szMessages) != 0)
m_ThreadParam.m_pGridCtrl->ShowWindow(SW_SHOW);
}
}
if(m_bCanceled)
{
m_strPaneTextZero = "Query batch canceled";
if(m_strMessages.length())
m_strMessages += _T("\n\n");
m_strMessages += _T("Query canceled by user.");
}
else if(wParam)
m_strPaneTextZero = "Query batch completed with errors";
else
{
m_strPaneTextZero = "Query batch completed";
if(m_bProcText)
{
if(!m_strProcText.length() && !m_strMessages.length())
m_strMessages = _T("The command(s) completed successfully.");
}
else
{
if(!m_nGridCount && !m_strMessages.length())
m_strMessages = _T("The command(s) completed successfully.");
}
}
m_wndStatusBar.SetPaneText(0, m_strPaneTextZero);
if(m_bProcText)
{
if(!m_strProcText.length() && m_strStatusText.CompareNoCase(g_szMessages) != 0)
OnViewMessages();
}
else
{
if(!m_nGridCount && m_strStatusText.CompareNoCase(g_szMessages) != 0)
OnViewMessages();
}
SetExecutionTime();
if(m_nTimerID != 0)
{
KillTimer(m_nTimerID);
m_nTimerID = 0;
}
m_bExecuting = FALSE;
m_bCanceling = FALSE;
m_bCanceled = FALSE;
bool bOkUI = true;
if(m_bCloseMainFrameAfterCancel)
bOkUI = false;
if(m_bDisconnectAllAfterCancel)
bOkUI = false;
if(m_bCloseAfterCancel)
bOkUI = false;
if(bOkUI)
{
LONG lIdle = 0;
while(AfxGetApp()->OnIdle(lIdle++));
}
}
return 0L;
}
LONG CChildFrame::OnGetGridCtrl(UINT wParam, LONG lParam)
{
LRESULT lResult = 0L;
if(::IsWindow(m_hWnd))
{
UNUSED_ALWAYS(lParam);
CMSFlexGrid* pGridCtrl = NULL;
try
{
pGridCtrl = new CMSFlexGrid();
ASSERT(pGridCtrl != NULL);
m_ThreadParam.m_pGridCtrl = NULL;
m_ThreadParam.m_ptrGridCtrl = NULL;
CRect rect(0, 0, 0, 0);
m_pResultView->GetWindowRect(&rect);
if(!pGridCtrl->Create(g_lpszProgID, WS_CHILD | WS_VISIBLE | WS_HSCROLL |
WS_VSCROLL, rect, m_pResultView, m_nID++, NULL, FALSE,
L"72E67120-5959-11cf-91F6-C2863C385E30"))
{
TRACE(_T("Failed to create GridCtrl.\n"));
lResult = -1L;
}
else
{
pGridCtrl->ShowWindow(SW_HIDE);
m_GridList.AddTail(pGridCtrl);
m_nGridCount = m_GridList.GetCount();
if(m_nGridCount == 1)
if(GetFocus() == m_pResultView)
m_pQryView->SetFocus();
m_ThreadParam.m_pGridCtrl = pGridCtrl;
m_ThreadParam.m_ptrGridCtrl = pGridCtrl->GetControlUnknown();
if(m_nIndex == 0)
m_pResultView->m_pGridCtrl = pGridCtrl;
m_ThreadParam.m_ptrGridCtrl->put_BackColorBkg(
(unsigned long)RGB(255, 255, 255)
);
m_ThreadParam.m_ptrGridCtrl->put_BorderStyle(
MSFlexGridLib::flexBorderNone
);
m_ThreadParam.m_ptrGridCtrl->put_AllowUserResizing(
MSFlexGridLib::flexResizeColumns
);
m_ThreadParam.m_ptrGridCtrl->put_FocusRect(
MSFlexGridLib::flexFocusNone
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -