WPF programming: Chapter 1 Application and window)

Source: Internet
Author: User

The application and the window

Part OneApplicationProgramAnd window

1. Note that C # Is written directly here #Code(Console Program), that is, writing programs in the text mode. Of course, you can use Visual Studio 2008 IDE for writing faster. It is recommended that you use the former to learn about WPF application development.

For some learners, if you are still using VS 2005, you can create an application as follows:

1. Select "new project" from the "file" menu ";

2. In the "new project" dialog box, select "Visual C #"-"Windows"-"Empty Project" (empty project ). After the Directory and project name are specified

And then press "OK ".

In this way, you can create an empty project.

 

2. To develop applications for Windows Presentation Foundation (WPF), you need to build the application object and window object. The following is a simple WPF program:

 

// -----------------------------------------
// Sayhello. CS (c) 2008 by Jack Dong
// -----------------------------------------
Using System;
Using System. windows;

Namespace Jackdong. sayhello
{
Class Sayhello
{
[Stathread] // Indicates that the COM thread model of an application is a single-threaded unit (STA)
Public   Static   Void Main ()
{
Window win =   New Window ();
Win. Title =   " Sayhello " ;
Win. Show ();

Application app =   New Application ();
App. Run ();
}
}
}

 

(1) Use Using to compile the directiveSystem. WindowsThe namespace is added to the project. The namespace contains all basic WPF categories, schemas, interfaces, delegates, and enum ), it includes two classes: Application and windows.

(2) Other WPF namespaces start with system. Window.

For example, system. Windows. Controls, system. Windows. input, and system. Windows. Media. Only system. Windows. forms is an exception. It is mainly the namespace of windows. forms.

(3) Except for the system. Windows. Forms. Integration namespace, classes are used to integrate windows. Forms and WPF programs.

For. Forms and WPF programs, all other namespaces starting with system. Windows. forms belong to the traditional windows. Forms namespace.

(4) For the project created in the preceding method, you must include "presentationcore", "presentationframework", and "system" in "Solution Explorer"

And "windowsbase ".

Run the above Code. The main class is used to create a window class object at the beginning to create a standard application window. Title property is the text displayed in the title bar of the window, and the show method displays the window on the screen.

The most important step here is to call the run method of the Application object. In the traditional thinking of Windows programming, this aims to establish a message loop so that the application can accept the user's keyboard or mouse input.

When closing the window created by sayhello, you will find that a console window is also running. This is from the compilation option settings. You can modify this compilation option in the project property. Click the project name and select "property" from the pop-up menu ". Now you can view the various settings of the project, of course, you can also change the settings. Note that "output type" (output type) is set to "console application" (console application). Obviously, this setting does not prevent the use of console programs to create GUI windows. Set this to "Windows application", and the program will run smoothly. This time, the console window will no longer appear. I think the console window is quite useful in the development phase. When running the program, we can use it to display some text information for debugging. If there are too many bugs in the program, you cannot display the GUI window or enter an infinite loop. In this case, you only need to type ctrl + C in the console window to close the program easily. These are the incidental advantages of the console window.

 

3. In a program, only one application object can be created. For other parts of the program, this application object acts as a fixed anchor. You cannot see the application object on the screen, but you can see the window object. Window objects appear on the screen. These are normal Windows System Windows. The title attribute value is changed to the text in the title bar. The system menu is on the left of the title bar, maximize, minimize, and close the window icon on the right.

This window has a border that can be adjusted. A large area of the window is occupied by a client area.

Note that the run call must be retained at the end. Once the run method is called, no result is returned, until the window is closed. After the run operation returns, the main method will be used for sleep. The Windows operating system will do some cleanup work. After the program calls the run method, it will actually start running, because only after the run method is called, the window object can respond to user input. When the user closes the window, the run method returns and the program preparation ends.

 

In this way, almost all the time is spent in running the program. After initialization, almost all the things that the program does are actually responding to various events. They are usually about the mouse, keyboard, and other events.

 

1. mousedown event,

(1) event Processor

It must comply with the mousebuttoneventhandler delegate, that is, the first parameter type is object, the second type is mousebuttoneventargs, and the class is defined in the system. Windows. Input namespace.

(2) Use

This event occurs when the customer clicks the mouse in the customer area of the window.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.