06.11.2021

Creation of applications with a graphical interface. Methodology for developing applications using forms We write an interface in c


Good day! In this tutorial, we will create your first GUI application in MS Visual Studio. It will be a kind of "Hello World" for graphical applications. I will say right away that using Windows Forms is not the only way to create graphical applications (applications with a graphical user interface) for C # programmers, but it is better to start learning from this. And so, we launch Visual Studio.

Launched? Then to business! We go to the main menu and select the item "File - New - Project", as shown in the figure below.

In the window that appears:

  • on the left side, select "Templates - Visual C # - Windows";
  • in the main area, select the element "Windows Forms Application";
  • in the lower part of the window, enter the name of the project and indicate its location on the disk.

In general, as shown in the figure below.

Did you specify what you need? Then click on the "OK" button. Now you should see something like this (the main areas are highlighted with rectangles):

In the image above, I've labeled the main areas: the designer area (top left), the solution explorer area (top right), and the properties area (bottom right). These are the areas we will work with most often.

In the designer area, there is now an empty “form”, this is the so-called window, in this case, the main window of our program. The properties area displays the properties of the element selected in the designer, in this case, our form, but the solution explorer area contains project files, including those related to the forms (windows) of the program. And now, let's change our form a bit, and run this first application.

To do this, select the form in the designer (for this, you can simply left-click on the form) and go to the properties block, in which we find the line "Text" (the word is text, we are looking for it in the left column), as shown in the figure below.

Property "Text" of the main form of the application

Please note that the left column shows the name (property name), and the right column shows its value.

In this case, we are dealing with a text property, and its value is displayed in the window title, so let's now put something of our own there, for example, something like: "Main window", as shown in the figure below:

Now, you can build the project and run it. To do this, go to the main menu and select "Build - Build Solution". And then we launch the application, for this we select the item "Debug - Run without debugging" in the main menu. As a result, you should see the following window.

In the next lesson, we will look at simple work with the form designer, and setting up GUI elements, and this lesson has come to an end, we have created the first graphical application, built it and launched it.

Ministry of Education and Science of the Russian Federation

Federal State Budgetary Educational Institution

higher professional education

"UFA STATE OIL

TECHNICAL UNIVERSITY"

Department of Computer Science and Engineering Cybernetics

Creating a GUI Application in Microsoft Visual Studio 2010

Teaching aid

for laboratory and practical classes

with students of the direction

230100 (09.03.01) "Informatics and Computer Engineering"

The teaching aid provides theoretical information, tasks for practical and laboratory work of the course "Programming".

The manual is addressed to teachers of the discipline, as well as students of the direction: 230100 "Informatics and Computer Engineering".

Compiled by: Gabdullina A.A., Art. lecturer at the department VTIK

Druzhinskaya E.V., Art. lecturer at the department VTIK

Reviewer: Filippov V.N., Ph.D., Associate Professor of the Department. VTIK.

1. Theoretical information 4

1.1. Basic concepts 4

1.2. Introduction to Windows Form Application in Microsoft Visual Studio 2010 4

1.3. Form 7 control

1.5. MessageBox 9 function

1.6. TextBox Control 10

2.Practical task. Introduction to Windows Form Application in Microsoft Visual Studio 2010 12

2.1. Assessment structure for completed work 12

2.2. Procedure for performing practical work 12

3. Laboratory work. Application Development in Microsoft Visual Studio 2010 16

3.1. Lab order 16

3.2. Task 1. Tabulation of a function and calculation of its values ​​in the specified interval with a given step 16

3.3. Individual tasks 19

3.4. Task 2. Processing two-dimensional arrays 21

3.5. Individual tasks 27

  1. Theoretical information

  1. 1.1. Basic concepts

Automation of information processes is currently represented, first of all, by the development of a software application with a graphical user interface (GUI) that manages data flows.

Graphical User Interface (GUI) is a system of means for user interaction with a device based on the presentation of all system objects and functions available to the user in the form of graphical screen components (windows, buttons, scroll bars, etc.).

Most often, the interface elements in the GUI are implemented on the basis of metaphors and display their purpose and properties, which makes it easier for unprepared users to understand and master programs. Thus, the user's work is carried out with screen forms containing control objects and toolbars with action buttons for processing.

A standard graphical user interface must meet a number of requirements:

Maintain the information technology of the user's work with the software product;

Focus on the end user who communicates with the program at the external level of interaction;

Satisfy the “six” principle, when no more than 6 concepts are included in one menu bar, each of which contains no more than 6 options;

    maintain a standardized purpose of graphic objects and, if possible, their location on the screen.

In object-oriented programming, we deal with classes and objects. Objects are composite data types: they combine multiple values ​​into a single unit and allow us to write and store those values ​​by name. In other words, an object is an unordered collection of properties, each with a name and a value. The named values ​​contained in an object can be either primitive types, such as numbers or strings, or other objects.

When developing non-console applications, the main concept is the Form.

A form is a container for hosting design environment controls.

Properties - the ability to access information stored in this element.

Methods are a set of actions that an object can perform.

An event is an action recognized by an object (for example, a mouse click, a key press), for which a response can be programmed, i.e. object's response to the event.

In this article, we will talk about creating simple applications using forms in C ++. I want to make it clear right away: the development of "form" C ++ applications will be carried out in the Microsoft Visual Studio environment (we will bypass the main competitor from Borland Software). It is worth noting that in Visual Studio there are two ways to make an application with forms, the decision is made at the time of creating a new project.

The first of these is to use Windows Forms, which implements a graphical user interface and is part of the Microsoft.NET Framework. This approach simplifies access to Microsoft Windows interface elements by wrapping the Win32 API in managed code. If you put your thoughts in a more understandable form, then this approach is very similar to building a console application, but a little more complicated. uses forms.

The second way is based on the use of Microsoft Foundation Classes (MFC), a library that takes care of building the application framework. Unlike the first, MFC out of the box uses the MVC (Model-View-Cont roller) pattern. This approach is more complicated than the first, but relying on it, you can easily create a framework for very interesting applications, for example, a text editor or use the Ribbon component and make a menu like in the notorious MS Office 2010.

Creating an application in MS Visual Studio

Let's create a new application: File->New->Project. In the window that appears, as in the figure above, find and select Windows Forms Application, then specify the name (app1) and location of the new project and confirm its creation by clicking the "OK" button.

Before proceeding directly to programming, you should understand what an event is. An event is an action that occurs under certain conditions. The simplest (and most common and) can be considered Load, Click ... let's look at some of them in more detail:

  • Activated - an event that occurs when the element is activated.
  • Click - occurs when the element is clicked once.
  • DoubleClick - double click on the element.
  • HelpRequested - fired when a key is pressed .
  • Paint - fired when an element is being redrawn.
  • MouseLeave - The event fires when the cursor leaves the bounds of the element.

Do not forget that the events specific to an element depend on its type. To view all events available for the form object, select it and select the lightning bolt icon in the properties window. Below is a part of the events for the elementForm1. As you can see, the Form1_Load function is selected for the Load event, so the code contained in this function will be called when the form is loaded.

Open the file Form1.h, there will be the following code:

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

This is the same Form1_Load function that fires when the form is loaded. Let's verify this by adding a TextBox component to the form.

To do this, open the form resource and select it. Next, select the Toolbox toolbar and drag the TextBox component onto the form. Modify the Form1_Load function as follows:

