Trivial windows programming knowledge points

Source: Internet
Author: User
Tags ranges

C language Time Functions
C Standard library functions include a series of date and time processing functions, which are described in the header file. These functions are listed below. Three types are defined in the header file: time_t, struct TM, and clock_t.

C language time functions described in

Time_t time (time_t * timer );

Double difftime (time_t time1, time_t time2 );

Struct TM * gmtime (const time_t * timer );

Struct TM * localtime (const time_t * timer );

Char * asctime (const struct TM * timeptr );

Char * ctime (const time_t * timer );

Size_t strftime (char * s, size_t maxsize, const char * format, const struct TM * timeptr );

Time_t mktime (struct TM * timeptr );

Clock_t clock (void );

 

The following is the time function set I collected from the Internet.

 

 

Asctime (represents the time and date in string format)
Related functions
Time, ctime, gmtime, localtime
Header file
# I nclude
Define functions
Char * asctime (const struct TM * timeptr );
Function Description
Asctime () converts the information in the TM structure referred to by the timeptr parameter to the time and date representation method used in the real world, and then returns the result in string form. This function has been converted from the time zone to the local time. The string format is: "wed Jun 30 21:49:08 1993/N"
Return Value
If you call the related time and date functions again, this string may be damaged. The difference between this function and ctime is that the input parameters are different structures.
Additional instructions
Returns a string representing the current local time and date.
Example
# I nclude
Main ()
{
Time_t timep;
Time (& timep );
Printf ("% s", asctime (gmtime (& timep )));
}
Run
Sat Oct 28 02:10:06 2000
 

 

Ctime (Representation of time and date in string format)
Related functions
Time, asctime, gmtime, localtime
Header file
# I nclude
Define functions
Char * ctime (const time_t * timep );
Function Description
Ctime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result in string form. This function has been converted from the time zone to the local time. The string format is "wed Jun 30 21: 49: 08 1993/N ". If you call the related time and date functions again, this string may be damaged.
Return Value
Returns a string representing the current local time and date.
Example
# I nclude
Main ()
{
Time_t timep;
Time (& timep );
Printf ("% s", ctime (& timep ));
}
Run
Sat Oct 28 10: 12: 05 2000
 

 

Gettimeofday (get current time)
Related functions
Time, ctime, ftime, settimeofday
Header file
# I nclude
# I nclude
Define functions
Int gettimeofday (struct timeval * TV, struct timezone * tz)
Function Description
Gettimeofday () will return the current time with the structure indicated by TV, and put the information of the local time zone in the structure indicated by tz.
The timeval structure is defined:
Struct timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* microseconds */
};
The timezone structure is defined:
Struct timezone {
Int tz_minuteswest;/* the time difference between Greenwich and */
Int tz_dsttime;/* state of daylight saving time */
};
The above two structures are defined in/usr/include/sys/time. h. The status of tz_dsttime is as follows:
Dst_none/* not used */
Dst_usa/* United States */
Dst_aust/* Australia */
Dst_wet/* Western Europe */
Dst_met/* Central Europe */
Dst_eet/* Eastern Europe */
Dst_can/* Canada */
Dst_gb/* Great Britain */
Dst_rum/* Romania */
Dst_tur/* Turkey */
Dst_austalt/* Australia (after January 1, 1986 )*/
Return Value
If the call succeeds, 0 is returned. If the call fails,-1 is returned. The error code is stored in errno. Note that the memory space specified by the efault pointer TV and TZ exceeds the access permission.
Example
# I nclude
# I nclude
Main (){
Struct timeval TV;
Struct timezone TZ;
Gettimeofday (& TV, & Tz );
Printf ("TV _sec; % d/N", TV,. TV _sec );
Printf ("TV _usec; % d/N", TV. TV _usec );
Printf ("tz_minuteswest; % d/N", tz. tz_minuteswest );
Printf ("tz_dsttime, % d/N", tz. tz_dsttime );
}
Run
TV _sec: 974857339
TV _usec: 136996
Tz_minuteswest:-540
Tz_dsttime: 0
 

 

