I am taking a class in C++ and have been given my first project. In class, the professor just talks about syntax, grammar, etc. He never talks about how to use Visual Studio. He emailed us a header file with no explanation whatsoever and expects us to use it for the project. I am not too sure how to get started with this file. I tried creating an empty Visual C++ project, adding this file and running it but for one, there are red underline error everywhere and two, VS says it can’t find the executable. If anybody can help me get VS and/or my project set up, I can take care of making the program (just got done writing the same exact program in Java).
This is the header file he sent us. Judging by the looks of it, he’s kinda sloppy.
#pragma once
namespace control2 {
using namespace System;
using namespace System::IO; // added by Zhang
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
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
// added by Zhang
StreamReader ^sr = gcnew StreamReader("control.txt");
this->choice=Int32::Parse(sr->ReadLine());
sr->Close();
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
int choice;
#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->SuspendLayout();
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Name = L"Form1";
this->Text = L"CS351";
this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
Graphics ^g = e->Graphics;
// g->Clear(BackColor);
// g->Clear(Color::Red);
for ( int y = 0; y < 10; y++ ) {
// pick the shape based on the user's choice
switch ( choice )
{
case 1: // draw rectangles
g->DrawRectangle(Pens::Black, 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 );
break;
case 2: // draw ovals
// g->DrawEllipse(Pens::Black, 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 );
g->DrawArc(Pens::Black, 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10, 0, 360 );
break;
case 3: // fill rectangles
g->FillRectangle(Brushes::Red, 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 );
break;
case 4: // fill ovals
// g->FillEllipse(gcnew SolidBrush(Color::Red), 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 );
g->FillPie(gcnew SolidBrush(Color::Red), 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10, 0, 360 );
break;
default: // draw lines
g->DrawLine(Pens::Black, 10 + i * 10, 60 + i * 20, 60 + i * 20, 10 + i * 10 );
break;
} // end switch
} // end for
choice=(choice+1)%5;
}
};
}
Ughh…pragma…Anyway…
If you open up VS, and go to file, you’ll find the new project option. I’m assuming this is a Windows Forms App. So, select new project, under languages select C++, and then Windows Form App. When that’s all set, go to file->save all and place it in a directory. Now take the file your professor gave you and put it with the rest of the code files in that directory. Back to your project, under the solution explorer, right click header files, add, add existing, and select your header. That should be enough to get you started!