NO MFC, ONLY API
In DOS programming, it is agency that I like to know all things, transforming to Windows programming, still likes to clarify things, I originally selected VC6, but because MFC is vast, let me think that Windows programming is MFC programming, after studying for a while, found that MFC is not a good tool for beginners, because MFC is based on class, using the nature of the use, so that beginners can't do it where they start How to pass messages, how to call the process. But at the same time, it is undeniable if you are a C master, clearly understand OO thinking, then use the MFC development project will make your work efficiency significantly. If you are not like this, don't try to start using MFC, it will make you confused, and there is no progress.
Wrap the MFC, you can write outstanding Win32 applications - using the API.
Many people are reluctant to use the API directly, they think the API is too low, the quantity is huge, it is not easy to master. But according to my learning experience, Although the API is huge, it is not big. After a certain practice, you can use the API to make your work very well.
The following explanation begins with the most basic API creates a simple Win32 program. If you are interested, you follow me.
Example 1: Create a "Hello World" window program
Because it is the first contact to use Win32 API programming, you need to introduce how to start your program, and how to configure your Visual C environment.
First, create a new project, select the Win32 Application option, which is the necessary beginning of the Win32 API program design, then enter the project name to enter the next step, just as long as the A Simple Win32 Application is good. Here's the coding phase, you need to configure your project, open the Project menu, settings option, select the Link tab, see if you have the corresponding header file in the program. LIB is listed, if you don't add it, this is Make sure there is no link error.
When starting the project, since the "A Simple Win32 Application" option is selected, the basic file of the project has been established, and you can fill in the code directly to WinMain, and the source program is provided below:
#define win32_Lean_and_mean // Description This is not an MFC program
#include "stdafx.h" // contains the corresponding header file
/ / Declared this function as the process of processing window called by Windows
LResult Callback MainProc (HWND, UINT, WPARAM, LPARAM);
// Define the application name and class name
Char szappname [] = "begin";
Int apientry Winmain (Hinstance Hinstance, // Application Handle
Hinstance Hprevinstance,
LPSTR LPCMDLINE, / / Command line parameters
INT ncmdshow) // window display method
{
MSG msg; // message variable
HWND HWND; // Main window handle
WNDCLASSEX WC;
// WNDCLASSEX structure initialization
wc.cbclsextra = 0;
wc.cbsize = sizeof (wndclassex);
Wc.cbwndextra = 0; wc.hbrbackground = (hbrush) getStockObject (White_brush);
Wc.hcursor = loadingCursor (NULL, IDC_ARROW);
Wc.hicon = loading;
Wc.hiconsm = 0;
wc.hinstance = hinstance;
Wc.lpfnWndProc = (WndProc) mainproc;
wc.lpszclassname = szappname;
Wc.lpsz GeneNuname = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW |
CS_DBLCLKS | CS_OWNDC;
// Register window class
RegisterClassex (& WC);
// Establish a main window
HWND = CREATEWINDOWEX (0, Szappname, Szappname, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, HINSTANCE, NULL;
// Display the main window
ShowWindow (HWND, NCMDSHOW);
UpdateWindow (HWND);
// Enter the event loop
While (GetMessage (& MSG, NULL, 0, 0))
{// translation and distribute the message
TranslateMessage (& MSG);
DispatchMessage (& MSG);
}
// Return to Windows below the program
Return msg.wparam;
}
/ / This function is called by Windows to process a message.
LResult Callback Mainproc (HWND HWND, UINT MESSAGE,
WPARAM WPARAM, LPARAM LPARAM)
{
HDC HDC; // Draw any object in the window customer area, use this handle (Handle device context)
Paintstruct PS;
Switch (Message)
{
When the CASE WM_CREATE: / / window is established, this message is sent
Return 0;
Case WM_PAINT: // Send this message when the window is recovered, move, change, change, etc.
HDC = BeginPaint (HWND, & PS);
Textout (HDC, 0, 0, "Hello World", 11);
Endpaint (hwnd, & ps);
Return 0;
Case WM_Close: // The user selects the action of the shutdown window
PostquitMessage (0);
Return 0;
}
// Process the message with the default function
Return DefWindowProc (Hwnd, Message, WPARAM, LPARAM);
}
The frame structure of the window-based Win32 API program is like this. As for the menu, toolbar, status bar, toolbar prompt, etc., I will come back later.
Example 2: "Hello World" program based on dialog
There are two ways to create the dialog and its control: 1. Create in the resource editor; A little strange, it changed the traditional design interface, as long as the mouse control can make a pleasing interface. I want to introduce the second method, the source program is as follows: #define win32_Lean_and_mean
#include "stdafx.h"
#include "resource.h" // Contains the header file that defines the control identifier
// Handle the function of the dialog message
Bool WinApi MAINDLG (HWND, UINT, WPARAM, LPARAM);
Hinstance hins; // global application handle, convenient for each program
Int apientry Winmain (Hinstance Hinstance,
Hinstance Hprevinstance,
LPSTR LPCMDLINE,
INT ncmdshow)
{
MSG msg;
Hins = hinstance;
// Create a dialog
Dialogbox (Hinstance, makeintResource (IDD_DIALOG),
NULL, (DLGPROC) MAINDLG;
// Enter the message loop
While (GetMessage (& MSG, NULL, 0, 0))
{
TranslateMessage (& MSG);
DispatchMessage (& MSG);
}
Return msg.wparam;
}
// Dialog Box Message Processing Function
Bool WinApi MAINDLG (HWND HDLG, UINT MESSAGE,
WPARAM WPARAM, LPARAM LPARAM)
{
Switch (Message)
{
Case WM_INITDIALOG:
// Initialization dialog
CreateWindow ("Static", "Hello World", WS_CHILD | WS_VISible,
160, 100, 200, 50, HDLG, NULL, HINS, NULL);
Return True;
Case WM_COMMAND:
Switch (loword (wparam))
{
Case IDOK:
Case IDCANCEL:
EndDialog (HDLG, 0); // End dialog
PostquitMessage (0);
Break;
}
Return True;
}
Return False;
}
There are the following contents in the resource file
IDD_DIALOG DIALOG DISCARDABLE 0, 0, 188, 98
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
Caption "Dialog"
Font 10, "System"
Begin
Defpushbutton "OK", IDOK, 25, 80, 40, 9
Pushbutton "Cancel", IDCANCEL, 65, 80, 40, 9
End
The dynamic creation of the control in the dialog is implemented with the function createWindow, and the creation and use of other controls will be seen later.
When the dialog function processes some message, it should return TRUE, which is reversed, and returns false.
This talk tells the simple Win32 programming framework, and there is no more difficult knowledge. If you can accept and like this Windows program design method, please learn together with me.