Gmtime (get current time and date)
Related functions
Time, asctime, ctime, localtime
Header file
# I nclude
Define functions
Struct TM * gmtime (const time_t * timep );
Function Description
Gmtime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result from the structure TM.
The structure TM is defined
Struct TM
{
Int tm_sec;
Int tm_min;
Int tm_hour;
Int tm_mday;
Int tm_mon;
Int tm_year;
Int tm_wday;
Int tm_yday;
Int tm_isdst;
};
Int tm_sec indicates the current number of seconds. The normal range is 0-59, but the value can be 61 seconds.
Int tm_min indicates the current score, range: 0-59
Int tm_hour, which is counted from midnight, ranges from 0 to 23.
The number of days in the current month of int tm_mday, ranging from 01 to 31.
Int tm_mon indicates the current month. The value ranges from 0 to 11 from January 1, January.
Int tm_year: The number of years since January 1, 1900
The number of days in a week for int tm_wday. The value ranges from 0 to 6 from Monday.
Int tm_yday indicates the number of days since January 1, January 1 this year. The value range is 0-365.
Int tm_isdst time saving flag
The time and date returned by this function are not converted by the time zone, but UTC time.
Return Value
The returned structure TM indicates the current UTC time.
Example
# I nclude
Main (){
Char * wday [] = {"sun", "mon", "Tue", "wed", "Thu", "fri", "sat "};
Time_t timep;
Struct TM * P;
Time (& timep );
P = gmtime (& timep );
Printf ("% d", (1900 + P-> tm_year), (1 + P-> tm_mon), p-> tm_mday );
Printf ("% S % d; % d/N", wday [p-> tm_wday], p-> tm_hour, p-> tm_min, p-> tm_sec );
}
Run
2000/10/28 sat 8:15:38
 

 

Localtime (obtain the current local time and date)
Related functions
Time, asctime, ctime, gmtime
Header file
# I nclude
Define functions
Struct TM * localtime (const time_t * timep );
Function Description
Localtime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result from the TM structure. For the definition of the structure TM, see gmtime (). The time date returned by this function has been converted to the local time zone.
Return Value
The returned structure TM indicates the current local time.
Example
# I nclude
Main (){
Char * wday [] = {"sun", "mon", "Tue", "wed", "Thu", "fri", "sat "};
Time_t timep;
Struct TM * P;
Time (& timep );
P = localtime (& timep);/* obtain the local time */
Printf ("% d", (1900 + P-> tm_year), (L + P-> tm_mon), p-> tm_mday );
Printf ("% S % d: % d/N", wday [p-> tm_wday], p-> tm_hour, p-> tm_min, p-> tm_sec );
}
Run
2000/10/28 sat 11:12:22
 

 

Mktime (the number of seconds after the time structure data is converted)
Related functions
Time, asctime, gmtime, localtime
Header file
# I nclude
Define functions
Time_t mktime (strcut TM * timeptr );
Function Description
Mktime () is used to convert the TM structure data referred to by the timeptr parameter to the number of seconds that have elapsed since 00:00:00, January 1, January 1, 1970 AD.
Return Value
Returns the number of seconds that have elapsed.
Example
/* Obtain the time (in seconds) with time (), and use localtime ()
Convert to struct TM and then use mktine () to convert struct TM to the original number of seconds */
# I nclude
Main ()
{
Time_t timep;
Strcut TM * P;
Time (& timep );
Printf ("Time (): % d/N", timep );
P = localtime (& timep );
Timep = mktime (P );
Printf ("Time ()-> localtime ()-> mktime (): % d/N", timep );
}
Run
Time (): 974943297
Time ()-> localtime ()-> mktime (): 974943297
 

 

Settimeofday (set the current time)
Related functions
Time, ctime, ftime, gettimeofday
Header file
# I nclude
# I nclude
Define functions
Int settimeofday (const struct timeval * TV, const struct timezone * tz );
Function Description
Settimeofday () sets the current time to the structure information indicated by TV, and the local time zone information to the structure indicated by tz. For more information, see gettimeofday (). Note: Only the root permission can use this function to modify the time.
Return Value
If the call succeeds, 0 is returned. If the call fails,-1 is returned. The error code is stored in errno.
Error Code
The root permission of eperm is not enough to call settimeofday.
The einval time zone or data is incorrect and the time cannot be set correctly.
 

 

Time (obtain the current time)
Related functions
Ctime, ftime, gettimeofday
Header file
# I nclude
Define functions
Time_t time (time_t * t );
Function Description
This function returns the number of seconds that have elapsed since January 1, 1970 ad utc time since 00:00:00. If t is not a null pointer, this function also saves the returned value to the memory indicated by T pointer.
Return Value
The number of seconds is returned for success, and (time_t)-1) is returned for failure. The cause of the error is stored in errno.
Example
# I nclude
Mian ()
{
Int seconds = Time (time_t *) null );
Printf ("% d/N", seconds );
}
Run
9.73e + 08


VC Window refresh invalidaterect and updatewindow RedrawWindow2010-01-12 first talk about wm_paint this important message:

 
The wm_paint message is generated by the system and shocould not be sent by an application. The system sends this message when there are no other messages in the application's Message Queue

That is to say, the wm_paint message is generated by the system. The wm_paint message must be sent only when the message queue of the application is empty, and the message should not be sent by the Program (write your own code using sendmessage.

When the updatewindow function is called, or the window detects that the window is overwritten and needs to be restored, for example, when the window is created for the first time, the window size is changed to maximize, minimize (in fact, when these events occur, the updatewindow function is called to send the wm_paint message), which will send a wm_paint message to the user program. After the wm_paint message is received during the Window Process, it does not mean that the entire customer zone needs to be refreshed. It is possible that the customer zone has only one small area, which is called "invalid area ", the program only needs to update this region. Similar to the wm_timer message, the wm_paint message is also a low-level message. Although it is not discarded like the wm_timer message, windows always puts wm_paint in the message when the message loop is empty.

The coordinates of the invalid region are not included in the parameter of the wm_paint message. Other methods can be obtained in the program. The wm_paint message only needs to be updated in the region of the notification program. Therefore, Windows does not add two wm_paint messages to the message loop at the same time. When Windows needs to put a wm_paint message, if an invalid region exists, it only needs to combine the old and new invalid regions to calculate an invalid region, in a message loop, only one wm_paint message is required.

In fact, Windows maintains a "Drawing Information Structure" for each window, and the coordinates of invalid regions are in it. If Windows finds an invalid region when the message loop is empty, A wm_paint message is placed. How can we obtain the "Drawing Information Structure? The second parameter of the beginpaint function is a buffer address of the drawing information structure. Windows returns the drawing information structure, which contains the location and size of the invalid area, the structure of drawing information is defined as follows:

Typedef struct tagpaintstruct {// PS
HDC;
Bool ferase;
Rect rcpaint;
Bool frestore;
Bool fincupdate;
Byte rgbreserved [32];
} Paintstruct;

The HDC field is the device environment handle of the window, and the rcpaint field is a rect structure, which specifies the diagonal vertex of the rectangle in the invalid area (if there is a () at the beginning ), (), now there is another (20, 20), (60, 30), after splicing, It is (), (60, 40 ))), if the ferase field is a non-zero value, it indicates that Windows has used the background color to erase the invalid area before sending the wm_paint message. The last three fields are used in windows, and the application does not have to ignore them.

In some cases, part of the display area is temporarily overwritten. Windows tries to save a display area and restore it later, but this may not succeed. Windows may send the wm_paint message: Windows erased the dialog box or message box that overwrites some windows. The menu is pulled down and then released. A prompt message is displayed.

In some cases, windows always saves the covered display area and restores it. In these cases, the mouse cursor crosses the display area, and the icon is dragged over the display area.

Sometimes the application also needs to be able to actively trigger the Drawing operation in the window. For example, when the data displayed in the window changes, this is generally done through the invalidaterect and invalidatergn functions. Invalidaterect and invalidatergn Add the specified region to the update Region Window. If the application's message queue has no other messages, if the update region of the window is not empty, the system automatically generates the wm_paint message.

Why does the system not send the wm_paint message when invalidate is called? Why do wm_paint messages need to be sent only when the application message queue is empty? This is because the system regards the painting operation in the window as a low-priority operation, so we do it as much as possible. However, this will also improve the rendering efficiency: the partition domains that invalidate the two wm_paint messages through invalidaterect and invaliatergn will be accumulated and updated once in a wm_paint message, this not only avoids repeated updates to the same region, but also optimizes the application update operations. Such as invalidaterect and invalidatergn to make the window area ineffective, relying on the system to send the wm_paint message at the right time is actually an asynchronous way of working, that is, there is a delay between the Invalid Window area and the wm_paint message. Sometimes this delay is not what we want, in this case, we can use sendmessage to send a wm_paint message after the Invalid Window area to force re-painting immediately, but it is better to use Windows GDI to provide us with more convenient and powerful functions: updatewindow and redrawwindow. Updatewindow checks the update region of the window and sends the wm_paint message only when it is not empty. redrawwindow gives us more control: whether to redraw non-customer areas and backgrounds, whether the wm_paint message is always sent, regardless of whether the update region is empty or not.

Beginpaint

Today, a low-level error occurred when processing the wm_paint message, and it took me an hour to find the cause. When processing messages, I didn't use the beginpaint and endpaint functions. As a result, other messages cannot pop up, and the windows are blinking (in fact, re-painting ). Later, I found the answer on msdn and posted the original statement. (In the wm_paint message title of msdn)

Beginpaint sets the update region of a window to null. this clears the region, preventing it fromgenerating subsequent wm_paint messages. if an application processes a wm_paint message but does not call beginpaint or otherwise clear the update region, the application continues to receive wm_paint messages as long as the region is not empty. in all cases, an application must clear the update region before returning from the wm_paint message.
The beginpaint function is used to set the blank area (that is, update region) of the window to be repainted ). Under normal circumstances, after we receive the wm_paint message, the update region of the window is non-empty (if it is empty, we do not need to send the wm_paint message ). When you respond to this message without calling beginpaint to clear it, the update region of the window is always non-empty, and the system will always send the wm_paint message. In this way, an endless loop is formed for processing wm_paint messages.

Beginpaint and wm_paint messages are closely related. What if you don't write beginpaint in the wm_paint processing function? The program will achieve amazing CPU usage as it enters an endless loop, and you will find that the program is always processing one by one wm_paint message. This is because in general, when the application receives the wm_paint message, the update region of the window is not empty (if it is empty, the wm_paint message does not need to be sent ), one function of beginpaint is to set the update region to null, so that if you do not call beginpaint, the update region of the window will not be empty, as described earlier, the system will always send the wm_paint message.

Beginpaint and wm_erasebkgnd messages are also related. When the update region of the window is marked as the background to be erased, beginpaint will send the wm_erasebkgnd message to re-draw the background, at the same time, there is a flag in the returned information indicating whether the background of the window has been re-painted. When we use invalidaterect and invalidatergn to add a specified region to update region, we can set whether the region needs to be erased, so that the next beginpaint will know whether to send the wm_erasebkgnd message.

Of course, there is still a lot of knowledge to learn about the wm_paint message. In addition, beginpaint can only be used in the wm_paint processing function, and after the beginpaint function is called, do not forget to call the endpaint function. They are a pair.

Redraw the invalidaterect, invalidate, updatewindow, and redrawwindow functions.

Invalidaterect (partial region) and invalidate (entire window) are only used to set invalid region, but do not redraw the window.

Updatewindow: check whether there are any invalid areas in the window. If yes, immediately send a wm_paint message to the window and re-draw the window.

Redrawwindow is equivalent to calling invalidaterect first, and then calling updatewindow. In addition, redrawwindow provides some functions that cannot be done by the first two.

If you do not call invalidaterect to call updatewindow, updatewindow will not do anything, because there is no invalid region. If you do not call updatewindow after invalidaterect is called, the system automatically sends a wm_paint message when the window message queue is empty.


Int_ptr dialogbox (hinstance,
Lptstr lptemplate,
Hwnd hwndparent,
Dlgproc lpdialogfunc
);
This function creates a mode Dialog Box Based on the resources in the dialog box. This dialog box should end with enddialog.

The handle of the current hinstance application instance.
The lptemplate identifies the template resources of the dialog box. There are two ways to use it: one is to forcibly convert the ID of the dialog box template to the lpctstr, and the other is to use the makeintresource macro to obtain the ID.
The handle of the hwndparent parent window.
Message Processing Function in the lpdialogfunc dialog box.

Hwnd createdialog (hinstance,
Lptstr lptemplate,
Hwnd hwndparent,
Dlgproc lpdialogfunc
);
This function creates a non-mode Dialog Box Based on the resources in the dialog box. This dialog box should end with destroywindow.
The function parameters are used in the same way as dialogbox.

The Mode dialog box is generally generated in the stack, so the enddialog may only hide the window and not destroy it. When the objects in the mode dialog box exit the lifecycle, the destroy dialog box is displayed. Non-mode dialog boxes are generally in the heap, so you must use destroywindow to destroy them.
The dialogbox function processes the message loop by itself (this message loop is maintained in user32.dll and cannot be seen). After the dialog box is closed, the function returns (the return value is the second parameter of enddialog, therefore, the second parameter of enddialog can be used to identify the subcontrol ID), while the createdialog function calls the createmediawex function to create a window and return immediately, then, this window uses the message loop of the main window (that is, the messages generated by this window may be sent directly to the processing function of this window, or enter the message loop of the Main Window ).

