VC skill set (1)
He zhidan
1. Avoid the same random number. You can take the current time, read the file, and read any memory (such as undefined variables, which may be dangerous ).
Ctime T = ctime: getcurrenttime ();
Int n = T. getminute () * 60 + T. getsecond ();
While (n --> 0)
{
Rand ();
}
Const int n= 100;
Int result = rand () % N;
2. String and number Conversion
Sprintf and sscanf actually have many other functions, but these two functions are the easiest to remember, because they are similar to printf, scanf, fprint, and fscanf.
Char STR [100];
Int x = 5;
Sprintf (STR, "% d", X );
Sscanf ("10", "% x", & X );
3. check whether a key is in the pressed State and whether it has been pressed.
Short key = getasynckeystate (vk_f11 );
0x8001 is in the pressed status
One or two calls to getasynckeystate (vk_f11);, F11 is pressed.
Getasynckeystate, getkeystate specifies the virtual code as vk_lbutton, and vk_rbutton can determine whether the mouse has been pressed
4. How to delete a class.
Method 1:
1. delete the file.
2. Open the Class Wizard (CTRL + W). If the Class Wizard cannot find the file, a dialog box "Remove" will pop up.
Method 2:
1. Delete in FileView.
2. Physical deletion.
5. The custom class is gone:
Project-> Add to project-> files
6. Use pi
Include math. h
# DEFINE _ use_math_defines
# Include "math. H"
Then m_pi is pi.
7. How can I obtain the volume label of a partition ??
Char Buf [max_path];
Getvolumeinformation ("C: //", Buf, max_path, null, null );
Afxmessagebox (BUF );
8. Open a webpage
ShellExecute (: getshorttopwindow (), _ T ("open"), // SK modified for Unicode
_ T ("http://www.sohu.com"), null, null, // SK modified for Unicode
Sw_maximize );
9. When it is closed, a dialog box is displayed for confirmation.
Void cmainframe: onclose ()
{
If (idok! = MessageBox ("do you want to exit? Click confirm to exit "," Do you want to exit? ", Mb_okcancel ))
Return;
Cframewnd: onclose ();
}
10. Menu issues
Id_file_open
In resource accelerator:
ID key
Id_file_open Ctrl + O
Press Ctrl + O in the program to activate the Open menu (if the File menu is opened, it will not work ).
In the resource menu:
The title of id_file_open is open (& O).../tctrl + O
& O indicates that when its parent menu (File menu) is opened, O activates the File menu.
11. Is there a problem with sizeof ??
Class B
{
Bool;
Bool C;
Int B;
}; // Sizeof (B) = 8
Layout:
1 2 3 4
Bool Filling
1 2 3 4
Int ...............
Clear at a glance .:)
At this time, the compiler only inserts two Padding Bytes after C. Because B occupies four bytes, it automatically fills in the binary of a word.
This is the alignment of the compiler, as long as you add
# Pragma 1
So that one byte can be aligned.