■Visual C++によるManaged Codeの例(一部を抜粋)
(赤字はコードに対するコメント)
1: #using <mscorlib.dll>
2: using namespace System;
3:
4: #using <System.DLL> ←使用するクラスライブラリを指定する。
5: #using <Microsoft.Win32.Interop.dll>
6: #using <System.Drawing.DLL>
7: #using <System.WinForms.DLL>
8:
9: using namespace System::IO;
10: using namespace System::Drawing;
11: using namespace System::WinForms;
12:
13: #define null 0L
14: ↓「__gc」によりガーベジ・コレクションの対象となる(Managed Codeとなる)
15: __gc class mNotepad : public Form ←System.WinForms.Formより派生
16: {
17: static String* s_strAppName = S"Managed C++ Notepad";
18: public:
19: // constructor
20: mNotepad() : m_bFileOpen(false) {
21: InitForm();
22: }
23:
24: // destructor
25: virtual ~mNotepad()
26: {
27: }
28:
29: private:
30: bool OnResetModifedBuffer()
31: {
32: if(!(m_pTextBox->Modified)) // nothing to save
33: return true;
34:
35: String* strFile = m_pFileName ? m_pFileName : S"Untitled"; ←「S"…"」は.NET Frameworkでの文字列定数
36:
37: switch( MessageBox::Show(String::Concat( S"The text in the ", strFile, S" file has changed.\n\nDo you want to save the changes?"), s_strAppName, MessageBox::YesNoCancel ) )
38: {
39: case DialogResult::Cancel: return false;
40: case DialogResult::Yes: FileMenuSave(null, null);
41: case DialogResult::No: return true;
42: default : return false;
43: };
44: };
45:
46: void HelpMenuAbout(Object * pSender, EventArgs * pArgs)
47: {
48: MessageBox::Show(S"Version 1.0", s_strAppName);
49: }
50:
51: void FileMenuNew(Object * pSender, EventArgs * pArgs)
52: {
53: if( !OnResetModifedBuffer() )
54: return;
55: m_pTextBox->Text = S"";
56: m_pTextBox->Select(0,0);
57:
58: m_bFileOpen = false;
59:
60: //
61: // Set the application's caption
62: //
63: Text = String::Concat(S"Untitled - ", s_strAppName );
64: m_pTextBox->Modified = false;
65:
66: }