Getting started with Windows Programming

Source: Internet
Author: User

Since the last time I made the SDK black and white games, I have been very worried about the MFC. Recently, some children's shoes have been "struggling". No way, I have to study the content of windows program design. This is only for entry-level players. If you are a great player, it will not waste your precious time. Of course, if you are a cainiao like me, so please follow me to unveil the secrets of windows programming.

It should be emphasized that if you want to write a windows program, you need to know the SDK or MFC. If you do not know the term, please use "Baidu google, language is a tool for communication, but the mainstream is C or C ++. C can use SDK or MFC to design windows programs, but you must understand the object-oriented concept before using MFC. C ++ may feel strange if it is used to design the SDK program. Let's take a look at the figure below:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-right -: "title =" image "border =" 0 "alt =" image "width =" 432 "height =" 407 "src =" http://www.bkjia.com/uploads/allimg/131228/1JK5A93-0.png "/>

This is a figure I drew by imitating Cheng binghui. From the figure, you can easily see that the difference between SDK and MFC is that the SDK calls API functions directly, while the MFC uses class encapsulation to call APIs, of course, it can also be called directly.

I will not talk much about it, because the old black and white games I designed were designed with sdks. So here I will explain the SDK design method first, the design idea of MFC will be coming soon.

We can simply think that windows programs are message-based event drivers. this is my personal opinion. If you have any questions, please forgive me ), every operation we make on the computer may trigger the program to send a message. For example, if we move the mouse, we may send the WM_MOUSEMOVE message. This message will not be taken care of for the time being and will be discussed later. Why is it possible? Because we can ignore this operation when designing a program, which is what we often say is not responding to user operations. The event that we often say is triggered when the sent message is processed by the processing function. It may be said that some people are dizzy. If you are dizzy, you can directly look down.

Why does Microsoft use windows? Because most programs exist in the form of a window, if the browser you open is a window, the windows program we designed should obviously exist in this form, in fact, the SDK program structure is very simple. There are two main functions, one is the main function WinMain). This function is the same as the main function in C, it is essential for general windows programs. Another function is the message processing function. It can be named as WinProc ). As long as we write these two functions, a simple windows program will work. How do we write these two functions?

First, let's look at the main structure of the WinMain function:

Int WinMain (......)
{
......
RegisterClass (......); -- Register the window class
CreateWindow (......); -- Create window
ShowWindow (......); -- Display window
UpdateWindow (......); -- Update window
......
While (GetMessage (......)) -- Receive and process messages cyclically
{
TranslateMessage (......);
DispatchMessage (......);
}
Return (......);
}

As ...... What is written in it, which will be discussed later. Here you will say: um ~ Sounds great ~, Except for the red functions. Okay, so let's not worry about those functions. Of course, their functions can be seen from their names that the naming method from annotations is really good ~).

Let's take a look at our WinProc function:

Long CALLBACK WinProc (......)
{
......
Switch (message)
{
Case WM_MOUSEMOVE:
{......; Break ;}
Case WM_KEYDOWN:
{......; Break ;}
......
Default:
{Return DefWindowProc (......);}
}
Return (0 );
}

Then you will say: Well, I have seen the messages starting with WM, and here is a case-switch statement. Yes, what I mark in blue is a small unit for processing messages. In this case statement, the WM_MOUSEMOVE message is matched first, and then the processing function is written in the case for processing, wait for the next message to arrive.

The above two functions are not difficult to understand. I have provided a simple and complete framework in the attachment, except that the content of messages received in WinProc is not written, if you are interested, you can download it and see it. If you can understand it, I suggest you read my previous blog article on black and white games! After reading the code, you will have a perceptual knowledge of the SDK Design for windows programs. Next, I suggest you take a look at this windows programming material, now, you can easily understand his Hello world example. Of course, I am also studying and will continue to share with you the difficulties and joys I have encountered in my study. I hope you can make progress together.

This article from the "cainiao surfaced" blog, please be sure to keep this source http://rangercyh.blog.51cto.com/1444712/397568

Related Article

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.