Alyda ?

The domain name www.alyda.nl is active since December 2004.
 
Home arrow Tutorials arrow MS Visual Studio C++ 2005 .NET
MS Visual Studio C++ 2005 .NET

MS Visual Studio 2005 C++ .NET Cook book Tutorials

MS Visual Studio C++ 2005 .NET 

At a certain moment in time I had a great compulsion to learn how to make application with MS Visual Studio.NET. Having knowledge about C++ but not Visual C++ I started to search for examples on the net. All I found where sites with advanced examples way out of my league.
Luckily I came across the book "Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner" that helped me understanding the "visual" part. While learning I made notes that are written down below, more or less resulting in a Cook book how to use Visual Studio. Just follow the instructions descripted in the examples. Hopefully it helps making your first start.

If you like what you see or have suggestions, please drop a line using the "Contact Us". I would appreciate your comments.

MS Visual Studio C++ 2005 .NET and NI Measurement Studio V8.0

Links

Example #1,  the “Up Down Counter”

Program that shows a counter value that can be changed by pressing the "Up" or "Down" button.

Start Microsoft Visual Studio2005, go to File -> New -> Project. The “New Project”  window will be shown:

Image

Select Project types: CLR and “Windows Forms Application” as Template. Name the project Up_Down_Counter, press OK and the Visual C++ IDE places you in the Design view of the Up_Down_Counter project.

Image

If you don’t see the Toolbox go to View and select Toolbox.
Next drag three Buttons and a TextBox from the Toolbox to the Form1 field as shown below. Do not double click the buttons.

Image

Next rename the buttons. Close the ToolBox window and open the Properties window (View -> Other Windows -> Properties Window). Single click button1 and change Text and Design (Name) into “Up”. Do the same for button2 but call this button, “Down” and call button3 “Exit”.

Image

After making this very simple GUI design we will enter some code. Double click somewhere on the (grey) form. Search in the code for the following lines (somewhere near the top):

private:
  /// <summary>
  /// Required designer variable.
  /// </summary>

and define after the comment line a global integer called intCounter. Like this:
   Int16 intCounter; // Counter variable

Scroll to the bottom and search for the Form1_load function. Initialize the intCounter variable and display the variable value intCounter in the TextBox by adding the following code to the Form1_Load function:

   private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {

intCounter = 0;  
textBox1->Text = intCounter.ToString(); 
}

Return to the design view by clicking on the “Form1.h [Design]” tab and double click on the Up button. Add the code:

  private: System::Void Up_Click(System::Object^  sender, System::EventArgs^  e) {

  intCounter++;
  textBox1->Text = intCounter.ToString();
  }

The code for the Down button would be:

  private: System::Void Down_Click(System::Object^  sender, System::EventArgs^  e) {

intCounter--;
textBox1->Text = intCounter.ToString();
}

And for the Exit button:

  private: System::Void Exit_Click(System::Object^  sender, System::EventArgs^  e) {

Application::Exit();
}

This is all the code we need for the Up Down Counter. To compile and execute the program go to Debug -> Start Without Debugging. After compilation your first program will start.

Example #2,  a Mathematical plot program

Program that plots a sine wave  when the "Draw" button is pressed.

Start Microsoft Visual Studio2005, go to File -> New -> Project. The “New Project”  window will be shown:

Image

Select Project types: CLR and “Windows Forms Application” as Template. Name the project Math_Plot, press OK and the Visual C++ IDE places you in the Design view of the Math_Plot project.

Image

Drag three buttons and a PictureBox to the Form1 field.
Click once on each separate component and set the component properties as follows:

Form1:  Text -> Plot program
             Size -> 700; 300

Button1: Text -> Draw
              Name -> Draw

Button2: Text -> Clear
              Name-> Clear

Button3: Text -> Exit
              Name -> Exit

PictureBox1: Size -> 500; 200
                     BackColor -> select Custom -> select Black

The end result should look as shown in the folowing figure.

Image

When all properties are set double click on the gray colored field and enter the following code:

Located the following code somewhere near the top:

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

and add  
  //Handle to form graphic surface
  System::Drawing::Graphics^ FormGraphic;
  Char i;  // Loop counter variable

This defines a global graphic surface handle and variable i.

Scroll down and include in the Form1_load function:

   private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {

  //Obtain the pictureBox1 graphic surface
  FormGraphic = pictureBox1->CreateGraphics();
  }

Return to the design view by clicking on the “Form1.h [Design]” tab and double click on the Draw button. Add the code:

   private: System::Void Draw_Click(System::Object^  sender, System::EventArgs^  e) {

  //Handle to pen
  System::Drawing::Pen^ drawingPen;
  //Declare two point objects
  System::Drawing::PointF point1, point2;
  //Instantiate a pen object
  drawingPen = gcnew System::Drawing::Pen (System::Drawing::Color::White );
  //Define starting point
  point1.Y = 100.0;
  point1.X = 0.0;
  i = 0;

  while ( i < 500 )
  {
     point2.Y = 100 - (50.0 * (Math::Sin( (i/50.0) * 2 *  Math::PI)));
     point2.X = i;
     FormGraphic->DrawLine ( drawingPen, point1,  point2 );
     point1.Y = point2.Y;
     point1.X = point2.X;
     i++;
  }
 }

