Advertisement
ponchic

Form1.h

Nov 3rd, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.38 KB | None | 0 0
  1. #pragma once
  2.  
  3. namespace БуферОбменаSaveBMP {
  4.  
  5.     using namespace System;
  6.     using namespace System::ComponentModel;
  7.     using namespace System::Collections;
  8.     using namespace System::Windows::Forms;
  9.     using namespace System::Data;
  10.     using namespace System::Drawing;
  11.  
  12.     /// <summary>
  13.     /// Сводка для Form1
  14.     /// </summary>
  15.     public ref class Form1 : public System::Windows::Forms::Form
  16.     {
  17.     public:
  18.         Form1(void)
  19.         {
  20.             InitializeComponent();
  21.             //
  22.             //TODO: добавьте код конструктора
  23.             //
  24.         }
  25.  
  26.     protected:
  27.         /// <summary>
  28.         /// Освободить все используемые ресурсы.
  29.         /// </summary>
  30.         ~Form1()
  31.         {
  32.             if (components)
  33.             {
  34.                 delete components;
  35.             }
  36.         }
  37.  
  38.  
  39.     private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;
  40.  
  41.     private: System::Windows::Forms::Panel^  panel1;
  42.     private: System::Windows::Forms::PictureBox^  pictureBox1;
  43.     private: System::Windows::Forms::Button^  button1;
  44.     private: System::Windows::Forms::Button^  button2;
  45.  
  46.  
  47.     protected:
  48.  
  49.     private:
  50.         /// <summary>
  51.         /// Требуется переменная конструктора.
  52.         /// </summary>
  53.         System::ComponentModel::Container ^components;
  54.  
  55. #pragma region Windows Form Designer generated code
  56.         /// <summary>
  57.         /// Обязательный метод для поддержки конструктора - не изменяйте
  58.         /// содержимое данного метода при помощи редактора кода.
  59.         /// </summary>
  60.         void InitializeComponent(void)
  61.         {
  62.             this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
  63.             this->panel1 = (gcnew System::Windows::Forms::Panel());
  64.             this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
  65.             this->button1 = (gcnew System::Windows::Forms::Button());
  66.             this->button2 = (gcnew System::Windows::Forms::Button());
  67.             this->panel1->SuspendLayout();
  68.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
  69.             this->SuspendLayout();
  70.             //
  71.             // panel1
  72.             //
  73.             this->panel1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
  74.                 | System::Windows::Forms::AnchorStyles::Left)
  75.                 | System::Windows::Forms::AnchorStyles::Right));
  76.             this->panel1->Controls->Add(this->pictureBox1);
  77.             this->panel1->Location = System::Drawing::Point(12, 12);
  78.             this->panel1->Name = L"panel1";
  79.             this->panel1->Size = System::Drawing::Size(325, 224);
  80.             this->panel1->TabIndex = 3;
  81.             //
  82.             // pictureBox1
  83.             //
  84.             this->pictureBox1->Location = System::Drawing::Point(3, 3);
  85.             this->pictureBox1->Name = L"pictureBox1";
  86.             this->pictureBox1->Size = System::Drawing::Size(312, 189);
  87.             this->pictureBox1->TabIndex = 0;
  88.             this->pictureBox1->TabStop = false;
  89.             //
  90.             // button1
  91.             //
  92.             this->button1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
  93.             this->button1->Location = System::Drawing::Point(12, 242);
  94.             this->button1->Name = L"button1";
  95.             this->button1->Size = System::Drawing::Size(239, 23);
  96.             this->button1->TabIndex = 0;
  97.             this->button1->Text = L"button1";
  98.             this->button1->UseVisualStyleBackColor = true;
  99.             this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
  100.             //
  101.             // button2
  102.             //
  103.             this->button2->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
  104.             this->button2->Location = System::Drawing::Point(276, 242);
  105.             this->button2->Name = L"button2";
  106.             this->button2->Size = System::Drawing::Size(51, 23);
  107.             this->button2->TabIndex = 2;
  108.             this->button2->Text = L"Re-get bitmap image";
  109.             this->button2->UseVisualStyleBackColor = true;
  110.             this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
  111.             //
  112.             // Form1
  113.             //
  114.             this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  115.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  116.             this->ClientSize = System::Drawing::Size(339, 277);
  117.             this->Controls->Add(this->panel1);
  118.             this->Controls->Add(this->button2);
  119.             this->Controls->Add(this->button1);
  120.             this->Name = L"Form1";
  121.             this->Text = L"Form1";
  122.             this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
  123.             this->panel1->ResumeLayout(false);
  124.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
  125.             this->ResumeLayout(false);
  126.  
  127.         }
  128. #pragma endregion
  129.         // Программа читает буфер обмена, и если данные в нем представлены
  130.         // в формате растровой графики, то записывает их в BMP-файл
  131.     private: System::
  132.                  Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
  133.              {
  134.                  //panel1->Size = Drawing::Size(200, 151);
  135.                  this->Text = "Сохраняю копию БО в BMP-файл";
  136.                  button1->Text = "Сохранить";
  137.                  //pictureBox1->Size = Drawing::Size(184, 142);
  138.                  pictureBox1->SizeMode = PictureBoxSizeMode::AutoSize; //ScretchImage - сжатие\растягивание изображения
  139.                  panel1->AutoScroll = true;
  140.  
  141.                  button2->PerformClick();
  142.              }
  143.     private: System::Void button1_Click(System::Object^  sender,
  144.                  System::EventArgs^  e)
  145.              {
  146.                  // Объявление объекта-получателя из буфера обмена
  147.                  auto Получатель = Clipboard::GetDataObject();
  148.                  Bitmap ^ Растр;
  149.                  // Если данные в буфере обмена представлены в формате Bitmap...
  150.                  if (Получатель->GetDataPresent(DataFormats::Bitmap))
  151.                  {  // то записать их из БО в переменную Растр в формате Bitmap
  152.                      Растр = (Bitmap^)Получатель->GetData(DataFormats::Bitmap);
  153.                      
  154.                      //ЗДЕСЬ КАРТИНКА ДОЛЖНА ОБРЕЗАТЬСЯ
  155.  
  156.                      saveFileDialog1->Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
  157.                      saveFileDialog1->Title = "Save screenshot";
  158.                      saveFileDialog1->RestoreDirectory = true;
  159.                      
  160.  
  161.  
  162.                     if(saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
  163.                      {        
  164.                                              
  165.                      
  166.                      switch(saveFileDialog1->FilterIndex)
  167.                      {
  168.                      case 1 :
  169.                          this->pictureBox1->Image->Save(saveFileDialog1->FileName, System::Drawing::Imaging::ImageFormat::Jpeg);
  170.                          break;    
  171.                      case 2 :          
  172.                          this->pictureBox1->Image->Save(saveFileDialog1->FileName, System::Drawing::Imaging::ImageFormat::Bmp);
  173.                          break;    
  174.                      case 3 :          
  175.                          this->pictureBox1->Image->Save(saveFileDialog1->FileName, System::Drawing::Imaging::ImageFormat::Gif);
  176.                          break;
  177.                      }
  178.  
  179.                      MessageBox::Show("Изображение из БО записано в файл:  " + saveFileDialog1->ToString());
  180.                      
  181.                     }
  182.  
  183.                  }
  184.                  else
  185.                      // В БО нет данных в формате изображений
  186.                     MessageBox::Show("В буфере обмена нет данных в формате Bitmap", "Запишите какое-либо изображение в БО");
  187.              }
  188.     private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
  189.                  IDataObject ^ Получатель = Clipboard::GetDataObject();
  190.                  Bitmap ^ Растр;
  191.                  // Если данные в БО представлены в формате Bitmap...
  192.                  if (Получатель->GetDataPresent(DataFormats::Bitmap) == true)
  193.                  { // то записать эти данные из БО в переменную
  194.                      // Растр в формате Bitmap:
  195.                      Растр = (Bitmap^)Получатель->GetData(DataFormats::Bitmap);
  196.                      pictureBox1->Image = Растр;
  197.                  }
  198.              }
  199. };
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement