글
Technical Article/펌 2004. 5. 6. 13:57Macro to Add New Classes to Visual C++ Projects
'------------------------------------------------------------------------------
'FILE DESCRIPTION: A description was not provided.
'------------------------------------------------------------------------------
Function GetProjectPath(ByVal proj)
dim szProjectPath
dim nIndex
dim szReturn
szProjectPath = proj.FullName
nIndex = InStrRev(szProjectPath, "\")
if (nIndex <> 0) then
szReturn = Left(szProjectPath, nIndex)
end if
GetProjectPath = szReturn
End Function
Sub ClassGenerator
'DESCRIPTION: Creates minimal source/header files for a
' class, given its input name.
On Error Resume Next
dim szClassName
dim szClassRoot ' classname minus any MFC-style prefix
dim szSourceFile
dim szHeaderFile
' used to determine full paths for the added
' source/header file
dim szProjectPath
dim szTempPath
dim szHeaderPreprocessor, szHeaderClassBegin
dim szFirstChar, szSecondChar
' make sure the project is valid
if ActiveProject.Type <> "Build" then
MsgBox "This project is not valid. Ending macro."
Exit Sub
end if
' enter the class name
szClassName = InputBox("Enter the class name:", "Class Name")
if (len(szClassName) <= 0) then
MsgBox "Invalid class name. Ending macro."
Exit Sub
end if
' generate file names based on the input
' class name
szSourceFile = szClassName + ".cpp"
szHeaderFile = szClassName + ".h"
' test for MFC style class names [ie: CClassName's
' file should be ClassName.h"] all we do is check the
' first to characters; if they're both capitals, we
' don't use the first one
szFirstChar = Left(szClassName, 1)
szSecondChar = Mid(szClassName, 2, 1)
if ( ((Asc(szFirstChar) >= 65) and (Asc(szFirstChar) <= 90)) _
and ((Asc(szSecondChar) >= 65) and (Asc(szSecondChar) <= 90)) ) _
then
szSourceFile = Mid(szSourceFile, 2)
szHeaderFile = Mid(szHeaderFile, 2)
szClassRoot = Mid(szClassName, 2)
else
szClassRoot = szClassName
end if
' add the files to the project -- if they can't be added,
' Resume Next will see to it that we skip past this
ActiveProject.AddFile szSourceFile
ActiveProject.AddFile szHeaderFile
' get the project path
szProjectPath = GetProjectPath(ActiveProject)
' now add the header file to the hard drive
Documents.Add "Text"
ActiveDocument.Selection.StartOfDocument
ActiveDocument.Selection = "#ifndef " & UCase(szClassRoot) _
& "_H_" & vbCrLf & _
"#define " & UCase(szClassRoot) & "_H_" & vbCrLf & _
vbCrLf & _
"class " & szClassName & vbCrLf & _
"{" & vbCrLf & _
"public: // object creation/destruction" & vbCrLf & _
" " & szClassName & "();" & vbCrLf & _
" " & szClassName & "(const " & szClassName & "& source);" _
& vbCrLf & _
" " & szClassName & "& " & "operator=(const " & szClassName _
& "& right);" & vbCrLf & _
" virtual " & "~" & szClassName & "();" & vbCrLf &_
vbCrLf &_
"public: // attribute modification" & vbCrLf & _
vbCrLf & _
"protected: // protected members" & vbCrLf & _
" void assign(const " & szClassName & "& source);" _
& vbCrLf & vbCrLf & _
"private: // attributes" & vbCrLf & _
"};" & vbCrLf & _
vbCrLf & _
"#endif"
ActiveDocument.Save szProjectPath & szHeaderFile
' now add the source file to the hard drive
Documents.Add "Text"
ActiveDocument.Selection.StartOfDocument
ActiveDocument.Selection = "#include " & Chr(34) & szHeaderFile _
& Chr(34) & vbCrLf & vbCrLf & _
szClassName & "::" & szClassName & "()" & vbCrLf & _
"{" & vbCrLf & _
" // nothing to do yet" & vbCrLf & _
"}" & vbCrLf & _
vbCrLf & _
szClassName & "::" & szClassName & "(const " & szClassName _
& "& source)" & vbCrLf & _
"{" & vbCrLf & _
" assign(source);" & vbCrLf & _
"}" & vbCrLf & _
vbCrLf & _
szClassName & "& " & szClassName & "::" & "operator=(const " _
& szClassName & "& right)" & vbCrLf & _
"{" & vbCrLf & _
" if (this != &right)" & vbCrLf & _
" {" & vbCrLf & _
" assign(right);" & vbCrLf & _
" }" & vbCrLf & _
vbCrLf & _
" return (*this);" & vbCrLf & _
"}" & vbCrLf & _
vbCrLf & _
szClassName & "::~" & szClassName & "()" & vbCrLf & _
"{" & vbCrLf & _
" // nothing to do yet" & vbCrLf & _
"}" & vbCrLf & _
vbCrLf & _
"void " & szClassName & "::assign(const " & szClassName _
& "& source)" & vbCrLf & _
"{" & vbCrLf & _
" // assign all of source's members to this*" & vbCrLf & _
"}" & vbCrLf
ActiveDocument.Save szProjectPath & szSourceFile
End Sub
'FILE DESCRIPTION: A description was not provided.
'------------------------------------------------------------------------------
Function GetProjectPath(ByVal proj)
dim szProjectPath
dim nIndex
dim szReturn
szProjectPath = proj.FullName
nIndex = InStrRev(szProjectPath, "\")
if (nIndex <> 0) then
szReturn = Left(szProjectPath, nIndex)
end if
GetProjectPath = szReturn
End Function
Sub ClassGenerator
'DESCRIPTION: Creates minimal source/header files for a
' class, given its input name.
On Error Resume Next
dim szClassName
dim szClassRoot ' classname minus any MFC-style prefix
dim szSourceFile
dim szHeaderFile
' used to determine full paths for the added
' source/header file
dim szProjectPath
dim szTempPath
dim szHeaderPreprocessor, szHeaderClassBegin
dim szFirstChar, szSecondChar
' make sure the project is valid
if ActiveProject.Type <> "Build" then
MsgBox "This project is not valid. Ending macro."
Exit Sub
end if
' enter the class name
szClassName = InputBox("Enter the class name:", "Class Name")
if (len(szClassName) <= 0) then
MsgBox "Invalid class name. Ending macro."
Exit Sub
end if
' generate file names based on the input
' class name
szSourceFile = szClassName + ".cpp"
szHeaderFile = szClassName + ".h"
' test for MFC style class names [ie: CClassName's
' file should be ClassName.h"] all we do is check the
' first to characters; if they're both capitals, we
' don't use the first one
szFirstChar = Left(szClassName, 1)
szSecondChar = Mid(szClassName, 2, 1)
if ( ((Asc(szFirstChar) >= 65) and (Asc(szFirstChar) <= 90)) _
and ((Asc(szSecondChar) >= 65) and (Asc(szSecondChar) <= 90)) ) _
then
szSourceFile = Mid(szSourceFile, 2)
szHeaderFile = Mid(szHeaderFile, 2)
szClassRoot = Mid(szClassName, 2)
else
szClassRoot = szClassName
end if
' add the files to the project -- if they can't be added,
' Resume Next will see to it that we skip past this
ActiveProject.AddFile szSourceFile
ActiveProject.AddFile szHeaderFile
' get the project path
szProjectPath = GetProjectPath(ActiveProject)
' now add the header file to the hard drive
Documents.Add "Text"
ActiveDocument.Selection.StartOfDocument
ActiveDocument.Selection = "#ifndef " & UCase(szClassRoot) _
& "_H_" & vbCrLf & _
"#define " & UCase(szClassRoot) & "_H_" & vbCrLf & _
vbCrLf & _
"class " & szClassName & vbCrLf & _
"{" & vbCrLf & _
"public: // object creation/destruction" & vbCrLf & _
" " & szClassName & "();" & vbCrLf & _
" " & szClassName & "(const " & szClassName & "& source);" _
& vbCrLf & _
" " & szClassName & "& " & "operator=(const " & szClassName _
& "& right);" & vbCrLf & _
" virtual " & "~" & szClassName & "();" & vbCrLf &_
vbCrLf &_
"public: // attribute modification" & vbCrLf & _
vbCrLf & _
"protected: // protected members" & vbCrLf & _
" void assign(const " & szClassName & "& source);" _
& vbCrLf & vbCrLf & _
"private: // attributes" & vbCrLf & _
"};" & vbCrLf & _
vbCrLf & _
"#endif"
ActiveDocument.Save szProjectPath & szHeaderFile
' now add the source file to the hard drive
Documents.Add "Text"
ActiveDocument.Selection.StartOfDocument
ActiveDocument.Selection = "#include " & Chr(34) & szHeaderFile _
& Chr(34) & vbCrLf & vbCrLf & _
szClassName & "::" & szClassName & "()" & vbCrLf & _
"{" & vbCrLf & _
" // nothing to do yet" & vbCrLf & _
"}" & vbCrLf & _
vbCrLf & _
szClassName & "::" & szClassName & "(const " & szClassName _
& "& source)" & vbCrLf & _
"{" & vbCrLf & _
" assign(source);" & vbCrLf & _
"}" & vbCrLf & _
vbCrLf & _
szClassName & "& " & szClassName & "::" & "operator=(const " _
& szClassName & "& right)" & vbCrLf & _
"{" & vbCrLf & _
" if (this != &right)" & vbCrLf & _
" {" & vbCrLf & _
" assign(right);" & vbCrLf & _
" }" & vbCrLf & _
vbCrLf & _
" return (*this);" & vbCrLf & _
"}" & vbCrLf & _
vbCrLf & _
szClassName & "::~" & szClassName & "()" & vbCrLf & _
"{" & vbCrLf & _
" // nothing to do yet" & vbCrLf & _
"}" & vbCrLf & _
vbCrLf & _
"void " & szClassName & "::assign(const " & szClassName _
& "& source)" & vbCrLf & _
"{" & vbCrLf & _
" // assign all of source's members to this*" & vbCrLf & _
"}" & vbCrLf
ActiveDocument.Save szProjectPath & szSourceFile
End Sub
![](https://lh3.googleusercontent.com/-hYZb_novCPQ/V5HuGPkGFUI/AAAAAAAAANk/f8zcKkeTBbA1A-W6yuqfk12fs8bd8FeOQCL0B/banner_468_60.png)
RECENT COMMENT