Return to the design view by clicking on the “Form1.h [Design]” tab and double click on the Clear button. Add the code:

  private: System::Void Clear_Click(System::Object^  sender, System::EventArgs^  e) {

  FormGraphic->Clear ( pictureBox1->BackColor );
  }

Return to the design view by clicking on the “Form1.h [Design]” tab and double click on the Exit button. Add the code:

private: System::Void Exit_Click(System::Object^  sender,  System::EventArgs^  e) {

  Close();
  } 

In the Menu bar go to Debug -> Start Without Debugging. After compilation your plot program will start.

Example #2a,  a Mathematical plot program, some extensions

Having the Clear button activated while there’s nothing to clear is a bit stupid. Let’s enhance our program. Go to Form1.h code page and add the following code:

  private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {

   //Obtain the pictureBox1 graphic surface
   FormGraphic = pictureBox1->CreateGraphics();
   Clear->Enabled = false;
   }

  private: System::Void Clear_Click(System::Object^  sender,  System::EventArgs^  e) {

    FormGraphic->Clear ( pictureBox1->BackColor );
    Clear->Enabled = false;
   }

And add after the while loop in the draw function:
  Clear->Enabled = true;

In the Menu bar go to Debug -> Start Without Debugging. After compilation your plot program with enhancements will start.

  

MS Visual Studio C++ 2005 .NET and NI Measurement Studio V8.0

Example #1, National Instruments demo #1  
Goal

Cook book tutorial how to use the NI Measurement Studio in combination with Microsofts Visual Studio 2005 C++.
This document shows step by step how to use the graphical components of the NI Measurements Studio together with C++.
I noticed that documentation for Measurement Studio with C# or VB is available. However how to’s, tutorials or examples for Measurement Studio together with C++ are rare. I hope this document gives you some help in the first steps.

What do you need

Microsoft Visual Studio 2005 and the National Instruments Measurement Studio version 8. Note that if you want to distribute your program you can’t use the Microsoft Visual Studio 2005 Express version.

Example #3:  Measurement Studio Demo #1

Start Microsoft Visual Studio2005, go to File -> New -> Project. The “New Project”  window will be shown:

Image
Figure 1

Select Project types: CLR and “Windows Forms Application” as Template. Name the project “NI demo 01”, press OK and the Visual C++ IDE places you in the Design view of the “NI demo 01” project (figure 2).

Image
Figure 2


If you don’t see the Toolbox go to View and select Toolbox.
Drag from the Measurement Studio Toolbox the following components to the Form1 field :
- Meter              - Gauge                  - Tank
- Thermometer  - WaveformGraph  - Knob
- Switch            - NummericEdit       - Led

Drag from the Common Controls Toolbox a Button and from the Components Toolbox a Timer to Form1 field.
An example of  how the Form field looks with all the components is shown in figure 3.

Image
Figure 3

Rename button1 by right clicking on the button and selecting properties. Change in the (Name) and Text field “button1” into “Exit”.

After creating this weird GUI design we will enter some code. Double click somewhere on the (grey) form field. The code for the present program will open and the cursor is placed at the Form1_Load function. Insert the following code:

   private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {

 timer1->Enabled=false;                                                              // Disable the timer.

 // Define title of the program
 this->Text = "NI Measurement Studio and Visual Studio .NET 2005 C++ demo";

 numericEdit1->CoercionInterval = 0.1;                                        // Define stepsize nummerEdit1
 numericEdit1->Value = 4.0;                                                         // Define default value nummerEdit1
 switch1->Caption = "Start";                                                         // Place "Start" on top of the switch

 xAxis1->Inverted = false;                                                             // Do not invert Y-axis
 waveformPlot1->CanScaleYAxis = false;                                    // Do not auto scale Y-axis
 waveformGraph1->Caption = "Knob value";                                // Place "Knob value" on top of the  plot
 xAxis1->Mode = NationalInstruments::UI::AxisMode::StripChart; // Select Strip chart plotting

 // Define min. and max. ranges of the different components
 knob1->Range = gcnew NationalInstruments::UI::Range(-10,10);
 gauge1->Range = gcnew NationalInstruments::UI::Range(-10,10);
 meter1->Range = gcnew NationalInstruments::UI::Range(-10,10);
 tank1->Range = gcnew NationalInstruments::UI::Range(-10,10);
 thermometer1->Range = gcnew NationalInstruments::UI::Range(-10,30);
 yAxis1->Range = gcnew NationalInstruments::UI::Range(-10,10);
}

Return to the design view by clicking on the “Form1.h [Design]” tab and double click on the switch. Add the code:

 private: System::Void switch1_StateChanged(System::Object^  sender, NationalInstruments::UI::ActionEventArgs^  e) {

 if (timer1->Enabled)   // If timer is enabled
   { 
      timer1->Enabled=false;  // then disable timer
   }
 else
   {
      timer1->Enabled=true;  // otherwise enable timer
   }
}

