VC下有用的几个宏

Panda
//贴几个自认为有用的宏,有自己写的,也由down的。若有问题,可回帖或发站内信件与
//sarge(网际浪子)联系
//粘贴到openlab的发表文章文本框发现文字缩进不正确--但没有任何问题
一、计算制定字符串在选定范围中出现的次数--演示常用字符串操作
Sub GetStringAmount()
'DESCRIPTION: get account of string----------case-insensitive.
strRest = ActiveDocument.Selection
If strRest = "" Then
MsgBox "NO SELECTION!" + VbLf + VbLf + "Select range of search first.If the whole document is the range,use CTRL+A."
Exit Sub
End If
Amount = 0
Word = "Input words here!"
Word = InputBox("To account what string?","Input here",Word)
Word = LCase(Word)
WordLen = Len(Word)
For i= 1 to Len(strRest)
If LCase(Mid(strRest,i,WordLen)) = Word Then
Amount = Amount + 1
End If
Next
MsgBox("String """ + Word + """ appears" + CStr(Amount) + "time(s).")
End Sub
二、光标定位到上一函数代码--简短有效的宏
Sub GoToFunctionHeaderUp()
'DESCRIPTION: moves cursor to the previous function definition
'TODO: Put macro code here
ActiveDocument.Selection.FindText "^{",dsMatchBackward + dsMatchRegExp
ActiveDocument.Selection.Lineup
ActiveDocument.Selection.StartOfLine
End Sub
//要求:函数头部之后应该有一换行、下一行开头是'{',函数体代码中的“{”都应该有缩进
//--要有良好的代码风格,如
main()
{
//To do :add your code here
{//缩进
//your code...
}
}
三、光标定位到下一函数代码
Sub GoToFunctionHeaderDown()
'DESCRIPTION: Moves cursor to the next function definition.
'TODO: Put macro code here
ActiveDocument.Selection.LineDown dsMove,2
ActiveDocument.Selection.FindText "^{",dsMatchRegExp
ActiveDocument.Selection.LineUp
ActiveDocument.Selection.StartOfLine
End Sub
//我将二、三定义到快捷键CTRL+PageUp和CTRL+PageDown,非常好用--推荐
四、打开代码文件对应的头文件或者头文件的代码文件--长了一点,但功能齐全
Sub ToggleSourceAndHeader()
'DESCRIPTION: Toggle between the active source and header file.
sCurrent = LCase (ActiveDocument.FullName)
select case right(sCurrent, 3)
case "cpp"
DocStr = Replace (sCurrent, ".cpp", ".hpp", 1, 1)
DocAltStr = Replace (sCurrent, ".cpp", ".h", 1, 1)
DocName = Replace (sCurrent, ".cpp", "", 1, 1)
DocType = "header"
case "hpp"
DocStr = Replace (sCurrent, ".hpp", ".cpp", 1, 1)
DocAltStr = Replace (sCurrent, ".hpp", ".c", 1, 1)
DocName = Replace (sCurrent, ".hpp", "", 1, 1)
DocType = "source"
end select
select case right(sCurrent, 1)
case "c"
DocStr = Replace (sCurrent, ".c", ".h", 1, 1)
DocAltStr = Replace (sCurrent, ".c", ".hpp", 1, 1)
DocName = Replace (sCurrent, ".c", "", 1, 1)
DocType = "header"
case "h"
DocStr = Replace (sCurrent, ".h", ".c", 1, 1)
DocAltStr = Replace (sCurrent, ".h", ".cpp", 1, 1)
DocName = Replace (sCurrent, ".h", "", 1, 1)
DocType = "source"
end select
'try to open the default
on error resume next
Documents.Open DocStr
if err.number = 0 then exit sub
'if not found try to open the alternative
on error resume next
Documents.Open DocAltStr
if err.number = 0 then exit sub
if msgbox("Error during the loading of the " & _
DocType & " of '" & DocName & "'." & _
vbCr & "Search for alternative ?" , vbExclamation + vbYesNo, "Error") = vbYes Then
'Find an alternative
ActiveDocument.Selection.StartOfDocument
sShortCurrent = LCase (ActiveDocument.Name)
sShortCurrent = Left(sShortCurrent, Instr(sShortCurrent, ".")-1)
do
nStartLine = ActiveDocument.Selection.CurrentLine
ActiveDocument.Selection.FindText "#include", dsMatchForward + dsMatchCase + dsMatchRegExp
if nStartLine >= ActiveDocument.Selection.CurrentLine then
msgbox "I found no alternative, I'm only a stupid computer...", vbInformation, "Oops!"
Exit Do
end if
ActiveDocument.Selection.CharRight
ActiveDocument.Selection.EndOfLine dsExtend
'if one is found ask the user to open it...
if instr(ActiveDocument.Selection, sShortCurrent) > 0 then
if msgbox("Open " & ActiveDocument.Selection & "?", vbInformation + vbYesNo, "Question") = vbYes then
DocAlt = ActiveDocument.Selection.Text
DocAlt = mid(DocAlt, Instr(DocAlt, Chr(34))+1)
DocAlt = left(DocAlt, Instr(DocAlt, Chr(34))-1)
Documents.Open ActiveDocument.Path & "\" & DocAlt
Exit Do
end if
end if
loop
end if
End Sub
五、增加头文件的单次包含宏
//其中的JudgeFileType()是一个判断文件类型的函数,与VC自带的Sample.dsm中的Function FileType //(ByVal doc)类似,故没有列出
//对头文件header.h,在文件的开头增加以下两行:
//#ifndef __HEADER_H__
//#define __HEADER_H__
//在header.h的末尾增加:
//#endif/*__HEADER_H__*/
Sub AddHeadFileMacros()
'DESCRIPTION: 增加头文件的#ifndef……#define……#endif宏
'TODO: Put macro code here
Retval = JudgeFileType()
If RetVal <> HFileType Then
MyVar = MsgBox("This file is not a .h file." + VbLf + _
"The macro will not execute.",vbCritical,"Error Information")
Exit Sub
End If
Dim FileName,NameTag
Dim Output
Dim c
c = 0
FileName = ActiveDocument.Name
DotPos = Instr(1,FileName,".")
Do While ( DotPos <> 0 And FileName <> "" )
NameTag = Left(FileName,DotPos - 1)
NameTag = NameTag + "_"
FileName = Right(FileName,Len(FileName) - DotPos )
DotPos = Instr(1,FileName,".")
c = c + 1
If c >= 10 Then
Exit Sub
End If
Loop
NameTag = "__" + NameTag + FileName + "__"
NameTag = UCase(NameTag)
ActiveDocument.Selection.SelectAll
Output = "#ifndef " + NameTag + VbLf + _
+ "#define " + NameTag + VbLf + _
+ VbLf + _
+ ActiveDocument.Selection + _
+ VbLf + _
+ "#endif /*" + NameTag + "*/"
ActiveDocument.Selection = Output
End Sub
※编辑: sarge 于 2002-1-27 11:09:56 在 [202.117.84.248] 编辑本文

走吧,走吧
为自己的心找一个家
也曾伤心流泪
也曾黯然心碎
这是爱的代价