9樓 巨大八爪鱼
2015-11-28 14:35
程序核心代碼:(鼠標點擊OK按鈕後執行的內容) void CInsertStrDlg::OnBnClickedOk() { // TODO: Add your control notification handler code here //CDialogEx::OnOK(); int len1, len2, i, pos; UpdateData(); len1 = mString1.GetLength(); len2 = mString2.GetLength();
/* Find the position */ for (pos = 0; pos < len1; pos++) { if (mString1.GetAt(pos) == mString2.GetAt(0)) break; // found; } // When not found, pos == len1
/* Move */ mResult = mString1 + CString(' ', len2 + 1); // lengthen the string if (pos != len1) { for (i = len1; i >= pos - 1 && i >= 0; i--) { // At first when i == len1, the character is '\0' // and then '\0' will be moved to the end mResult.SetAt(i + len2 - 1, mString1.GetAt(i)); } }
/* Copy chars from s2 to s1 */ // copy the first character of s2 if (pos == len1) mResult.SetAt(len1 + len2, '\0'); // when not found, the moving wasn't executed, so '\0' was not copied for (i = 0; i < len2; i++) mResult.SetAt(pos + i, mString2.GetAt(i));
UpdateData(false); }
其中mString1綁定的是第一個文本框,mString2是第二個文本框,mResult是第三個只讀的文本框。
|