Private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) ( textBox1->Text = "Hello, World!"; //textBox1 is the name of the text box you added)

Run the project, as a result, the following message should appear:

That's all for now, continued in the following lessons.

Hello everyone. In my previous lessons, I talked about creating console applications in the Borland C++ Builder environment. Starting from this lesson, we will learn C++ using graphical applications as an example. Who wants to learn how to create consoles. Can read the book "The Art of Building Console Applications in C++". Our first application will be a program that displays a random number. We open borlandyayu, just do not create a console application. After launch, the following form will appear on the screen:

At the top there will be a toolbar:

On the right, the Object Inspector and the Form List:

Components (unlike BASIC) are already divided into tabs. By name, it is not difficult to guess what type of components are placed on the tab. Open the standart tab and place the components on the form like mine:

The button will be labeled Button1. She needs to be changed. In the lower left window of Borland, the properties of the components are shown; they must be changed to ours:

At the button Caption (Inscription) change to Generate

For label Label1, change the Caption property to Number

In Edit1, the Text property (the text in the edit itself) is simply erased.

After these manipulations, the form will look like mine:

We've finished with the visual style, by the way, most of the properties in Borlyad look like BASIC or Delphi. Experiment with them.

Now let's talk about events. Each component has events, they contain code that will be executed when certain conditions are reached. For example, for a button, the code in the Click event will be executed when we click on it, etc.

Today we will only use the Click event. Click twice on the button and get into the code window:

The Click event for the button was automatically generated. The code, like a console application, is placed between curly braces. We write the code:

Void __fastcall TForm1::Button1Click(TObject *Sender) ( double aaa; //Put the number generated by the processor here String count; //Write the same number, but reduced to a string randomize; //This is necessary so that the numbers do not repeat aaa=random (34)*43646; //Generate any number count=FloatToStr (aaa); //Translate the number into a string using the FloatToString function Edit1->Text=count; //Output a string variable in the text window)

The algorithm is simple, we declare a variable to store a fractional number in it, and a variable for the number in string form. The fact is that the immediately generated number cannot be displayed in a text box (there will be an error. A text was expected and a number was received), so using the FloatToStr function we translate the number into a string and display it in a text box. For output, we turn (using the -> sign (similar to a dot in vb)) to the Edit1 text property and display the text there. That's all for now.

By the way, a question for filling: who breeds faster computer viruses, the Chinese, or rabbits?


Comments()

Vitay

artyomka

"randomize; //This is necessary so that the numbers do not repeat." I still keep repeating. what to do?

Andrey

There are 2 options 1-use "randomize();" or in line 6 complicate the function e.g. add more seconds

Andrey

"seconds" or the result of adding two pseudo-random numbers divided by seconds - the more factors, the more unpredictable the number is

artyomka Alexey(alex13sh)

randomize
this is so that the numbers do not repeat when the program is turned on
well, that is. turned on the program, press the button several times
1)5
2)47
3)86
this is me in whole numbers
well, if you turn off the program and turn it on again, when the button is pressed serially, there will be the same numbers with the same sequences
this is without randomize, but this will not be removed

And what is repeated in this way
1)3
2)69
3)1
4)3
5)8
6)1
THIS DOES NOT APPLY TO randomize
to avoid this Andrey already answered))

Begzod

I have visual c++.net on my computer. I can not find textbooks, source codes for it. Help pzhs.

Ali05

I saw a textbook on Visual C ++ .Net "Nikita Kultin Programming Basics in Microsoft Visual C ++ 2010" in the bookstore, it just shows how to create graphical applications under Windows (WinForms).

Coolhacker Nintendo

I wonder what is his "bad"?

Coolhacker

In the absence of the ability to properly present the material and accustoming you, gentlemen, to a bad programming style, a la transliterated variable/function names.

Edward Coolhacker

Annotation: We study widgets - the visual elements that make up the graphical user interface, their layout, size policy, signal-slot connections, graphical interface elements and their use.

13.1 Widgets

Widgets are the visual elements that make up the graphical user interface.

Widget examples:

  • Button (class QPushButton );
  • Label (QLabel class);
  • Input field (QLineEdit class);
  • Numeric counter field (QSpinBox class);
  • Scroll bar (QScrollBar class).

Qt has about 50 ready-made classes of graphic elements available for use. The parent class for all widgets is the QWidget class. All the main properties of visual elements are inherited from it, which we will carefully consider. Let's start exploring ways to develop programs with a graphical interface with an example.

Let's create an empty project file. Run the project wizard and select in the section Projects (Projects) item Other Project. Next, choose a project type. Empty Qt Project. Add the following content to the project file:

