📄 itapi.cpp
字号:
nReplacements++;
}
}
return nReplacements;
}
// @func Finds the last occurance of the specified substring.
//
// @parm Null-terminated string to search.
// @parm Null-terminated string to search for.
//
// @rdesc A pointer to the last occurance of the substring or NULL
// if the substring cannot be found.
//
// @comm This function is in the ITCLIB namespace.
LPCTSTR ITCLIB::ReverseFindString(LPCTSTR lpszString, LPCTSTR lpszSub)
{
LPCTSTR lpszFindLast = strstr(lpszString, lpszSub);
LPCTSTR lpszFindNext = lpszFindLast;
while (lpszFindNext)
{
lpszFindNext = strstr(lpszFindLast + strlen(lpszSub), lpszSub);
if (lpszFindNext)
lpszFindLast = lpszFindNext;
}
return lpszFindLast;
}
// @func Returns the string after the last occurance of the
// specified separator.
//
// @parm The string to search.
// @parm Ths string to search for.
//
// @rdesc The string after the last occurance of the separator.
//
// @comm This function is in the ITCLIB namespace.
CString ITCLIB::GetStringAfterSeperator(CString strValue, CString strSeperator)
{
CString strAfter = strValue;
LPCTSTR lpszEndOfValue = ReverseFindString(strValue, strSeperator);
if (lpszEndOfValue)
{
strValue = lpszEndOfValue;
int nLen = strValue.GetLength();
int nSepLen = strSeperator.GetLength();
strAfter = strValue.Right(nLen - nSepLen);
}
return strAfter;
}
#ifdef _DEBUG
// @func Outputs a description of the specified window to <t afxDump>.
// The description can include the window class, its handle value, and
// its caption.
//
// @parm The <c CWnd> of the window.
// @parm The level of indentation to output before the description.
// A tab character will be output for each level.
//
// @comm This function is in the ITCLIB namespace.
void ITCLIB::TraceWindow(CWnd* pWnd, int nLevel)
{
ASSERT(pWnd != NULL);
// Indent
for (int i=0; i < nLevel; i++)
afxDump << "\t";
CRuntimeClass* pRuntime = pWnd->GetRuntimeClass();
if (pRuntime == NULL)
{
afxDump << "a window";
}
else
{
afxDump << "a " << pWnd->GetRuntimeClass()->m_lpszClassName;
}
afxDump << " with HWND";
afxDump << " " << (HWND)*pWnd;
CString strLabel;
pWnd->GetWindowText(strLabel);
if (!strLabel.IsEmpty())
afxDump << " '" << strLabel << "'";
afxDump << "\n";
}
// @func This function outputs a description of the specified parent,
// its children, and optionally all descendent children using <f TraceWindow>.
//
// @parm The <c CWnd> of the parent window.
// @parm If nonzero, all descendent children will be output.
// @parm The indentation level; should be 0 when enumerating the parent.
//
// @comm This function is in the ITCLIB namespace.
void ITCLIB::TraceWindowChildren(CWnd* pParentWnd, BOOL bRecursive, int nLevel)
{
ASSERT_VALID(pParentWnd);
if (nLevel == 0)
{
afxDump << "Enumerating children of ";
TraceWindow(pParentWnd, 0);
nLevel++;
}
// Enumerate children
for (HWND hWndChild = ::GetTopWindow(*pParentWnd); hWndChild != NULL;
hWndChild = ::GetNextWindow(hWndChild, GW_HWNDNEXT))
{
CWnd* pChildWnd = CWnd::FromHandle(hWndChild);
TraceWindow(pChildWnd, nLevel);
if (bRecursive)
TraceWindowChildren(pChildWnd, TRUE, nLevel+1);
}
}
#endif // _DEBUG
// @func Shows the enables the specified window using <f ShowWindow> and
// <f EnableWindow>. If the window is hidden it is automatically
// disabled.
//
// @syntax BOOL ShowAndEnableWindow(HWND hWnd, BOOL bShow, BOOL bEnable);
// @syntax BOOL ShowAndEnableWindow(CWnd* pChild, BOOL bShow, BOOL bEnable);
// @syntax BOOL ShowAndEnableWindow(CWnd* pParent, UINT nIDC, BOOL bShow, BOOL bEnable);
//
// @parm HWND | hWnd | The window handle of the window.
// @parm CWnd* | pChild | The <c CWnd> of the window.
// @parm CWnd* | pParent | The <c CWnd> of the parent window.
// @parm UINT | nIDC | The ID of the child window.
// @parm BOOL | bShow | Nonzero to show the window; 0 to hide it.
// @parm BOOL | bEnable | Nonzero to enable the window; 0 to disable it.
//
// @rdesc Nonzero if successful; otherwise 0.
//
// @comm This function is in the ITCLIB namespace.
BOOL ITCLIB::ShowAndEnableWindow(HWND hWnd, BOOL bShow, BOOL bEnable)
{
BOOL bSuccess = FALSE;
// Always disable hidden windows to keep the control from
// getting the focus and to disable any acclerators.
if (!bShow)
bEnable = FALSE;
if (hWnd)
{
::ShowWindow(hWnd, (bShow) ? SW_SHOW : SW_HIDE);
::EnableWindow(hWnd, bEnable);
bSuccess = TRUE;
}
return bSuccess;
}
BOOL ITCLIB::ShowAndEnableWindow(CWnd* pChild, BOOL bShow, BOOL bEnable)
{
if (pChild)
return ShowAndEnableWindow(pChild->GetSafeHwnd(), bShow, bEnable);
return FALSE;
}
BOOL ITCLIB::ShowAndEnableWindow(CWnd* pParent, UINT nIDC, BOOL bShow, BOOL bEnable)
{
ASSERT_VALID(pParent);
ASSERT(::IsWindow(*pParent));
if (!pParent || !::IsWindow(*pParent))
{
TRACE("The parent window is invalid\n");
return FALSE;
}
CWnd* pChild = pParent->GetDlgItem(nIDC);
if (!pChild)
{
TRACE("Failed to find a child control with ID %08x\n", nIDC);
return FALSE;
}
return ShowAndEnableWindow(pChild, bShow, bEnable);
}
// @func This function calls <f ShowAndEnableWindow> to show and
// enable the next sibling window of the specified child.
//
// @parm The <c CWnd> of the parent window.
// @parm The ID of the child window.
// @parm Nonzero to show the window; 0 to hide it.
// @parm Nonzero to enable the window; 0 to disable it.
//
// @rdesc Nonzero if successful; otherwise 0.
//
// @comm This function is in the ITCLIB namespace.
//
// @xref <f ShowAndEnableWindow> <f ShowPrevDialogSibling>
BOOL ITCLIB::ShowNextDialogSibling(CWnd* pParent, UINT nIDC,
BOOL bShow, BOOL bEnable)
{
ASSERT_VALID(pParent);
ASSERT(::IsWindow(*pParent));
if (!pParent || !::IsWindow(*pParent))
{
TRACE("The parent window is invalid\n");
return FALSE;
}
CWnd* pChild = pParent->GetDlgItem(nIDC);
if (!pChild)
{
TRACE("Failed to find a child control with ID %08x\n", nIDC);
return FALSE;
}
CWnd* pNext = pChild->GetWindow(GW_HWNDNEXT);
if (!pNext)
{
TRACE("The child control with ID %08x does not have a next sibling\n", nIDC);
return FALSE;
}
return ShowAndEnableWindow(pNext, bShow, bEnable);
}
// @func This function calls <f ShowAndEnableWindow> to show and
// enable the previous sibling window of the specified child.
//
// @parm The <c CWnd> of the parent window.
// @parm The ID of the child window.
// @parm Nonzero to show the window; 0 to hide it.
// @parm Nonzero to enable the window; 0 to disable it.
//
// @rdesc Nonzero if successful; otherwise 0.
//
// @comm This function is in the ITCLIB namespace.
//
// @xref <f ShowAndEnableWindow> <f ShowNextDialogSibling>
BOOL ITCLIB::ShowPrevDialogSibling(CWnd* pParent, UINT nIDC,
BOOL bShow, BOOL bEnable)
{
ASSERT_VALID(pParent);
ASSERT(::IsWindow(*pParent));
if (!pParent || !::IsWindow(*pParent))
{
TRACE("The parent window is invalid\n");
return FALSE;
}
CWnd* pChild = pParent->GetDlgItem(nIDC);
if (!pChild)
{
TRACE("Failed to find a child control with ID %08x\n", nIDC);
return FALSE;
}
CWnd* pPrev = pChild->GetWindow(GW_HWNDPREV);
if (!pPrev)
{
TRACE("The child control with ID %08x does not have a next sibling\n", nIDC);
return FALSE;
}
return ShowAndEnableWindow(pPrev, bShow, bEnable);
}
// @func This functions uses <f ShowAndEnableWindow> to show and enable
// dialog controls as well as their associated static controls (usually
// used as a label).
//
// @parm The <c CWnd> of the parent window.
// @parm The ID of the child window.
// @parm Nonzero to show the window; 0 to hide it.
// @parm Nonzero to enable the window; 0 to disable it.
// @parm Nonzero to automatically change the state of the previous
// static control window when necessary.
//
// @rdesc Nonzero if successful; otherwise 0.
//
// @comm This function is in the ITCLIB namespace.
//
// @xref <f ShowAndEnableWindow>
BOOL ITCLIB::ShowDialogControl(CWnd* pParent, UINT nIDC,
BOOL bShow, BOOL bEnable, BOOL bSmart)
{
ASSERT_VALID(pParent);
ASSERT(::IsWindow(*pParent));
if (!pParent || !::IsWindow(*pParent))
{
TRACE("The parent window is invalid\n");
return FALSE;
}
CWnd* pChild = pParent->GetDlgItem(nIDC);
if (!pChild)
{
TRACE("Failed to find a child control with ID %08x\n", nIDC);
return FALSE;
}
VERIFY(ShowAndEnableWindow(pChild, bShow, bEnable));
// Setting this to TRUE will cause the immediate
// siblings of this child to be hidden if they
// match a specific set of criteria.
BOOL bHideSiblings = FALSE;
if (IsEditControl(*pChild))
{
// Definitely edit controls
bHideSiblings = TRUE;
}
else if ((pChild->SendMessage(WM_GETDLGCODE) & DLGC_BUTTON) == 0)
{
// Assume anything other than buttons are safe
bHideSiblings = TRUE;
}
if (bSmart && bHideSiblings)
{
CWnd* pPrev = pChild->GetWindow(GW_HWNDPREV);
// Hide the label for the edit contol
if (pPrev && IsStaticControl(*pPrev))
{
VERIFY(ShowAndEnableWindow(pPrev, bShow, bEnable));
}
}
return TRUE;
}
// @func Sends a message to the owner of the specified window. A message
// is not sent if the window does not have an owner.
//
// @parm The window handle of the window.
// @parm Specifies the message to send.
// @parm Specifies additional message-specific information.
// @parm Specifies additional message-specific information.
//
// @rdesc The results of <f SendMessage> if successful; otherwise -1.
//
// @comm This function is in the ITCLIB namespace.
LRESULT ITCLIB::SendMessageToOwner(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
ASSERT(::IsWindow(hWnd));
HWND hwndOwner = AfxGetParentOwner(hWnd);
if (!hwndOwner)
{
TRACE("The specified window does not have an owner\n");
return -1;
}
return ::SendMessage(hwndOwner, nMsg, wParam, lParam);
}
// @func This function uses <f SendMessageToOwner> to send a
// notification to the owner of a child control. Notification codes
// are sent to the parent in a WM_COMMAND message and <t NMHDR>
// notifications are sent in a WM_NOTIFY message.
//
// @syntax LRESULT SendNotifyToOwner(HWND hWnd, WORD wNotifyCode);
// @syntax LRESULT SendNotifyToOwner(HWND hWnd, LPNMHDR pnmh);
// @syntax LRESULT SendNotifyToOwner(CWnd* pControl, WORD wNotifyCode);
// @syntax LRESULT SendNotifyToOwner(CWnd* pControl, LPNMHDR pnmh);
//
// @parm HWND | hWnd | The window handle of the window.
// @parm CWnd* | pControl | The <c CWnd> of the window.
// @parm WORD | wNotifyCode | The notification code such as BN_CLICKED.
// @parm LPNMHDR | pnmh | A pointer to a <t NMHDR> structure.
//
// @rdesc The results of <f SendMessageToOwner>.
//
// @comm This function is in the ITCLIB namespace.
//
// @xref <f SendMessageToOwner>
LRESULT ITCLIB::SendNotifyToOwner(HWND hWnd, WORD wNotifyCode)
{
int nCtrlID = ::GetDlgCtrlID(hWnd);
WPARAM wParam = MAKEWPARAM(nCtrlID, wNotifyCode);
LPARAM lParam = (LPARAM)hWnd;
return SendMessageToOwner(hWnd, WM_COMMAND, wParam, lParam);
}
LRESULT ITCLIB::SendNotifyToOwner(HWND hWnd, LPNMHDR pnmh)
{
int nCtrlID = ::GetDlgCtrlID(hWnd);
WPARAM wParam = (WPARAM)nCtrlID;
LPARAM lParam = (LPARAM)pnmh;
return SendMessageToOwner(hWnd, WM_NOTIFY, wParam, lParam);
}
LRESULT ITCLIB::SendNotifyToOwner(CWnd* pControl, WORD wNotifyCode)
{
ASSERT(pControl != NULL);
if (!pControl)
{
TRACE("The control pointer is invalid\n");
return -1;
}
// Delegate
return SendNotifyToOwner(pControl->m_hWnd, wNotifyCode);
}
LRESULT ITCLIB::SendNotifyToOwner(CWnd* pControl, LPNMHDR pnmh)
{
ASSERT(pControl != NULL);
if (!pControl)
{
TRACE("The control pointer is invalid\n");
return -1;
}
// Delegate
return SendNotifyToOwner(pControl->m_hWnd, pnmh);
}
// @func Outputs a field and its associated data to a dump context.
//
// @syntax void DumpField(CDumpContext& dc, LPCTSTR lpszField, BOOL bValue);
// @syntax void DumpField(CDumpContext& dc, LPCTSTR lpszField, CString strValue);
//
// @parm CDumpContext& | dc | A <c CDumpContext> to output to.
// @parm LPCTSTR | lpszField | The name of the field.
// @parm BOOL | bValue | The field boolean value.
// @parm CString | strValue | The field string value.
//
// @comm This function is in the ITCLIB namespace.
void ITCLIB::DumpField(CDumpContext& dc, LPCTSTR lpszField, CString strValue)
{
const int MAX_LEN = 60;
PrettyString(strValue, MAX_LEN);
// Output the field name
dc << lpszField;
if (strValue.IsEmpty())
{
dc << _T("<Empty>\r\n");
return;
}
if (strValue.GetLength() <= MAX_LEN)
{
dc << strValue << _T("\r\n");
return;
}
int nChar = 0;
TCHAR sz[2] = _T(" ");
BOOL bNeedNewline = FALSE;
const CString strLead = _T("\t");
const CString strEndline = _T("\r\n");
// Output the first line
for (; nChar < strValue.GetLength(); nChar++)
{
if (strValue[nChar] != _T('\n'))
{
sz[0] = strValue[nChar];
dc << sz;
bNeedNewline = TRUE;
}
else
{
dc << strEndline;
bNeedNewline = FALSE;
break;
}
}
if (bNeedNewline)
dc << strEndline;
bNeedNewline = FALSE;
// The rest of the lines are indented
while (nChar < strValue.GetLength())
{
dc << strLead;
for (nChar++; nChar < strValue.GetLength(); nChar++)
{
if (strValue[nChar] != _T('\n'))
{
sz[0] = strValue[nChar];
dc << sz;
bNeedNewline = TRUE;
}
else
{
dc << strEndline;
bNeedNewline = FALSE;
break;
}
}
if (bNeedNewline)
dc << strEndline;
bNeedNewline = FALSE;
}
}
void ITCLIB::DumpField(CDumpContext& dc, LPCTSTR lpszField, BOOL bValue)
{
DumpField(dc, lpszField, (bValue) ? "Yes" : "No");
}
// @func Work in progress.
//
// @comm This function is in the ITCLIB namespace.
CString ITCLIB::AppProfileFindPath(CWinApp* pApp, LPCTSTR lpszSection,
LPCTSTR lpszBasePathEntry, LPCTSTR lpszFileEntry,
LPCTSTR lpszBaseDefault, LPCTSTR lpszFileDefault)
{
ASSERT(pApp != NULL);
ASSERT(lpszSection != NULL);
ASSERT(lpszFileEntry != NULL);
CString strFile;
CString strFullPath;
CString strBasePath;
strFile = pApp->GetProfileString(lpszSection,
lpszFileEntry, lpszFileDefault);
if (lpszBasePathEntry != NULL)
strBasePath = pApp->GetProfileString(lpszSection,
lpszBasePathEntry, lpszBaseDefault);
if (!strFile.IsEmpty())
{
if (!strBasePath.IsEmpty())
strFile = strBasePath + strFile;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -