VC Skills 2

Source: Internet
Author: User

1. Execute the doscommand in VC
A.System ("md c: // 12 ");
B.Winexec ("cmd.exe/c md c: // 12", sw_hide );
C.ShellExecute
ShellExecute (null, "open", "d: // windows // system32 // cmd.exe", "/c md d: // Zzz", "", sw_show );
D.CreateProcess
The function in the following example can execute the given DOS command and record the output content in the buffer. Demonstrate the usage of anonymous pipeline redirection output at the same time:

Bool cdosdlg: execdoscmd ()
{
# Define execdoscmd "dir C:" // you can replace it with your command

Security_attributes SA;
Handle hread, hwrite;

SA. nlength = sizeof (security_attributes );
SA. lpsecuritydescriptor = NULL;
SA. binherithandle = true;
If (! Createpipe (& hread, & hwrite, & SA, 0 ))
{
Return false;
}

Char Command [1024]; // A 1 K Command Line. Enough.
Strcpy (command, "cmd.exe/C ");
Strcat (command, execdoscmd );
Startupinfo Si;
Process_information PI;
Si. cb = sizeof (startupinfo );
Getstartupinfo (& Si );
Si. hstderror = hwrite; // redirects the standard error output of the creation process to the pipeline input.
Si. hstdoutput = hwrite; // redirects the standard output of the creation process to the pipe input.
Si. wshowwindow = sw_hide;
Si. dwflags = startf_useshowwindow | startf_usestdhandles;

// Key steps. For meanings of parameters of the CreateProcess function, refer to msdn
If (! CreateProcess (null, command, null, null, true, null, & Si, & PI ))
{
Closehandle (hwrite );
Closehandle (hread );
Return false;
}
Closehandle (hwrite );

Char buffer [4096] = {0}; // use 4 K space to store the output content. As long as it is not the file content, it is generally enough.
DWORD bytesread;
While (true)
{
If (readfile (hread, buffer, 4095, & bytesread, null) = NULL)
Break;
// Buffer is the execution result. It can be saved to text or output directly.
Afxmessagebox (buffer); // the pop-up dialog box is displayed.
}
Closehandle (hread );
Return true;
}

2. delete a directory, including deleting subfolders and contents.
-------------------------------------------------
Bool deletedirectory (char * dirname) // Delete deletedirectory ("C: // AAA ")
{
Cfilefind tempfind;
Char tempfilefind [max_path];
Sprintf (tempfilefind, "% S // *. *", dirname );
Bool isfinded = (bool) tempfind. findfile (tempfilefind );
While (isfinded)
{
Isfinded = (bool) tempfind. findnextfile ();
If (! Tempfind. isdots ())
{
Char foundfilename [max_path];
Strcpy (foundfilename, tempfind. getfilename (). getbuffer (max_path ));
If (tempfind. isdirectory ())
{

Char tempdir [max_path];

Sprintf (tempdir, "% S // % s", dirname, foundfilename );
Deletedirectory (tempdir );
}
Else
{
Char tempfilename [max_path];
Sprintf (tempfilename, "% S // % s", dirname, foundfilename );
Deletefile (tempfilename );
}
}
}

Tempfind. Close ();
If (! Removedirectory (dirname ))
{
MessageBox (0, "failed to delete directory! "," Warning information ", mb_ OK); // For example, if the folder is not found and the deletion fails, delete the sentence.
Return false;
}

Return true;
}


Pause the program: System ("pause ");

Capture Keyboard Events in pretranslatemessage

If (PMSG-> message = wm_keydown & PMSG-> wparam = vk_return)
Return true; // pay attention to the return value.

Change the key message (the following code changes the Enter key message to the tab key message)
-------------------------------------------------------
Bool ct3dlg: pretranslatemessage (MSG * PMSG)
{

If (PMSG-> message = wm_keydown & vk_return = PMSG-> wparam)
{
PMSG-> wparam = vk_tab;
}

Return cdialog: pretranslatemessage (PMSG );
}

The problem of converting hexadecimal to a decimal number
Use a software to read binary files to read files
The 8f C2 F5 3C segment in the binary file is changed to 0.03 at last.
How is this converted ??
Method 1: floating point technology, such
Dword dw = 0x3cf5c28f;
Float d = * (float *) & DW; /// 0.03;
Method 2: The floating point storage method is the same as the integer storage method.
Http://www.zahui.com/html/1/3630.htm
Take a look, but we usually do not have to understand it to complete the conversion.
Char A [4] = {0x8f, 0xc2, 0xf5, 0x3c };
Float F;
Memcpy (& F, A, sizeof (float ));
Trace ("% d", 0x3cf5c28f );

Edit ControlEm_setsel, em_replacesel message

Monitor keyboard messages in other processes: Setwindowshookex (wh_keyboard_ll ,...);

Write anywhere on the desktop
--------------------------------------------------
HDC primary Dc =: getdc (0 );
Cstring stext = "my desktop ";
: Textout (deskdc, 100,200, stext, stext. getlength ());
: Releasedc (0, DC );

 

Hwnd thread_hwnd = findwindow (null, "process form to be monitored (view with spy ++ )"),
If (thread_hwnd = NULL )..........
Else DWORD thread_id = getwindowthreadprocessid (thread_hwnd, null)

 

Waveoutgetvolume ()The waveform volume can be obtained.


Hide the desktop icon and disable the right-click menu:
------------------------------------
Hwnd hwd =: findwindow ("progman", null );
If (bshowed)
: Showwindow (hwd, sw_hide );
Else
: Showwindow (hwd, sw_show );

Bshowed =! Bshowed;

When my program is currently running, you can use setcursor ()Set the cursor.
In addition, you can use setcapture () to move the cursor out of my program window, which is also the icon I set.
However, if my program is not the current running program, the mouse will change to default.
How can I change the settings to the default one, or use the cursor I set?
Setsystemcursor

Sendmessage Functions:
ControlTheThis is the case.
Sendmessage (N1, wm_command, makelparam (ID, bn_clicked), (lparam) N2); (N1, N2 is the handle)
WhileGet Text ContentYes,
Sendmessage (hwnd, wm_gettext, 10, (lparam) BUF ),
The wm_ctlcolor that processes a single row of Edit must respond to both nctlcolor = ctlcolor_edit and ctlcolor_msgbox at the same time, refer to the http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/vclib/html/_ mfc_cwnd.3a3a.onctlcolor.asp

Device change HandlerYou can capture wmdevicechange events in cwnd: ondevicechange. messages such as device insertion and unplugging cannot be distinguished.

The shgetfileinfo function obtains the file information.

Operations on sound files in VC: Http://www.pujiwang.com/twice/Article_Print.asp? ArticleID = 550

Read a line from a text file: Use cstdiofile, a derived class of the cfile class, as follows: cstdiofile: readstring

The icon on the taskbar flashes:
The flashwindow function flashes the specified window once, whereas the flash1_wex function flashes a specified number of times.

Bool flashwindow (
Hwnd, // handle to window to flash
Bool binvert // flash status
); // Flashes once
Flashmediawex () // flashes multiple times

In a string composed of Chinese characters, since a Chinese character is composed of two bytes, how can we determine whether one byte is the first or second byte of a Chinese character, use the isdbcsleadbyte function to determine whether a character is the first byte of the double character. Try again :)
_ Ismbslead
_ Ismbstrail

How to automatically adjust controls on the dialog box Panel as the dialog box size changes

Scales with movewindow in proportion in onsize. http://www.codeproject.com/dialog/dlgresizearticle.asp

After the data is inserted into clistctrl, it is always displayed vertically and horizontally.
Processing in redraw () of clistctrl (see http://community.csdn.net/Expert/topic/4383/4383963.xml? Temp =. 3442041)
For example:
M_list.redraw (false );
M_list.redraw (true );


How to set the color of a row in listview:
Csdn posts: http://community.csdn.net/Expert/topic/4386/4386904.xml? Temp = 2.422512e-03
Link on codeguru: http://www.codeguru.com/Cpp/controls/listview/backgroundcolorandimage/article.php/c1093/

How to obtain the size of the window title bar: Http://community.csdn.net/Expert/topic/4387/4387830.xml? Temp =. 6934168.
Getsystemmetrics(Sm_cycaption or sm_cysmcaption );

Sm_cycaption height of a caption area, in pixels.
Sm_cysmcaption height of a small caption, in pixels.
--------------------------------------------------------
Getwindowrect (& rect );
Rect. Bottom = rect. Top + getsystemmetrics (sm_cysize) + 3;

How to find the window under the mouse (specific to the sub-window and menu), whether the window has focus:
Point pt;
Cwnd * hwnd; // find out which window owns the cursor
Getcursorpos (& pt );
Hwnd = cwnd: windowfrompoint (PT );
If (hwnd = This)
{
// Place the cursor in the blank area of the form, that is, it is not in any control or subwindow
}

Obtain the position of the clistctrl control when you click an event:
-----------------------------------------------
Void ctest6dlg: onclicklist1 (nmhdr * pnmhdr, lresult * presult)
{

Nm_listview * pnmlistview = (nm_listview *) pnmhdr;
If (pnmlistview-> iItem! =-1)
{
Cstring strtemp;
Strtemp. Format ("Click column % d of row % d.",
Pnmlistview-> iItem, pnmlistview-> isubitem );
Afxmessagebox (strtemp );
}
* Presult = 0;
}

How to add an image to a clistctrl Cell? Http://community.csdn.net/Expert/topic/4388/4388748.xml? Temp =. 2233393.

Self-handling key response functions:
-------------------------------------------------
Bool ctest6dlg: pretranslatemessage (MSG * PMSG)
{
If (PMSG-> message = wm_keydown)
If (PMSG-> hwnd = getdlgitem (idc_edit1)-> m_hwnd) // you can check whether the current control is in the edit box.
{
Switch (PMSG-> wparam)
{
Case vk_return: // if the return key is used
Onbutton1 (); // call the response function of button1.
}
}
Return cdialog: pretranslatemessage (PMSG );
}

How to manipulate Word in VC:Http://www.vckbase.com/document/viewdoc? Id = 1174

How to determine the brightness level of two pixels (expressed in RGB:
Weighted gray value: R * 0.21 + green * 0.70 + blue * 0.09, or:
(Red value x 299) + (green value x 587) + (blue value x 114)/1000

You have written a function to obtain the number of real characters in an ANSI string. For example, if the length of "I server U" is returned, 4 is returned:
--------------------------------------------------
Int getcount (cstring Str)
{
Int Total = 0;
For (INT I = 0; I {
If (127 <(unsigned INT) Str. getat (I ))
{
Total ++;
I ++;
}
Else
Total ++;
}
Return total;
}

Delete A non-empty folder:
------------------------------------------------
Shfileopstruct shfileop;
Shfileop. hwnd = NULL;
Shfileop. wfunc = fo_delete;
Shfileop. fflags = fof_silent | fof_noconfirmation;
Shfileop. pfrom = "C: // Temp"; // folder to be deleted
Shfileop. PTO = "";
Shfileop. lpszprogresstitle = "";
Shfileop. fanyoperationsaborted = true;
Int NOK = shfileoperation (& shfileop );

FAQs about controls:
Http://fxstudio.nease.net/article/ocx/ <=
Serial communication between PC and single chip microcomputer using VC ++ 6.0
Http://www.zahui.com/html/1/1710.htm

Refresh the screen:
Refresh the control area
:
Control ID: idc_static_static
------------------------------------
Crect static_rect;
Cwnd * pwnd = getdlgitem (idc_static_static );
If (pwnd = NULL)
{
Return;
}
Pwnd-> getwindowrect (& static_rect );
Screentoclient (& static_rect );
Invalidaterect (& static_rect); // note that this function will call onerasebkgnd

Draw a picture on the title bar:Http://community.csdn.net/Expert/topic/4416/4416434.xml? Temp =. 8910944.
Precise latency:Http://www.vckbase.com/document/viewdoc? Id = 1301
How to rename the node in the Treeview control: http://community.csdn.net/Expert/topic/4409/4409069.xml? Temp =. 1730463.
Load and start an EXE from the memory:Http://community.csdn.net/Expert/topic/4418/4418306.xml? Temp =. 7619135.
Display in the console window of the program: Http://www.codeguru.com/Cpp/W-D/console/
Find redirection in it.

Change the font size of the edit box.: Http://community.csdn.net/Expert/topic/4389/4389148.xml? Temp =. 2317163.
First, declare a cfont object in the dialog box class, such as cfont myfont;
---------------------------------
Myfont. createpointfont (500, "Arial ");
Getdlgitem (idc_edit1)-> setfont (& myfont );

How to convert a BMP image to JPG:
Use cximage
Www.codeproject.com has

Quickly extract numbers of a specific length from the string of a number:
-------------------------------------------------------
Int A [4];
Sscanf ("2004115819185", "% 07d % 02d % 02d % 02d", & A [0], & A [1], & A [2], & A [3]); // separate by specified length
--------------------------------------------------------
Or:
-------------------------------------------------------
Cstring S = "AAA, BBB, CCC, DDD ";
Char A1 [4], A2 [4], A3 [4], A4 [4]; // pay attention to the reserved space to store the length of each substring.
Sscanf (S, "% [^,], % [^,], % [^,], % [^,]", A1, A2, A3, A4 ); // separate by a specified character (here is a comma)
Afxmessagebox (A4); // display ddd

How to change the color and style of clistctrl including scrollbars and column headers: Http://www.codeguru.com/Cpp/controls/listview/backgroundcolorandimage/print.php/c4185/

Automatically adjust the width of the ComboBox control based on the length of the string added to the ComboBox:
// Assume that two strings are added to ComboBox.
Cstring str1 = "People's Republic of China", str2 = "1234567890123 China 89012345678 ";
M_combo.addstring (str1); // m_combo is the variable bound to the control in the combo box.
M_combo.addstring (str2 );
Int Len = str1.getlength () * 6.2; // calculate the actual width of the combo box based on the length of the added string (in bytes) and the default font size used by the combo box, the two implicit conversions between integer-> floating point-> integer are used in calculation. You can also use the winapi function gettextextentpoint32 () or gettextextent to calculate
M_combo.setdroppedwidth (LEN );
Pop-up USB flash drive:Http://community.csdn.net/Expert/topic/4432/4432968.xml? Temp =. 8724634.
Send text to the editing box of another program:Handle-> sendmessage (wm_settext, strlen (BUF), (lparam) BUF); // Buf: the char you want to add *
How to add a hyperlink in RichEdit:Http://community.csdn.net/Expert/topic/4434/4434686.xml? Temp = 9.524173e-02
VC control usage:Http://www.vckbase.com/document/indexold.html
Learning Resources:Http://code.ddvip.net/list/sort000081_1.html

An SDK-Based Soft Keyboard example can be used to learn how to send a virtual button or mouse message:Http://www.codeproject.com/cpp/togglekeys.asp

Quickly extract the drive letter, path name, file name, and Suffix from the obtained full path File Name:
------------------------------------------------
Char path_buffer [_ max_path];
Char Drive [_ max_drive];
Char dir [_ max_dir];
Char fname [_ max_fname];
Char ext [_ max_ext];
Getmodulefilename (0, path_buffer, _ max_path );
_ Splitpath (path_buffer, drive, Dir, fname, ext); // use this function to convert

How to debug zero division errors:Http://community.csdn.net/Expert/topic/4440/4440273.xml? Temp =. 2427484.
Modify the default icon of the menu in the "Tools" column of vs.net: Http://www.codeproject.com/dotnet/vsnet_addin_icon_change.asp
Add your own logo on the title bar and menu bar of the window as in RealPlayer: Http://www.codeproject.com/menu/menuicon.asp
You can customize the bitmap menu by deriving sub-classes from cmenu:Http://www.codeguru.com/Cpp/controls/menu/bitmappedmenus/article.php/c165
Http://www.codeguru.com/Cpp/controls/menu/bitmappedmenus/article.php/c163
How to obtain the memory and CPU usage occupied by the program: Getprocessmemoryinfo and getperformanceinfo

How to make your program run in release Mode: Build-> set Active configuration

Monitor whether folders are updated: Three functions: findfirstchangenotification, findnextchangenotification, and findclosechangenotification
See: http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/fileio/fs/obtaining_directory_change_icationications.asp

Dynamic menu:Http://community.csdn.net/Expert/topic/4441/4441893.xml? Temp =. 2887384.
Http://community.csdn.net/Expert/topic/4506/4506791.xml? Temp =. 2409326.
How to obtain the central coordinates of the customer zone:Http://community.csdn.net/Expert/topic/4449/4449444.xml? Temp = 8.642215e-02
Forcibly manipulate the content of a specified location in a memory virtual address:
----------------------------------------------
Int * A = (int *) 0x00440000; // The access address 0x00440000 is used as an example.
Cout <* A <----------------------------------------------

How to respond to the bar code Server:Http://community.csdn.net/Expert/topic/4453/4453026.xml? Temp =. 1966516.
Barcode Scanner has three main interfaces: 1. RS232 2. shared disk interface 3. USB peripherals. for RS232, You Need To program to monitor and read the bar code. For the shared disk interface, the bar code information is converted to the corresponding Keyboard Message. applications with the input focus will receive the keyboard input message, our previous practice was to make a global keyboard hook or keyboard hook at the application level to monitor keyboard messages. When there is a continuous Keyboard Message (within a short period of time ), in addition, these keyboard characters can constitute the complete bar code information, and a custom message is generated. The notification window (the window registered to the Monitoring Program) bar code information arrives. The bar code machine is just equivalent to a keyboard, therefore, you can also put an edit box on the interface. After reading the bar code from the bar code machine, a carriage return will be added to the string (this is generally configurable and can be added or not added ), if the bar code machine automatically adds a carriage return, you can rewrite the onok function and extract the content of the edit box into the list.
Of course, you can also directly receive keyboard characters (for example, rewrite the onchar function and so on, there are many ways) without the edit box, but consider this situation: the Bar Code cannot be read, in this case, manually enter the barcode, so it is better to put an edit box.

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.