2014-02-02 3 views
-1

Я просто набираю все вещи, подобные примеру в моей книге. Не могли бы вы сказать мне, почему это не работает?Простой графический интерфейс не работает. [C++]

Благодарим за внимание.


Это макет.

enter image description here

Это Example_1.cpp.

// Example_1.cpp : main project file. 

#include "stdafx.h" 
#include "Form1.h" 

using namespace Example_1; 

[STAThreadAttribute] 
int main(array<System::String ^> ^args) 
{ 
    // Enabling Windows XP visual effects before any controls are created 
    Application::EnableVisualStyles(); 
    Application::SetCompatibleTextRenderingDefault(false); 

    // Create the main window and run it 
    Application::Run(gcnew Form1()); 
    return 0; 
} 

Ниже приведен код.

#pragma once 

namespace Example_1 { 

    using namespace System; 
    using namespace System::ComponentModel; 
    using namespace System::Collections; 
    using namespace System::Windows::Forms; 
    using namespace System::Data; 
    using namespace System::Drawing; 

    /// <summary> 
    /// Summary for Form1 
    /// </summary> 
    public ref class Form1 : public System::Windows::Forms::Form 
    { 
    public: 
     Form1(void) 
     { 
      InitializeComponent(); 
      // 
      //TODO: Add the constructor code here 
      // 
     } 

    protected: 
     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     ~Form1() 
     { 
      if (components) 
      { 
       delete components; 
      } 
     } 
    private: System::Windows::Forms::Button^ button1; 
    private: System::Windows::Forms::Label^ label1; 
    private: System::Windows::Forms::TextBox^ textBox1; 
    protected: 

    private: 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     System::ComponentModel::Container ^components; 

#pragma region Windows Form Designer generated code 
     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     void InitializeComponent(void) 
     { 
      this->button1 = (gcnew System::Windows::Forms::Button()); 
      this->label1 = (gcnew System::Windows::Forms::Label()); 
      this->textBox1 = (gcnew System::Windows::Forms::TextBox()); 
      this->SuspendLayout(); 
      // 
      // button1 
      // 
      this->button1->Location = System::Drawing::Point(154, 74); 
      this->button1->Name = L"button1"; 
      this->button1->Size = System::Drawing::Size(104, 23); 
      this->button1->TabIndex = 0; 
      this->button1->Text = L"OK"; 
      this->button1->UseVisualStyleBackColor = true; 
      this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); 
      // 
      // label1 
      // 
      this->label1->AutoSize = true; 
      this->label1->Location = System::Drawing::Point(12, 9); 
      this->label1->Name = L"label1"; 
      this->label1->Size = System::Drawing::Size(118, 13); 
      this->label1->TabIndex = 1; 
      this->label1->Text = L"Please enter you name."; 
      // 
      // textBox1 
      // 
      this->textBox1->Location = System::Drawing::Point(15, 35); 
      this->textBox1->Name = L"textBox1"; 
      this->textBox1->Size = System::Drawing::Size(243, 20); 
      this->textBox1->TabIndex = 2; 
      // 
      // Form1 
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
      this->ClientSize = System::Drawing::Size(273, 110); 
      this->Controls->Add(this->textBox1); 
      this->Controls->Add(this->label1); 
      this->Controls->Add(this->button1); 
      this->Name = L"Form1"; 
      this->Text = L"Form1"; 
      this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); 
      this->ResumeLayout(false); 
      this->PerformLayout(); 

     } 
#pragma endregion 
    private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { 
      } 
    private: System::Void button1_Click(System::Object^ sender, 
    System::EventArgs^ e) { 
      String^ message = "Hello"; 
      String^ title = "Welcome"; 
      MessageBox::Show(message, title, MessageBoxButtons::OK); 
      } 
    }; 
} 

Ниже приведен выход.

1>------ Build started: Project: Example_1, Configuration: Debug Win32 ------ 
1>Build started 02-Feb-14 6:28:11 PM. 
1>InitializeBuildStatus: 
1> Touching "Debug\Example_1.unsuccessfulbuild". 
1>GenerateTargetFrameworkMonikerAttribute: 
1>Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files. 
1>ClCompile: 
1> All outputs are up-to-date. 
1> Example_1.cpp 
1>c:\users\user\desktop\example_1\example_1\Form1.h(108): error C2653: 'MessageBoxButton' : is not a class or namespace name 
1>c:\users\user\desktop\example_1\example_1\Form1.h(108): error C2065: 'OK' : undeclared identifier 
1> 
1>Build FAILED. 
1> 
1>Time Elapsed 00:00:01.88 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+0

Что такое "MessageBoxButton"? – khajvah

+0

Извините. Это должны быть MessageBoxButtons. – Casper

+0

Из-за чего ошибка в коде исчезает. –

ответ

1

Вы забыли пространство имен класса MessageBox:

using namespace System::Windows; 
+0

Где добавить это заявление? «Eample_1.cpp» или «Form1.h»? Похоже, он не работает в обоих файлах. Благодарю. – Casper

+0

введите его в Form1.h – thumbmunkeys

+0

1> TRACKER: ошибка TRK0002: Не удалось выполнить команду: "" C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ VC \ bin \ CL.exe "@C: \ Users \ User \ AppData \ Local \ Temp \ tmpab12ce6007fa4afabbb059350e07538e.rsp». Недопустимый адрес блока управления хранением. Как это решить? – Casper

Смежные вопросы