手机动态网站制作/北京百度快照推广公司
直到2008年我才知道,.NET上的C++已经不是托管C++而是C++/CLI(VS2005引入)。
这是一个进制转换器的源代码,可以进行二进制,八进制,十进制,十六进制的转换。本身没什么难的地方,主要是.net framework的使用。限于整型数(_int64)的限制,输入的数太大会溢出(十六进制只能显示15位)。如果可以直接对字符串进行处理,或是对数字进行分割应该就可以实现无限大数字的进制转换了,不过我不会(汗~~)。
值得一提的是,.net程序很“可怕”,才几十K的程序占用内存竟然十几M。不过如果你最小化程序,占用内存就会迅速下降。这是因为系统调用了SetProcessWorkingSetSize函数,通过这个函数可以实现实际内存和虚拟内存的转换。所以我在构造函数和清除按钮的响应事件中加入了
SetProcessWorkingSetSize(::GetCurrentProcess(),-1,-1);
这并不是真正的内存优化,只是让程序在进程管理器中显的“好看”一些。
- #pragmaonce
- #include<windows.h>
- //加入windows头文件,因为我们会用到
- //SetProcessWorkingSetSize函数
- namespaceconverter{
- usingnamespaceSystem;
- usingnamespaceSystem::ComponentModel;
- usingnamespaceSystem::Collections;
- usingnamespaceSystem::Windows::Forms;
- usingnamespaceSystem::Data;
- usingnamespaceSystem::Drawing;
- ///<summary>
- ///Form1摘要
- ///
- ///警告:如果更改此类的名称,则需要更改
- ///与此类所依赖的所有.resx文件关联的托管资源编译器工具的
- ///“资源文件名”属性。否则,
- ///设计器将不能与此窗体的关联
- ///本地化资源正确交互。
- ///</summary>
- publicrefclassForm1:publicSystem::Windows::Forms::Form
- {
- public:
- Form1(void)
- {
- InitializeComponent();
- SetProcessWorkingSetSize(::GetCurrentProcess(),-1,-1);
- }
- protected:
- ///<summary>
- ///清理所有正在使用的资源。
- ///</summary>
- ~Form1()
- {
- if(components)
- {
- deletecomponents;
- }
- }
- private:System::Windows::Forms::Label^label1;
- protected:
- private:System::Windows::Forms::Label^label2;
- private:System::Windows::Forms::Label^label3;
- private:System::Windows::Forms::Label^label4;
- private:System::Windows::Forms::TextBox^tex2;
- private:System::Windows::Forms::TextBox^tex8;
- private:System::Windows::Forms::TextBox^tex10;
- private:System::Windows::Forms::TextBox^tex16;
- private:System::Windows::Forms::Button^btnClear;
- private:
- ///<summary>
- ///必需的设计器变量。
- ///</summary>
- System::ComponentModel::Container^components;
- #pragmaregionWindowsFormDesignergeneratedcode
- ///<summary>
- ///设计器支持所需的方法-不要
- ///使用代码编辑器修改此方法的内容。
- ///</summary>
- voidInitializeComponent(void)
- {
- System::ComponentModel::ComponentResourceManager^resources=(gcnewSystem::ComponentModel::ComponentResourceManager(Form1::typeid));
- this->label1=(gcnewSystem::Windows::Forms::Label());
- this->label2=(gcnewSystem::Windows::Forms::Label());
- this->label3=(gcnewSystem::Windows::Forms::Label());
- this->label4=(gcnewSystem::Windows::Forms::Label());
- this->tex2=(gcnewSystem::Windows::Forms::TextBox());
- this->tex8=(gcnewSystem::Windows::Forms::TextBox());
- this->tex10=(gcnewSystem::Windows::Forms::TextBox());
- this->tex16=(gcnewSystem::Windows::Forms::TextBox());
- this->btnClear=(gcnewSystem::Windows::Forms::Button());
- this->SuspendLayout();
- //
- //label1
- //
- this->label1->AutoSize=true;
- this->label1->BackColor=System::Drawing::Color::Transparent;
- this->label1->Location=System::Drawing::Point(12,9);
- this->label1->Name=L"label1";
- this->label1->Size=System::Drawing::Size(53,12);
- this->label1->TabIndex=0;
- this->label1->Text=L"二进制:";
- //
- //label2
- //
- this->label2->AutoSize=true;
- this->label2->BackColor=System::Drawing::Color::Transparent;
- this->label2->Location=System::Drawing::Point(12,38);
- this->label2->Name=L"label2";
- this->label2->Size=System::Drawing::Size(53,12);
- this->label2->TabIndex=1;
- this->label2->Text=L"八进制:";
- //
- //label3
- //
- this->label3->AutoSize=true;
- this->label3->BackColor=System::Drawing::Color::Transparent;
- this->label3->Location=System::Drawing::Point(12,67);
- this->label3->Name=L"label3";
- this->label3->Size=System::Drawing::Size(53,12);
- this->label3->TabIndex=2;
- this->label3->Text=L"十进制:";
- //
- //label4
- //
- this->label4->AutoSize=true;
- this->label4->BackColor=System::Drawing::Color::Transparent;
- this->label4->Location=System::Drawing::Point(12,96);
- this->label4->Name=L"label4";
- this->label4->Size=System::Drawing::Size(65,12);
- this->label4->TabIndex=3;
- this->label4->Text=L"十六进制:";
- //
- //tex2
- //
- this->tex2->Location=System::Drawing::Point(79,6);
- this->tex2->Name=L"tex2";
- this->tex2->Size=System::Drawing::Size(248,21);
- this->tex2->TabIndex=4;
- this->tex2->TextChanged+=gcnewSystem::EventHandler(this,&Form1::tex2_TextChanged);
- this->tex2->KeyDown+=gcnewSystem::Windows::Forms::KeyEventHandler(this,&Form1::tex2_KeyDown);
- this->tex2->KeyPress+=gcnewSystem::Windows::Forms::KeyPressEventHandler(this,&Form1::tex2_KeyPress);
- //
- //tex8
- //
- this->tex8->Location=System::Drawing::Point(79,36);
- this->tex8->Name=L"tex8";
- this->tex8->Size=System::Drawing::Size(248,21);
- this->tex8->TabIndex=5;
- this->tex8->TextChanged+=gcnewSystem::EventHandler(this,&Form1::tex8_TextChanged);
- this->tex8->KeyDown+=gcnewSystem::Windows::Forms::KeyEventHandler(this,&Form1::tex8_KeyDown);
- this->tex8->KeyPress+=gcnewSystem::Windows::Forms::KeyPressEventHandler(this,&Form1::tex8_KeyPress);
- //
- //tex10
- //
- this->tex10->Location=System::Drawing::Point(79,66);
- this->tex10->Name=L"tex10";
- this->tex10->Size=System::Drawing::Size(248,21);
- this->tex10->TabIndex=6;
- this->tex10->TextChanged+=gcnewSystem::EventHandler(this,&Form1::tex10_TextChanged);
- this->tex10->KeyDown+=gcnewSystem::Windows::Forms::KeyEventHandler(this,&Form1::tex10_KeyDown);
- this->tex10->KeyPress+=gcnewSystem::Windows::Forms::KeyPressEventHandler(this,&Form1::tex10_KeyPress);
- //
- //tex16
- //
- this->tex16->Location=System::Drawing::Point(79,96);
- this->tex16->Name=L"tex16";
- this->tex16->Size=System::Drawing::Size(248,21);
- this->tex16->TabIndex=7;
- this->tex16->TextChanged+=gcnewSystem::EventHandler(this,&Form1::tex16_TextChanged);
- this->tex16->KeyDown+=gcnewSystem::Windows::Forms::KeyEventHandler(this,&Form1::tex16_KeyDown);
- this->tex16->KeyPress+=gcnewSystem::Windows::Forms::KeyPressEventHandler(this,&Form1::tex16_KeyPress);
- //
- //btnClear
- //
- this->btnClear->DialogResult=System::Windows::Forms::DialogResult::Cancel;
- this->btnClear->Location=System::Drawing::Point(340,94);
- this->btnClear->Name=L"btnClear";
- this->btnClear->Size=System::Drawing::Size(50,23);
- this->btnClear->TabIndex=8;
- this->btnClear->Text=L"清除";
- this->btnClear->UseVisualStyleBackColor=true;
- this->btnClear->Click+=gcnewSystem::EventHandler(this,&Form1::btnClear_Click);
- //
- //Form1
- //
- this->AutoScaleDimensions=System::Drawing::SizeF(6,12);
- this->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
- this->BackColor=System::Drawing::SystemColors::Window;
- this->BackgroundImage=(cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"$this.BackgroundImage")));
- this->CancelButton=this->btnClear;
- this->ClientSize=System::Drawing::Size(402,133);
- this->Controls->Add(this->btnClear);
- this->Controls->Add(this->tex16);
- this->Controls->Add(this->tex10);
- this->Controls->Add(this->tex8);
- this->Controls->Add(this->tex2);
- this->Controls->Add(this->label4);
- this->Controls->Add(this->label3);
- this->Controls->Add(this->label2);
- this->Controls->Add(this->label1);
- this->FormBorderStyle=System::Windows::Forms::FormBorderStyle::Fixed3D;
- this->Icon=(cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));
- this->MaximizeBox=false;
- this->Name=L"Form1";
- this->Opacity=0.8;
- this->Text=L"进制转换器";
- this->TopMost=true;
- this->ResumeLayout(false);
- this->PerformLayout();
- }
- #pragmaendregion
- //textbox改变时调用ConvertString来更新其他textbox
- //我们要单独对空字符串进行处理,因为函数不支持空字符串
- private:System::Voidtex2_TextChanged(System::Object^sender,System::EventArgs^e){
- if(tex2->Text!="")
- {
- tex8->Text=ConvertString(tex2->Text,2,8);
- tex10->Text=ConvertString(tex2->Text,2,10);
- tex16->Text=ConvertString(tex2->Text,2,16);
- }
- else
- {
- tex8->Text="";
- tex10->Text="";
- tex16->Text="";
- }
- }
- private:System::Voidtex8_TextChanged(System::Object^sender,System::EventArgs^e){
- if(tex8->Text!="")
- {
- tex2->Text=ConvertString(tex8->Text,8,2);
- tex10->Text=ConvertString(tex8->Text,8,10);
- tex16->Text=ConvertString(tex8->Text,8,16);
- }
- else
- {
- tex8->Text="";
- tex10->Text="";
- tex16->Text="";
- }
- }
- private:System::Voidtex10_TextChanged(System::Object^sender,System::EventArgs^e){
- if(tex10->Text!="")
- {
- tex2->Text=ConvertString(tex10->Text,10,2);
- tex8->Text=ConvertString(tex10->Text,10,8);
- tex16->Text=ConvertString(tex10->Text,10,16);
- }
- else
- {
- tex2->Text="";
- tex10->Text="";
- tex16->Text="";
- }
- }
- private:System::Voidtex16_TextChanged(System::Object^sender,System::EventArgs^e){
- if(tex16->Text!="")
- {
- tex2->Text=ConvertString(tex16->Text,16,2);
- tex8->Text=ConvertString(tex16->Text,16,8);
- tex10->Text=ConvertString(tex16->Text,16,10);
- }
- else
- {
- tex2->Text="";
- tex8->Text="";
- tex10->Text="";
- }
- }
- //处理按键,忽略非法按键,如二进制编辑框中输入字母
- private:System::Voidtex2_KeyPress(System::Object^sender,System::Windows::Forms::KeyPressEventArgs^e){
- wchar_tkey=e->KeyChar;
- if(key=='0'||key=='1')
- e->Handled=false;
- else
- e->Handled=true;
- }
- private:System::Voidtex8_KeyPress(System::Object^sender,System::Windows::Forms::KeyPressEventArgs^e){
- wchar_tkey=e->KeyChar;
- if(key>='0'&&key<='7'||key=='/b'||key==(wchar_t)Keys::Delete)
- e->Handled=false;
- else
- e->Handled=true;
- }
- private:System::Voidtex10_KeyPress(System::Object^sender,System::Windows::Forms::KeyPressEventArgs^e){
- wchar_tkey=e->KeyChar;
- if(key>='0'&&key<='9'||key=='/b'||key==(wchar_t)Keys::Delete)
- e->Handled=false;
- else
- e->Handled=true;
- }
- private:System::Voidtex16_KeyPress(System::Object^sender,System::Windows::Forms::KeyPressEventArgs^e){
- wchar_tkey=e->KeyChar;
- if(key>='0'&&key<='9'||key>='a'&&key<='f'||key>='A'&&key<='F'||
- key=='/b'||key==(wchar_t)Keys::Delete)
- e->Handled=false;
- else
- e->Handled=true;
- }
- private:System::VoidbtnClear_Click(System::Object^sender,System::EventArgs^e){
- tex2->Text="";
- tex8->Text="";
- tex10->Text="";
- tex16->Text="";
- SetProcessWorkingSetSize(::GetCurrentProcess(),-1,-1);
- }
- //字符串进制转换的程序,主要用到Convert::ToInt64和Convert::ToString
- //其中的异常处理主要是输入的数字太多造成数据溢出,或是复制粘贴时贴入非法字符
- String^ConvertString(String^const%num,intfromBase,inttoBase)
- {
- try{
- _int64number=Convert::ToInt64(num,fromBase);
- //异常处理是个大麻烦
- //一开始直接用ToInt64内部的发出的溢出异常,但是当在十六进制
- //文本框中输入16个F时,数据已经溢出,十进制窗口也显示负数,但是没有异常发送
- //只有当再输入一个数字时才会发送溢出异常
- //溢出异常同时会伴随两个参数错误的异常,为什么是两个?
- //现在程序也有个异常问题,就是当粘贴一个负数时,不会发送格式错误或溢出异常
- //但是仍有异常发生,提示字符串不能包含‘-’
- if(number<0)
- throwgcnewOverflowException();
- returnConvert::ToString(number,toBase);
- }
- catch(FormatException^)//格式错误异常
- {
- MessageBox::Show("格式错啦!","错误");
- tex2->Text="";
- tex8->Text="";
- tex10->Text="";
- tex16->Text="";
- }
- catch(ArgumentOutOfRangeException^)//数据溢出时会有
- //两个参数错误异常
- {
- }
- catch(OverflowException^)//数据溢出异常
- {
- MessageBox::Show("数据溢出啦!","错误");
- tex2->Text="";
- tex8->Text="";
- tex10->Text="";
- tex16->Text="";
- }
- }
- //组合键实现复制、粘贴、剪切、全选功能
- private:System::Voidtex16_KeyDown(System::Object^sender,System::Windows::Forms::KeyEventArgs^e){
- if(Control::ModifierKeys==Keys::Control&&e->KeyCode==Keys::A)
- tex16->SelectAll();
- if(Control::ModifierKeys==Keys::Control&&e->KeyCode==Keys::C)
- tex16->Copy();
- if(Control::ModifierKeys==Keys::Control&&e->KeyCode==Keys::V)
- tex16->Paste();
- }
- private:System::Voidtex10_KeyDown(System::Object^sender,System::Windows::Forms::KeyEventArgs^e){
- if(Control::ModifierKeys==Keys::Control&&e->KeyCode==Keys::A)
- tex10->SelectAll();
- if(Control::ModifierKeys==Keys::Control&&e->KeyCode==Keys::C)
- tex10->Copy();
- if(Control::ModifierKeys==Keys::Control&&e->KeyCode==Keys::V)
- tex10->Paste();
- }
- private:System::Voidtex8_KeyDown(System::Object^sender,System::Windows::Forms::KeyEventArgs^e){
- if(Control::ModifierKeys==Keys::Control&&e->KeyCode==Keys::A)
- tex8->SelectAll();
- if(Control::ModifierKeys==Keys::Control&&e->KeyCode==Keys::C)
- tex8->Copy();
- if(Control::ModifierKeys==Keys::Control&&e->KeyCode==Keys::V)
- tex8->Paste();
- }
- private:System::Voidtex2_KeyDown(System::Object^sender,System::Windows::Forms::KeyEventArgs^e){
- if(Control::ModifierKeys==Keys::Control&&e->KeyCode==Keys::A)
- tex2->SelectAll();
- if(Control::ModifierKeys==Keys::Control&&e->KeyCode==Keys::C)
- tex2->Copy();
- if(Control::ModifierKeys==Keys::Control&&e->KeyCode==Keys::V)
- tex2->Paste();
- }
- };
- }