For messages that do not want to be processed, defwindowproc should not be called for processing (otherwise there will be problems) in either mode or non-mode dialog box, because the system will take the initiative to process these messages. For messages that do not want to be processed, all the program needs to do is return false. For messages that have been processed, return true.
This is different from the processing of the main window. The main window also calls defwindowproc to process messages that do not want to be processed, and the return value of each Message Processing Branch is irrelevant (unlike the confirmation box, the return value must be true or false ).

The non-mode dialog box uses the same message loop as the main window. messages in the non-mode dialog box are processed by the system by calling the processing function of this dialog box, therefore, you should not convert or distribute the message in the message loop. You can change the message loop to the following:
// Main message loop:
While (getmessage (& MSG, null, 0, 0 ))
{
If (! Translateaccelerator (msg. hwnd, hacceltable, & MSG ))
{
// Messages that are not in the non-mode dialog box g_hflashwnd are delivered
If (! Isdialogmessage (g_hflashwnd, & MSG ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}

}
}

Createdialog sends the wm_initdialog message.

Some sample code:
/// Message processing function of the non-mode dialog box
Bool _ stdcall flashwndproc (hwnd, uint MSG, wparam, lparam)
{
Switch (MSG)
{
Case wm_initdialog:
Break;

Case wm_paint:
Break;

Case wm_lbuttondown:
Destroywindow (hwnd );
Break;

Default:
Return false; // messages that have not been processed
}

Return true; // processed
}

/// Create a non-mode dialog box
G_hflashwnd = createdialog (hinst, makeintresource (idd_flash), hwnd, (dlgproc) flashwndproc );
Showwindow (g_hflashwnd, sw_show );

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/bluesky_03/archive/2008/05/06/2403233.aspx


I. Differences in Creation
In Win32, the mode dialog box is generally created using dialogbox. The non-mode dialog box uses

Createwindow.

In MFC or wtl, the mode dialog box generally uses domodal, while the mode dialog box is created using create.

After the mode dialog box is created, other windows of the program cannot be operated. You must close the window before other windows can be operated. While

This is not required in the non-mode dialog box. It does not require immediate user response, but accepts user operations at the same time with other windows.

Ii. Differences between message responses
In terms of message response, the mode dialog box and the mode dialog box are very different.

When the mode dialog box is working, it has an internal message pump mechanism. The interaction between controls does not need to be manually controlled. The system will help me

.

The non-mode dialog box is similar to a normal window, and is driven by the message loop written in winmain. But because it is a dialog box, it

Special processing is available. Therefore, in a message loop, the dialog box must be provided with the opportunity to intercept messages.
While (getmessage (& MSG, null, 0, 0 ))
{
If (hdlgmodeless = 0 |! Isdialogmessage (hdlgmodeless, & MSG ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
}
If the received message is a dialog box message, isdialogmessage submits it to the dialog message processing function and returns true.

You do not need to send more messages.
Note: This method is not very easy to use, because when there are too many dialogs, it will be more troublesome to process. Another method is

Subclass controls are used to process interactions between controls.

Iii. Differences in destruction
The destroy mode dialog box uses enddialog, but the destroy mode dialog box uses destroywindow .. So we are destroying

When using the dialog box, you must also differentiate it.

In the non-mode dialog box, when you close the dialog box, the message processing function of the dialog box will receive the wm_close message, and then call

Destroywindow to destroy the non-mode dialog box.

Mode dialog box, usually returns the idok and idcancel. On PPC, we need to pay attention to this when handling the OK and X keys.

4. Others

Visibility:
The template of the non-modal dialog box must have the visible style; otherwise, the dialog box is invisible, and the modal dialog box does not need to set the style.

The more insurance method is to call showwindow (hdialog, sw_show) to display the dialog box, regardless of whether the dialog box has the visible style.

.

Obstructive:
Create returns the result immediately after the dialog box is displayed, while domodal returns the result only after the dialog box is closed. As we all know, in the MFC program

The lifetime of the window object should be longer than the corresponding window, that is, the corresponding window object cannot be

Is deleted. After the return result is "CREATE", you cannot determine whether the dialog box is closed. In this way, you cannot determine the lifetime of the dialog box objects,

Therefore, you have to build a dialog box object in the heap instead of a local variable.

 

 

 

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.