佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1609|回复: 9

visual c++ window form application picture viewer问题

[复制链接]
发表于 24-3-2011 07:22 PM | 显示全部楼层 |阅读模式
本帖最后由 尽善尽美 于 24-3-2011 07:25 PM 编辑
  1. #pragma once

  2. namespace pictureviewer {

  3.         using namespace System;
  4.         using namespace System::ComponentModel;
  5.         using namespace System::Collections;
  6.         using namespace System::Windows::Forms;
  7.         using namespace System::Data;
  8.         using namespace System::Drawing;

  9.         /// <summary>
  10.         /// Summary for Form1
  11.         /// </summary>
  12.         public ref class Form1 : public System::Windows::Forms::Form
  13.         {
  14.         public:
  15.                 Form1(void)
  16.                 {
  17.                         InitializeComponent();
  18.                         //
  19.                         //TODO: Add the constructor code here
  20.                         //
  21.                 }

  22.         protected:
  23.                 /// <summary>
  24.                 /// Clean up any resources being used.
  25.                 /// </summary>
  26.                 ~Form1()
  27.                 {
  28.                         if (components)
  29.                         {
  30.                                 delete components;
  31.                         }
  32.                 }
  33.         private: System::Windows::Forms::Button^  button1;
  34.         protected:
  35.         private: System::Windows::Forms::Button^  button2;
  36.         private: System::Windows::Forms::Button^  button3;


  37.         private: System::Windows::Forms::PictureBox^  pictureBox1;
  38.         private: System::Windows::Forms::OpenFileDialog^  openFileDialog1;

  39.         private:
  40.                 /// <summary>
  41.                 /// Required designer variable.
  42.                 /// </summary>
  43.                 System::ComponentModel::Container ^components;

  44. #pragma region Windows Form Designer generated code
  45.                 /// <summary>
  46.                 /// Required method for Designer support - do not modify
  47.                 /// the contents of this method with the code editor.
  48.                 /// </summary>
  49.                 void InitializeComponent(void)
  50.                 {
  51.                         this->button1 = (gcnew System::Windows::Forms::Button());
  52.                         this->button2 = (gcnew System::Windows::Forms::Button());
  53.                         this->button3 = (gcnew System::Windows::Forms::Button());
  54.                         this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
  55.                         this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
  56.                         (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
  57.                         this->SuspendLayout();
  58.                         //
  59.                         // button1
  60.                         //
  61.                         this->button1->Location = System::Drawing::Point(385, 12);
  62.                         this->button1->Name = L"button1";
  63.                         this->button1->Size = System::Drawing::Size(94, 23);
  64.                         this->button1->TabIndex = 0;
  65.                         this->button1->Text = L"Message Box";
  66.                         this->button1->UseVisualStyleBackColor = true;
  67.                         this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
  68.                         //
  69.                         // button2
  70.                         //
  71.                         this->button2->Location = System::Drawing::Point(385, 41);
  72.                         this->button2->Name = L"button2";
  73.                         this->button2->Size = System::Drawing::Size(94, 23);
  74.                         this->button2->TabIndex = 1;
  75.                         this->button2->Text = L"Change Color";
  76.                         this->button2->UseVisualStyleBackColor = true;
  77.                         this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
  78.                         //
  79.                         // button3
  80.                         //
  81.                         this->button3->Location = System::Drawing::Point(385, 70);
  82.                         this->button3->Name = L"button3";
  83.                         this->button3->Size = System::Drawing::Size(94, 23);
  84.                         this->button3->TabIndex = 2;
  85.                         this->button3->Text = L"Browse";
  86.                         this->button3->UseVisualStyleBackColor = true;
  87.                         this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
  88.                         //
  89.                         // pictureBox1
  90.                         //
  91.                         this->pictureBox1->Location = System::Drawing::Point(12, 2);
  92.                         this->pictureBox1->Name = L"pictureBox1";
  93.                         this->pictureBox1->Size = System::Drawing::Size(367, 287);
  94.                         this->pictureBox1->TabIndex = 3;
  95.                         this->pictureBox1->TabStop = false;
  96.                         this->pictureBox1->Click += gcnew System::EventHandler(this, &Form1::pictureBox1_Click);
  97.                         //
  98.                         // openFileDialog1
  99.                         //
  100.                         this->openFileDialog1->FileName = L"openFileDialog1";
  101.                         this->openFileDialog1->Filter = L"Image|*.jpg;*.jpeg";
  102.                         this->openFileDialog1->FileOk += gcnew System::ComponentModel::CancelEventHandler(this, &Form1::openFileDialog1_FileOk);
  103.                         //
  104.                         // Form1
  105.                         //
  106.                         this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  107.                         this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  108.                         this->BackColor = System::Drawing::SystemColors::Control;
  109.                         this->ClientSize = System::Drawing::Size(491, 301);
  110.                         this->Controls->Add(this->pictureBox1);
  111.                         this->Controls->Add(this->button3);
  112.                         this->Controls->Add(this->button2);
  113.                         this->Controls->Add(this->button1);
  114.                         this->Name = L"Form1";
  115.                         this->Text = L"Picture Viewer";
  116.                         this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
  117.                         (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
  118.                         this->ResumeLayout(false);

  119.                 }
  120. #pragma endregion
  121.         private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  122.                                                  MessageBox::Show("Hello, World!");         }
  123.         private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
  124.                          this->BackColor=Color::Red;        
  125.                                                   }
  126.         private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
  127.                 openFileDialog1->ShowDialog();
  128.                                  pictureBox1->Load(openFileDialog1->FileName);
  129.                          }
  130.         private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
  131.                                                }
  132.         private: System::Void openFileDialog1_FileOk(System::Object^  sender, System::ComponentModel::CancelEventArgs^  e) {
  133.                          }

  134. private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {
  135.                  }
  136. };
  137. }
