VC ++ beginners Frequently Asked Questions

zhaozj2021-02-11  220

VC beginners FAQs Li Hai

Visual C is a very powerful tool, but it is also recognized that is difficult to get started. Here, we have organized some common problems in beginners, hoping to help friends who have just come against Visual C enjoy the fun of programming. Q: I am a newbie. I would like to write a C program written in TC , build a working project in VC (the program is correct in TC )? A: When the new project in VC , you can choose Win 32 Console Application. This console program works like DOS programs and outputs text information, although the graphic function cannot be used, but other Windows API functions can be used. It should be noted that if your TC program uses a non-ANSI standard statement, such as the function in Conio.h, some modifications may need to be compiled under VC . Q: Is there a function of DOEvents in VC? A: You can use such a function: BOOL CheckMessageQueue () {msg msg; While (PeekMessage (&& Msg, NULL, 0, 0, PM_REMOVE)) {ix (msg.message == WM_QUIT) RETURN FALSE; TranslateMessage (&& Msg); DispatchMessage (&& Msg);} Return True;} When you call the peekMessage, other programs will have the opportunity to obtain control, and can implement the effect of DoEvents. However, pay attention to the meaning of this function return value, if the function returns false, indicating that the user presses the shutdown button. Q: How to remove the file tail in C / C without copying the file, which is the quick cut short file? A: In C / C , there is generally a function chsize or _chsize can change the size of the file. Here is an example: int fh, result; unsigned int nbytes = bufsiz; / * Open file * / if ((fH = _open ("c: // TestData", _o_rdwr | _o_creat, _s_iread | _s_iwrite))! = -1 ) {Printf ("Modified File Length:% LD / N", _FileLength (FH)); IF ((Result = _CHSIZE (FH, 329678)) == 0) Printf ("successfully modified file length / N" ); Else Printf ("changed file length error / N"); Printf ("Modified file length:% ld / n", _filelength (fH)); _close (fh);}} Q: Use VC5.0 to use OBJ After the file is compiled into a DLL, it always finds that the entry point is not found with VB.

What place to join the parameters for entry points? A: If the entry is not found: 1. Nothing to pay attention to the case of the function name in Win32, the functions in the DLL and the function in the VB must be exactly the case, otherwise the system thinks that there is no call in the DLL. function. 2. No declaration function You need to add an inlet function in the DLL. DEF file, such as: Exports setData @ 1 getData @ 2 This VB program can access the setData and getData functions. Where @ 1, @ 2 is the reference number of these two functions, usually not in VB. If your DLL project has not been used. DEF file, you can build a text file and name it to the project after name it. DEF extension. 3. Use the C compilation mode to translate the function name when compiling in C , such as converting KK (Double K) in the DLL to _kk @ 8. There are two ways to solve: (1) If you do not use C classes, you can change the .cpp file to .c, no such conversion. (2) Plus Extern "C" before the function definition, such as: extern "C" void _stdcall KK (Double K); what to indicate, VC allows not to write .def file, but in the function definition __Declspec (DLLEXPORT) modifier, such a function can also be called by external programs. But such functions will be converted in a C mode. Although such a function can be called by the VB program, it is inconvenient, so it is still recommended to use the .def file. Q: Use VC 5.0, how to set up the Caps Lock button in the program so that the user automatically enters the letter in a EDIT box, and then set the key and then press, and restore the lowercase letters. A: Use the Windows API function keybd_event to simulate pressing the Caps Lock button. The following code can switch the Caps Lock key.

void SetCapsLock (BOOL bState) {BYTE keyState [256]; GetKeyboardState ((LPBYTE) && keyState);! if ((bState &&&& (keyState [VK_CAPITAL] && 1)) || (bState &&&& (keyState [VK_CAPITAL] && 1)! )) {// simulation Press a key keybd_event (vk_capital, 0x45, keyeventf_extendedKey | 0, 0); // Simulate a key KeyBD_Event (vk_capital, 0x45, keyeventf_keyup, 0);}} // When user switching To the Edit1 control, set to uppercase status void cdlgdlg :: OnsetFocusedit1 () {setcapslock (true);} // When the user leaves the Edit1 control, set to lowercase status void cdlgdlg :: ONKILLFOCUSEDIT1 () {setcapslock (false);} Your question, I recommend using another method, to overload the PretranslateMessage function, the user automatically converts it to uppercase, rather than modifying the Caps Lock key, such as: Bool CDLGDLG :: PretranslateMessage (MSG * PMSG) {IF (PMSG-> Message == WM_CHAR &&&& pmsg-> hwnd == :: getdlgitem (m_hwnd, idc_edit1) {pmsg-> wparam = Toupper (PMSG-> wparam);} return cdialog :: PretranslateMESSAGE (PMSG); } Q: How to program the dialog window that implements Windows shutdown? That is, if I press a button, you can have a shutdown dialog window, let me choose whether it is turned off, restart, etc. And the background is also dusty. Is there a ready-made API that can be called? A: There is an unapproved API function to achieve this function, in shell32.dll, but no name, index number is 60. This function can be called using the LoadLibrary and getProcAddress function.

转载请注明原文地址:https://www.9cbs.com/read-4519.html

New Post(0)