TEMPLATE = app #Qt modules we will use QT += widgets #Add widgets module to work with widgets (required for Qt5). TARGET = widget#Name of the executable SOURCES += \ main.cpp

Now let's create a simple program with a window in which we will display an inscription. Set the size of the window and the text of its title, as well as set the font for the caption. To do this, let's create a main.cpp file with the following content:

#include #include int main (int lArgc, char * lArgv ) ( // Create a QApplication object that initializes and configures the window program, // controls its execution using the QApplication event loop lApplication (lArgc, lArgv); QLabel lLabel; // Create a QLabel widget - label lLabel.setText("I am widget!"); //Set the text for the label lLabel.setGeometry(200, 200, 300, 150); //Set dimensions - position (x, y) width and height. Set alignment text lLabel.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); //The QFont class is used to set font parameters. //Select the Arial Black font family and size 12. QFont lBlackFont(" Arial Black ", 12); lLabel. setFont(lBlackFont); //Set the font for the label lLabel.show(); //Call the show() method to show the label on the screen return lApplication.exec(); //Start the program to execute exec() executes //loop event handling - The program waits for the user's actions and performs their processing. )

As you can see, the elements that make up interfaces in Qt have their own position and size - the so-called "geometry" - and thus occupy the corresponding rectangular area on the screen (see Fig. 13.1). Also, each of the elements has settings that determine its behavior and appearance.


Rice. 13.1.

To create a structure, widgets are organized into a hierarchy according to the "part - whole" principle. Each of the widgets can contain other widgets. Such a visual element becomes the "parent" (parent widget) of the elements it contains. Note that such relationships should not be confused with inheritance in C++, relationships between classes in a program. Relationships between widgets are relationships between objects. This relationship has several implications:

  • the parent element will be responsible for deleting the child element: if the parent widget is deleted, it will automatically delete all child elements;
  • parent widget places child widgets inside itself, parts of child widgets that go outside the parent will be invisible;
  • part of the state of the parent widget is passed to the child ones - this concerns some properties (visibility, activity) and styles that are applied to the visual element.

Widgets that do not have a parent (top-level widgets) appear as separate windows in the program. Consider an example. Let's name the new project ParentExample . The project file will contain the usual settings for a GUI project:

TEMPLATE = app TARGET = ParentExample QT += widgets

For the widget that we will use as the main window, we will create a new class. For this category Files and Classes (Files and classes) select the C++ section and select the C++ Class (see Figure 13.2).

The next step is to create some elements on the window. To do this, open the parentwidget.cpp file and change the class constructor code. To display elements, it is enough to create them in the class constructor and set ParentWidget as the father of them. The parentwidget.cpp code looks like this:

#include "parentwidget.h" #include #include #include ParentWidget::ParentWidget (QWidget * parent) : QWidget (parent) ( //Create a label, specifying the parent widget - this, that is, an instance of the ParentWidget class. QLabel * lLabel=new QLabel (this); //Position relative to the upper left corner of the parent widget lLabel ->setGeometry(50, 0, 100, 30); lLabel ->setText("TextLabel"); //Text on the label. //Create a button, set parent, geometry and text QPushButton * lPushButton = new QPushButton (this); lPushButton->setGeometry (50, 50, 100, 30); lPushButton->setText (" PushButton "); // Create an input field, set the "parent", geometry and text QLineEdit * lLineEdit = new QLineEdit ( this); lLineEdit ->setGeometry(50, 100, 100, 30); lLineEdit ->setText("LineEdit"); lLineEdit ->selectAll(); //Select the text in the input field (just for example) //Finally change parent widget size setGeometry (x (), y (), 300, 150); //and set window title text setWindowTitle (" parent widgetExample "); )

Because the parent element is ParentWidget , the label (QLabel ), button (QPushButton ), and text field (QLineEdit) are within it. The position of child widgets is set relative to the upper left corner of the parent. This can be easily verified by changing the size and position of the window of our program. Notice how we created the user interface elements in heap memory using the new operator. This ensures that the elements are not removed after the ParentWidget constructor completes.


2022
maccase.ru - Android. Brands. Iron. news