Вы находитесь на странице: 1из 12

1

PHStudios.com Tutorials

C++ Advanced Series: Introduction


Text Edition

Copyright © 2008 PH Studios, All Rights Reserved.


2

Contents
Section I About Windows Applications 3
Part I Introduction 3
II Advantages and Disadvantages 3
III Visual C++ Windows Apps Overview (WinForms) 4
Section II Getting Started 9
Part I Modifying Window Size 9
II Change the Window's Title 11

Copyright © 2008 PH Studios, All Rights Reserved.


3

Section I
Section Overview

This section will introduce you to the greatest type of programs currently
available, GUI (graphical user interface) applications. Advantages and disadvantages
of creating a GUI application will be listed to help you determine if you want to go
through the trouble coding them. We will also take a look at the easiest way to
create GUI applications using Windows Forms, which is only available in Microsoft®
Visual C++. At the end of the advanced sub-series, I will briefly go over the Win32
type of GUI which is compiler independent.

Part I Introduction

A GUI application, like your browser or music player, is one of the most difficult
things to program. The more advanced your application progresses, the more
graphical you need to make it. Microsoft® Visual C++ makes developing GUI
applications easier, but limits distribution to Microsoft® Windows platforms only.

Part II Advantages and Disadvantages

GUI applications may look nice, but they require a large amount of development
time. Most of the code and actions of a program will be processed behind the
application itself, but the development and style of the main window needs to be
carefully implemented to look nice and professional. Here is a list of advantages and
disadvantages for GUI applications:

Advantages
 More programmer control
 Application does not quit when finished, unless programmed to
 Better looking than console
 Can do more than console applications

Disadvantages
 Requires more work than usual
 Graphical side needs to be designed carefully to appear professional
 Longer development time
 Longer testing time

Copyright © 2008 PH Studios, All Rights Reserved.


4

Throughout this sub-series, I will be using .NET to create GUI applications


(Windows Applications/WinForms in this case) which will be VC++ only. When we use
.NET we can get rid of or lower some disadvantages but also gain one, Microsoft®
Windows and .NET specific.

Part III Visual C++ Windows Apps Overview (WinForms)

I will briefly go over the basic code for the windows applications. Open Visual C++ 2008
Express and create a new Windows Form Application with any name you want:

Once your project is created, you will have a basic window for you finished already. We should
examine the current source and files before we continue. The following picture shows all the
files found in the generated program.

Copyright © 2008 PH Studios, All Rights Reserved.


5

Note: If you do not see the Solution Explorer, go to View - Solution Explorer

File Description
Form1.h Header file that contains the code to create
the window
Form1.resX XML resource file for Form1.h. Contains
BuildAction
resource.h General resource file for app.rc
stdafx.h Includes standard system files or program
specific include files
app.ico Icon for the file
app.rc Resource script
AssemblyInfo.cpp Project's metadata (copyright, company,
title, ...)
Introduction.cpp Entry point of the application (main function)
stdafx.cpp Includes stdafx.h file

Copyright © 2008 PH Studios, All Rights Reserved.


6

Code for Form1.h


Form1.h contains all code required to create the window. Whenever you
visually edit the window, the code will be automatically created inside this file.

Form1.h Code Listing


#pragma once

namespace Introduction {

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
///
/// 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
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}

private:

Copyright © 2008 PH Studios, All Rights Reserved.


7

/// <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->components = gcnew
System::ComponentModel::Container();
this->Size = System::Drawing::Size(300,300);
this->Text = L"Form1";
this->Padding =
System::Windows::Forms::Padding(0);
this->AutoScaleMode =
System::Windows::Forms::AutoScaleMode::Font;
}
#pragma endregion
};
}

Form1.h Analysis
As you see, the file is well commented. The only function they do not
recommend modifying manually is the InitializeComponent function. Every button,
label, text box, and other component you add will be automatically put in the
InitializeComponent function, so there is no need to modify it yourself. Use the
Properties Window if you want to modify something manually. We will be using the
Properties Window later in this tutorial.

Code for Introduction.cpp


This file is the main file for the program. The main function is the first function
to be called, and it will create the form for you. This function is pretty much complete
already, only rare occasions will cause you to add or edit this file.

Copyright © 2008 PH Studios, All Rights Reserved.


8

Introduction.cpp Code Listing


// Introduction.cpp : main project file.

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

using namespace Introduction;

[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;
}

Introduction.cpp Analysis
You should rarely have to edit this file, but sometimes you might need to. This
file is commented really well too and does not need much analysis. At the top of the
file we include the two header files stdafx and Form1. Next, we have a main function
which accepts program arguments. The function will first set the application to
windows cp visual effects, then will create the form.

Copyright © 2008 PH Studios, All Rights Reserved.


9

Section II
Section Overview
Enough with the introductions, this section we will be modifying the window's
size and title. These changes will be small, but it seems like a nice way to start
learning how to program with .NET. We will also take a look at the Properties
Window to change the values of anything we add to a Windows application.

Part I Modifying Window Size

Output Before Edit

Editing the Program


For most applications, the current size of the window is way too small. We
need to increase it to a large amount. The most common size for general windows
are 800 pixels wide and 600 pixels tall. We will use those dimensions for this
program. Locate Form1.h in the Solution Explorer, right-click and choose View
Designer. You should now be able to visually manage the program. To the right of
VC++, you should see the Properties Window. If you do not see this window, go to
View - Other Windows - Properties Window. Make sure the entire window is
selected and scroll down until you find Size under the Layout section. It should
have a value of 300, 300 right now, change that value to 800, 600 and hit Enter.
You should notice a change in the window straight from the designer.

Copyright © 2008 PH Studios, All Rights Reserved.


10

Press F5 on the keyboard and select Yes on the build confirmation window if you
receive it.

Output After Edit

We have a nice size for our application finally. If you would like a different
size, just change the Size property of the form. Width is first in the property, then
height.

Copyright © 2008 PH Studios, All Rights Reserved.


11

Part II Change the Window's Title

Output Before Edit

Editing the Program


Form1 as a title is not very good. We need to change it to fit our program. TO change
it, go to the Properties Window and scroll down until you see Text under the Appearance
category. Change Form1 to anything you would like to see as the window title and hit Enter.
You should notice the change inside the designer.

Press F5 on the keyboard and select Yes on the build confirmation window if you receive it.

Copyright © 2008 PH Studios, All Rights Reserved.


12

Output After Change

Conclusion
The goal of this tutorial was to introduce you to GUI programming, in this case using
Windows Forms. All we modified in this tutorial was the window dimensions and title. Next
tutorial we will add buttons to our window and add an effect when we click it.

Copyright © 2008 PH Studios, All Rights Reserved.

Вам также может понравиться