Return to the design view by clicking on the “Form1.h [Design]” tab and double click on the timer1 component. Add the code:

 private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {

 Double dblKnobValue;                            // Define Double variable dblKnobValue
 Double dblAlarmLevel;                           // Define Double variable dblAlarmLevel

 dblAlarmLevel = numericEdit1->Value;  // Read the NumericEdit value
 
 dblKnobValue = knob1->Value;             // Place current knob position into var.dblKnobValue
 gauge1->Value = dblKnobValue;           // Update Gauge1 reading with knob position
 meter1->Value = dblKnobValue;            // Update Meter1 reading with knob position
 tank1->Value = dblKnobValue;              // Update Tank1 reading with knob position
 thermometer1->Value = dblKnobValue; // Update Thermometer1 reading with knob  position
 waveformPlot1->PlotYAppend(dblKnobValue);

 if (dblKnobValue > dblAlarmLevel)                       // Check if knob position exceeds alarm level.
    led1->Value = true;                                            // If so, switch on LED
 else
   led1->Value = false;                                           // If not, switch off LED
}


Return to the design view by clicking on the “Form1.h [Design]” tab and double click on the Exit button. Add the code:

 private: System::Void Exit_Click(System::Object^  sender, System::EventArgs^  e) {

 timer1->Enabled=false;   // Disable the timer.
 Close();                           // Exit program
}


To compile and execute the program go to Debug -> Start Without Debugging. After compilation your first program will start.
 
Download here the Measurement Studio Demo #1 (7Mb). Note “Microsoft .NET Framework Version 2.0” needs to be installed.

Deploying software 

Once the software is written and you want to install the software on other computers you have to make sure to include all necessary modules. Luckily the Visual Studio developers environment provides you with a good tool.
Note: Visual C++ Express does not support Setup Projects

Let’s assume you want to make a setup program for example #1: Measurement Studio Demo #1. If you simply Build the project you find an executable in either de Debug or Release folder, depending on the Solution Configuration.

Image

Since the program is ready for deployment we choose Release as solution. Next make sure that the Visual Studio compiler leaves a copy of the non-standard Microsoft DLL’s, in this case a NI Measurement Studio DLL. Go to Project -> NI demo 01 Properties… (ALT+F7) -> Common Properties -> References and set under the “Build Properties” “Copy Local” to true.

Image

Build the Solution (F7). In the Project folder …/NI demo 01/release you will find the “NI demo 01” executable and the appropriate DLL. Running the executable on your development system is no problem because all the needed software is installed on your system. However you understand that the size of this executable tells you that this is only part of the story. We need to build a setup program.
Close the “NI demo 01” solution (File -> Close Solution) and create a new project. Go to File -> New -> Project -> Other Project Types -> Setup and Deployment and select Setup Project. Name the Project “NI_demo_01_setup” and create a directory for the solution. Press OK.

Image

The next configuration window will be opened. Make sure that you see on the left side the Solution Explorer (View -> Solution Explorer, Ctrl+Alt+L).
From the Project menu, choose Add and click File. Go to the …/NI demo 01/release folder, select both the DLL and EXE file and click Open.
In the File System window, right click on Application Folder, point to Add and click Create to create a new folder “NI_demo_01”.
Click on Application Folder, select the “NationalInstruments.Common.dll” and drag the dll into the just created “NI_demo_01” folder.
Next you need to merge modules for the MFC80.dll and MSVCR80.dll. From the Project menu, point to Add and click Merge Module. Select both Microsoft_VC80_CRT_x86.msm and Microsoft_VC80_MFC_x86.msm and click Open. If all goes well you should see something like the next figure.

Image

Select in “Solution Configuration” the Release configuration, go to Build -> Build Solution (F7). A NI_demo_01_setup.msi and setup.exe file should be placed into …\NI_demo_01_setup\NI_demo_01_setup\Release  folder. Both files can now be used for the distribution of your program.

Final note: make sure that on the target computer “Microsoft .NET Framework Version 2.0” is installed. If not you can download this freely from the Microsoft site (see Links).

 

Links:

Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner
Aaron Miller, Jerry Lee Ford, Jr.
ISBN 13: 978-1-59200-816-2 © 2006
ISBN 10: 1-59200-816-X
Publish date: December 21, 2005
360 pages
Softcover
Thomson Course Technology

C++ Deployment:
http://msdn2.microsoft.com/en-us/library/zebw5zk9.aspx

How to: Deploy using a Setup and Deployment Project:
http://msdn2.microsoft.com/en-us/library/ms235317(VS.80).aspx

Microsoft .NET Framework Version 2.0 Redistributable Package (x86):
Size 22.4Mb, date: 22/03/06
http://www.microsoft.com/downloads/details.aspx?FamilyID=0856eacb-4362-4b0d-8edd-aab15c5e04f5&displaylang=en

 
< Prev
© 2020 www.alyda.nl
Joomla! is Free Software released under the GNU/GPL License.