复制代码

这是我的编程,我要做一个简单的picture viewer
按那个browse按键后,load picture就没事,但是如果你cancel就会出现这个,请问是什么问题?
回复

使用道具 举报


ADVERTISEMENT

发表于 24-3-2011 08:24 PM | 显示全部楼层
问题在这里。
108. this->openFileDialog1->FileName = L"openFileDialog1";

136.   openFileDialog1->ShowDialog();
   PictureBox1->Load(openFileDialog1->FileName);

你应该先检查 if (openFileDialog1->ShowDialog()==DialogResult::OK)
才做一下步骤。
PS: 别被自己的L"openFileDialog1",吓倒。
回复

使用道具 举报

 楼主| 发表于 24-3-2011 08:41 PM | 显示全部楼层
问题在这里。
108. this->openFileDialog1->FileName = L"openFileDialog1";

136.   openFileDialog1-> ...
chrizyuen2 发表于 24-3-2011 08:24 PM



    可以详细解释吗,我还是新手
回复

使用道具 举报

发表于 24-3-2011 11:23 PM | 显示全部楼层
可以详细解释吗,我还是新手
尽善尽美 发表于 24-3-2011 08:41 PM


你按cancel, openFileDialog1->FileName 当然是原值。L"openFileDialog1";
没有这样的file存在,当然是 FileNotFoundException.
既然如此, 当然 , cancel时候就不执行
所以。 你应该先检查 if (openFileDialog1->ShowDialog()==DialogResult::OK)
才做一下步骤。
回复

使用道具 举报

 楼主| 发表于 25-3-2011 02:58 PM | 显示全部楼层
你按cancel, openFileDialog1->FileName 当然是原值。L"openFileDialog1";
没有这样的file存在,当然是 ...
chrizyuen2 发表于 24-3-2011 11:23 PM


  1.    if(openFileDialog1->ShowDialog() ==
  2.     System::Windows::Forms::DialogResult::OK)
  3.   {
  4.   pictureBox1->Load(openFileDialog1->FileName);
  5.   }
复制代码


我把这个加进去了但还是一样情况
回复

使用道具 举报

发表于 25-3-2011 03:04 PM | 显示全部楼层
本帖最后由 chrizyuen2 于 25-3-2011 03:05 PM 编辑

回复 5# 尽善尽美

sorry 原来我搞错了, 原因C++ 我不是很懂,
以下才对.
if ( openFileDialog1->DialogResult == :: DalogResult::OK )   
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult.aspx
回复

使用道具 举报

Follow Us
 楼主| 发表于 25-3-2011 03:14 PM | 显示全部楼层
回复  尽善尽美

sorry 原来我搞错了, 原因C++ 我不是很懂,
以下才对.
if ( openFileDialog1->DialogR ...
chrizyuen2 发表于 25-3-2011 03:04 PM



   其实#5是对的,只是我打错东西
可否详细解释第五楼,老实说我的programming很差
回复

使用道具 举报

 楼主| 发表于 25-3-2011 03:16 PM | 显示全部楼层
  1. if(openFileDialog1->ShowDialog() ==    System::Windows::Forms::DialogResult::OK)
  2.   {
  3.   pictureBox1->Load(openFileDialog1->FileName);
  4.   }
复制代码


这个也是可以
回复

使用道具 举报


ADVERTISEMENT

发表于 25-3-2011 03:35 PM | 显示全部楼层
其实#5是对的,只是我打错东西
可否详细解释第五楼,老实说我的programming很差
尽善尽美 发表于 25-3-2011 03:14 PM


我也猜不透,你什么不明白...看来要解释,就要从远古时代, 从非常basic说起,一直说到,你不明白的点.


可是,我没那时间.sorry.
回复

使用道具 举报

 楼主| 发表于 25-3-2011 04:13 PM | 显示全部楼层
我也猜不透,你什么不明白...看来要解释,就要从远古时代, 从非常basic说起,一直说到,你不明白的点.


...
chrizyuen2 发表于 25-3-2011 03:35 PM



   那谢谢你,我问其他人吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 16-11-2025 09:47 AM , Processed in 0.119382 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表