VC sculptor small technology set (1 ~ 7)

zhaozj2021-02-16  51

VC sculptor small technology set (1) He Zhidan

1. Avoid the same amount of random, you can take the current time, read the file, read any memory (if there is no variable, there is a danger).

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, SSCANF, in fact, there are many other functions, but only the two functions are the most easily remembered. Because they are similar to Printf, Scanf, Fprint, FSCANF usage.

Char Str [100];

INT x = 5;

Sprintf (STR, "% D", X);

SSCANF ("10", "% x", & x);

3. Look at whether a button is in the prescription and whether it is pressed.

Short key = getasynckeyState (vk_f11);

0x8001 is in a state

1 call getasyncKeyState (VK_F11) between two times, F11 is pressed.

GetasyncKeyState, getKeyState Specifies whether the virtual code is vk_lbutton, vK_rbutton can determine if the mouse is pressed

4, how to delete a class.

Remove the file, open the class wizard (Ctrl W)

The class wizard can not find a file, a dialog box will pop up

REMOVE is OK.

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 get the volume of the partition? ?

Char buf [max_path];

GetVolumeInformation ("C: ///", BUF, MAX_PATH, NULL, NULL, NULL, NULL, NULL);

AfxMessageBox (BUF);

8, open a web page

Shellexecute (:: getdesktopwindow (), _ t ("open"), // SK Modified for Unicode

_T ("http://www.sohu.com"), NULL, NULL, // SK MODIFIED For Unicode

SW_MAXIMIZE);

9. When you turn it off, the pop-up dialog allows the user to confirm

Void cmainframe :: onClose ()

{

IF (iDok! = messagebox ("Do you want to exit? Click to exit", "Do you want to exit?", MB_OKCANCEL))

Return;

CframeWnd :: onClose ();

}

10, menu small problem

ID_FILE_OPEN

In the resource accelerator:

ID Key

ID_FILE_OPEN CTRL O

Indicates that pressing the Ctrl O combination key in the program to activate the open menu (if the file menu is opened, it is invalid).

In the resource MENU:

ID_FILE_OPEN's title is open (& O) ... / tctrl O

& O means that when its parent menu (file menu) is opened, the O activation file menu.

11, SIZEOF has a problem??

Class B

{

BOOL A;

BOOL C;

INT B;

}; // sizeof (b) == 8

layout:

1 2 3 4

Bool Bool Fill Pack

1 2 3 4

Int ............... At a glance. :)

At this time, the compiler is only inserted into two padding bytes after C, because B is four bytes, so automatic fill in one word binary.

This is an alignment problem of the compiler, as long as it is joined in front

#pragma 1

Make it in one byte.

VC sculptor small technology set (2) He Zhidan

1. If you don't define _unicode, LPCTSTR represents const char *, so it can be used directly.

If the _unicode, lpctstr represents const wchar_t *, it cannot be used directly.

2, get the position of the mouse

DWORD DW = GetMessagePos ();

Cpoint PT (Loword (DW), HiWord (DW)); // Mouse screen coordinate

CWnd :: screenToclient ();

3, get the length of the file

Method 1: Handle Hhandle;

DWORD DWSIZE;

HHHANDE = :: Createfile ((lpctstr) strzipfile, generic_read,

File_share_read, null, open_existing, file_attribute_normal,

NULL);

IF (HHHANDLE == Invalid_Handle_Value)

Return False;

DWSIZE = :: getFileSize (HHANDE, NULL);

CloseHandle (HHANDLE);

Method 2: cstring szfile = "c: /aaa.txt";

INT nfilesize = 0;

CFILEFIND FINDER;

IF (Finder.findfile (SzFile) {

Finder.FindNextFile ();

nfilesize = finder.getLength (); // file size is here

Finder.Close ();

}

Method 3: cstdiofile sf;

.

sf.getlength ();

4, the array is not "=", but there is copy.

5, how to get the handle of the VIEW class

Don't forget the # include file

CMYVIEW * PVIEW = (CMYVIEW *) (CMAINFRAME *) AFXGETMAINWND ()) -> getActiveView ();

PView-> m_hwnd is

6, custom message

Below is the step of adding custom messages:

Step 1: Define the message. When developing a Windows95 application, Microsoft recommends that user-defined messages are at least WM_USER 100 because many new controls should also use WM_USER messages.

Step 2: Implement the message processing function. This function uses WPRAM and LPARAM parameters and returns lpeSult.

LpeSult CMAINFRAME :: ONMYMESSAGE (WPARAM WPARAM, LPARAM LPARAM)

{

// TODO: Handle user custom message

...

Return 0;

}

Step 3: Describe the message processing function in the AFX_MSG block of the class header:

Class CMAINFRAME: PUBLIC CMDIFRAMEWND

{

...

// General message mapping function

protected:

// {{AFX_MSG (CMAINFRAME)

AFX_MSG Int Oncreate (LPCReatestruct LPCreateStruct);

AFX_MSG Void Ontimer (uint nidEvent);

AFX_MSG LRESULT ONMYMESSAGE (WPARAM WPARAM, LPARAM LPARAM);

//}} AFX_MSG

Declare_message_map ()

}

Step 4: In the user class message block, use the ON_MESSAGE macro to map the message to the message processing function. Begin_MESSAGE_MAP (CMAINFRAME, CMDIFRAMEWND)

// {{AFX_MSG_MAP (CMAINFRAME)

ON_WM_CREATE ()

ON_WM_TIMER ()

ON_MESSAGE (WM_MY_MESSAGE, ONMYMESSAGE)

//}} AFX_MSG_MAP

END_MESSAGE_MAP ()

If the user needs a unique message throughout the system, you can call the SDK function REGISTERWINDOWMESAGE and use the on_register_message macro to replace the ON_MESSAGE macro, the rest is the same.

7, WSCANF is the wide character version of Scanf, the usage is the same as Scanf, used to read the STDIN stream, store information into the variable specified by its parameter list.

The CIN object is derived from the iStream class, mainly used to input data from the standard input device to the program, through the data input by CIN, must consist in the format of the word, CIN match ">>" symbol

VC sculptor small technology set (3) He Zhidan

1, the fixed window size, the maximum value is (600, 400), the minimum is also (600, 400).

Void CMAINFRAME :: ONGETMINMAXINFO (MinMaxInfo Far * LPMMI)

{

LPMMI-> PTMaxTracksize.x = 600;

LPMMI-> ptmaxtracksize.y = 400;

LPMMI-> ptmintracksize.x = 600;

LPMMI-> ptmintracksize.y = 400;

CframeWnd :: ONGETMINMAXINFO (LPMMI);

}

2. Close the dialog and window:

In fact, the destruction of the general window can be destroyed with DestoryWindow (), and the destruction of the dialog box can be enddialiog ().

EndDialog (IDOK);

The return value of Domodal () is IDOK, but does not execute the user-defined Onok.

As long as you send a WM_CLOSE message, there will be a function of responding.

SendMessage (WM_CLOSE);

Postmessage (WM_CLOSE);

Void ctestdialog :: onbutton1 ()

{

GetParent () -> DestroyWindow ();

}

Window and dialog box

Postmessage (WM_QUIT);

SendMessage (wm_quit); //

No mode dialog with destroyWindow ();

EndDialog

You have a problem with you open the original dialog.

3, the use of global variables:

C *** App Defines a cstring

when using it

Void cdlgdlg :: onbutton2 ()

{

((CDLGAPP *) AFXGetApp ()) -> str = "test";

}

Don't forget to include files

4, how to let the DOS program turn on automatically and not display

Automatically run the resolution, write the registry RUN value,

VC windows2000

Change Main to WinMain

5, clear automatic password history

In the browser:

Tools -> Internet Options -> Content -> Point "Automated (U)" button -> Clear password

http://tzsvc.xiloo.com/skill/sys/clear.htm

ShdeleteKey (HKEY_CURRENT_USER,

_T ("Software // Microsoft // Internet Explorer // IntelliForms));

6, execute an executable program

method one:

Shellexecute (this-> getsafehwnd (), "open", "f: //he.txt", null, null, sw_shownormal); method 2:

Process_information pi;

Startupinfo Si;

Si.cb = sizeof (startupinfo);

si.lpreserved = null;

Si.lpdesktop = null;

Si.lptitle = NULL;

Si.dwflags = 0;

Si.cbreserved2 = 0;

Si.lpreserved2 = NULL;

Bool Bres = CreateProcess (Null, "Test A.TXT B.TXT", NULL, NULL, FALSE,

Normal_Priority_Class,

NULL, NULL, & SI, & PI;

IF (BRES == FALSE)

{

AfxMessageBox ("CreateProcess Failed");

}

Else

{

CloseHandle (pi.hthread);

DWORD dwret = WaitforsingleObject (pi.hprocess, 1000 * 30);

Switch (dwret)

{

Case Wait_Object_0:

DWORD DWEXITCODE;

Bres = getExitcodeProcess (pi.hprocess, & dwexitcode);

TCHAR EXITMSGBUF [1024];

IF (BRES)

{

WSPrintf (Exitmsgbuf, "EXIT CODE:% D", DWEXITCODE);

}

Else

WSPrintf (Exitmsgbuf, "EXIT code failed to return");

AFXMESSAGEBOX (Exitmsgbuf);

Break;

DEFAULT:

AfxMessageBox ("exit for other reason";

VC sculptor small technology set (four) He Zhidan

1. Reading procedures often look at this variable changes in those local values.

Ctrl f looks too much trouble

Plus this variable before CONST

Changed place to report

2, look at a function is called by those functions.

method:

1, under debugging, view-> debug window-> Call Stack

2. Select Called by Class View's shortcut menu

3, the leftmost display time in the status bar

Static uint indeicators [] =

{

ID_SEPARATOR, // We increase

ID_separator,

ID_indicator_caps,

ID_indicator_num,

ID_Indicator_scrl,

}

INT CMAINFRAME :: OnCreate (lpcreatestruct lpcreatestruct)

{

..... m_wndstatusbar.setpaneinfo (1, ID_separtor, sbps_normal, 60);

}

// menu response function

Void CMAINFRAME :: ontest ()

{

CTIME T;

T = CTIME :: getcurrenttime ();

CString str = T.Format ("% h -% m -% s");

M_WndStatusBar.SetPanetext (0, STR);

}

4, simulate mouse

:: setCursorpos ((int) ptmouse-> x, (int) ptmouse-> y); :: mouse_event (MouseEventf_leftdown, 0,0,0,0) ;:: mouse_event (MouseEventf_leftup, 0,0,0,0) ;: mouse_event (MouseEventf_leftdown, 0,0,0) ;:: mouse_event (MouseEventf_leftup, 0, 0, 0); 5, what is Word, DWORD

Typedef unsigned short word

Typedef unsigned long dword

6, mobile control

CWND * PWND = getDLGITEM (IDC_Button1);

CRECT R;

PWND-> GetWindowRect (& R);

R.LEFT = 50;

R.right = 50;

PWND-> MoveWindow (R);

7, how to make the CEDIT in the dialog response ENTER key to enter

Getdlgitem (IDC_EDit1) -> SetwindowText ("111 / R / N22");

Right click attribute

Sytle -> Multiline

It is best to choose Want Return.

8, transform C language into compilation

VC provides this feature.

Project-> settings ...

C / C Tab

Category: choose Listing Files

Listing File Type:

SELECT "Assembly with Source Code"

9, how to get the CPU time consumed by the process?

Bool getProcesstimes

Handle HProcess,

LPFileTime LPCREATIONTIME,

LPFileTime Lpexittime,

LPFileTime LPkernelTime,

LPFileTime lpusertime

);

10. How to get the icon corresponding to the extension by the extension?

SHGETFILEINFO (PATH,

ATR,

& sfi,

SizeOf (SHFILEINFO),

SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_TYPENAME);

How to change the size of the dialog box to make it proportionally change. Just like video playing software

Handling WM_SIZING message

Void ctest 1dlg :: Onsizing (uint fwside, lprect prect)

{

CDIALOG :: Onsizing (fwside, prect);

IF ((fwside == wmsz_bottom) || (fwside == wmsz_top))

Prect-> Right = prect-> Left (prect-> bottom-prect-> TOP) * 2;

Else

Prect-> Bottom = prect-> top (prect-> right-prect-> left) * 0.5;

}

VC sculptor small technology set (5) He Zhidan

1. Right-click on the VC to click on a class or macro, you can look at his definition, but how to return to the initial place?

Install Visual Assist, And Use Alt Left Arrow

Press the back of the menu (<- turn 45 degrees) button

It seems that I opened a document and turned off it.

2, how to get the system menu, and pop up the system menu?

Void ctestsysmenudlg :: onok () {

// Click The Ok Button to Show System Menu (Masterz)

Point curpos;

GetCursorpos (& Curpos);

SetForegroundWindow ();

CMenu * pmenu = getSystemMenu (false);

INT nSelection = pmenu-> TRACKPOPUPMENU (TPM_LEFTBUTTON | TPM_LEFTALIGN |

TPM_BOTTOMALIGN | TPM_NONOTIFY | TPM_RETURNCMD, CURPOS.X, CURPOS.Y, THIS

DWORD DWPOS = Curpos.x (Curpos.Y << 16) & 0xffff0000);

SendMessage (WM_SYSCOMMAND, NSELECTION, DWPOS);

CSTRING MSG;

Msg.format ("x:% d, y:% d; cursor.x =% d, cursor.y =% d", loword (dwpos), Hiword (dwpos), curpos.x, curpos.y);

OutputDebugstring (MSG);

}

3, how to control the brightness of the three lamps above the keyboard (Num Lock, Caps Lock Srcoll Lock)

#include

Void SetNumlock (Bool BState)

{

Byte KeyState [256];

GetKeyboardState (LPBYTE) & keystate;

IF ((BState &&! (KeyState [vk_numlock] & 1) ||

(! BState && (KeyState [vk_numlock] & 1)))))))

{

// Simulate a Key Press

Keybd_Event (vk_numlock,

0x45,

KeyEventf_extendedKey | 0,

0);

// simulate a key release

Keybd_Event (vk_numlock,

0x45,

KeyEventf_extendedKey | KeyEventf_Keyup,

0);

}

}

void main ()

{

SetNUMLOCK (TRUE);

}

4. How to change it to English after the Chinese is selected in the appWizard of VC?

Open the resource file with a file editor, find "Language", find it after finding:

// Chinese (p.r.c) resources

#if! defined (AFX_RESOURCE_DLL) || Defined (AFX_TARG_CHS)

#ifdef _win32

Language lang_chinese, SUBLANG_CHINESE_SIMPLIFIED

#pragma code_page (936)

#ENDIF / / / WIN32

This code indicates that the currently used code page is 936. The language represented is Simplified Chinese, and the following code is replaced with the following code:

// English (u.s) Resources

#if! defined (AFX_RESOURCE_DLL) || Defined (AFX_TARG_ENU)

#ifdef _win32

Language lang_english, sublang_english_us

#pragma code_page (1252)

#ENDIF / / / WIN32

5. How to effectively determine if the mouse point is in the rectangular area of ​​the control

DWORD DW = getMessagePos (); cpoint Pt (Loword (DW), HiWord (DW)); // Mouse screen coordinate

CRECT R;

Getdlgitem (idc_edit1) -> getClientRect (& R);

Getdlgitem (IDC_EDIT1) -> ClientToscreen (& R);

IF (r.ptinRect (PT))

AfxMessageBox ("in the control");

6, handle the right click of the control, no

Bool ccDialogdlg :: PretranslateMessage (MSG * PMSG)

{

IF (WM_Mousemove == PMSG-> Message)

IF (PMSG-> hwnd == getdlgitem (idc_button1) -> getsafehwnd ())

{

Uint nflag = pmsg-> wparam; // state, such as Ctrl is not pressed

INT XPOS = loword (pmsg-> lparam); // Horizontal Position of Cursor

INT YPOS = HiWord (PMSG-> lparam); // Vertical Position of Cursor

}

Return CDIALOG :: PretranslateMessage (PMSG);

}

VC sculptor small technology set (six) He Zhidan

1, how to get CheckBox status on Tree Control ~

Oninit plus:

m_treectrl.insertitem ("item1");

m_treectrl.insertitem ("item2");

Void ccDialogdlg :: onbutton1 ()

{

HtreeItem HITEM = m_treectrl.getrootItem ();

While (NULL! = HITEM)

{

CString str = m_treectrl.getitemtext (HITEM);

IF ("item2" == STR)

{

IF (m_treectrl.getcheck (hItem))

AfxMessageBox ("Select");

}

HITEM = m_treectrl.getnextVisibleItem (HITEM);

}

}

2, how to send characters to another with another program and make it displayed

First get the window handle of the Windows program via FindWindow.

Then get the window handle of the input box by getDLGITEM.

Finally, send a WM_CHAR message to the window handle to display characters.

For example, for the NOTEPAD window, you can enter one 'a' in the following manner:

Take a notepad's window handle

HWND HWND = :: FindWindow (NULL, "Unexpected Title - Notepad");

Take the window handle of the input box

HWND HEDIT = :: getdlgitem (hwnd, 0x0f); // 0x0f here is the ID of the edit box, can be observed in SPY

Fill in 'a' in the input box

:: SendMessage (Hedit, WM_CHAR, (WPARAM) 'A', 0x00000001);

3, current time:

CTIME T = CTIME :: getcurrenttime ();

CString Str; str.format ("% D-% D-% D", T.Getyear (), T. GetMonth (), T. GetDay ()); str = t.format ("-% H-% M-% s ");

AfxMessageBox (STR);

Be sure to use CTIME :: getCurrentTime (); getCurrentTime () is a outdated function.

4, // change the background color of the button.

Hbrush CRECTWINDOW2VIEW :: ONCTLCOLOR (CDC * PDC, CWND * PWND, UINT NCTLCOLOR)

{

Hbrush Hbr = CFormview :: ONCTLCOLOR (PDC, PWND, NCTLCOLOR);

Switch (nctlcolor)

{

Case CTLColor_BTN:

{

CBRUSH Brush (RGB (128, 0, 128)); // Your color

CBRUSH * POLDBRUSH = PDC-> SelectObject (& brush);

PDC-> SELECTOBJECT (POLDBRUSH);

}

}

Return CFormview :: ONCTLCOLOR (PDC, PWND, NCTLCOLOR);

}

5. Use of accelerator keys.

Add in the DLG's header file:

Haccel M_Haccel;

Add the accelerator in the DLG constructor:

How does the program based on the dialog box use acceleration keys?

m_haccel = :: loadaccelerators (AFXGetInstanceHandle (), makeintResource (iDR_accelerator1)); // Accelerator ID ID

Heavy duty PretranslateMessage function:

Bool CDLG :: PretranslateMessage (MSG * PMSG)

{

// Todo: Add Your Specialized Code Here and / or Call The Base Class

IF (M_Haccel! = NULL)

IF (:: translateAccelerator (m_hwnd, m_haccel, pmsg))

Return True;

Return CDIALOG :: PretranslateMessage (PMSG);

}

6, how do you save a file to other places?

IF (! CopyFile ("f: //he.txt", "d: //he1.txt", true))

{

IF (IDOK == Messagebox ("Is there a file, do you have to overwrite?", NULL, MB_OKCANCEL)

{

Copyfile ("f: //he.txt", "d: //he1.txt", false);

}

Else

Return;

}

Deletefile ("f: //he.txt");

VC sculptor small technology set (7) He Zhidan

1, picture control

Right click Properties TYPE: Bitmap

You choose image directly

IDB_bitmap1 bitmap ID

M_ctrl is a control variable associated with the picture control

Void cdlg2dlg :: onbutton1 ()

{

CBITMAP Bitmap1;

Bitmap1.loadBitmap (IDB_bitmap1);

m_ctrl.setBitmap (Bitmap1);

}

Void cdlg2dlg :: onbutton2 ()

{

CBITMAP Bitmap2;

Bitmap2.LoadBitmap (IDB_bitmap2);

m_ctrl.setBitmap (Bitmap2);

}

2, what I can't see the CORTROL ID when I give the control?

1, radio button, hook the group.

2, static control converts ID to non IDC_STATIC.3, Size keyboard ASCLL code

0-9 Different, but , - the same.

4, display the characters entered by the keyboard, try to avoid flashing

Void csdiView :: onchar (uint nchar, uint nrepcnt, uint nflags)

{

CDC * PDC = Getdc ();

StringData = nchar;

CRECT R (0,0,0,0);

PDC-> DrawText (StringData, & R, DT_LEFT | DT_CALCRECT);

InvalidateERECT (R);

CView :: OnChar (nchar, nrepcnt, nflags);

}

Void CsdiView :: OnDraw (CDC * PDC)

{

PDC-> DrawText (StringData, CRECT (0, 0, 1000, 1000), DT_LEFT);

}

5. Dialog box A Domodal () Out of a dialog B. After clicking B, you can request a function in the class of A.

Processing in the response function of B:

((A *) getParent ()) -> fun ();

Files in INLCUDE A in B

6, delete folders and contain files

#include

Void deletedirfile (CString Spath)

{

WIN32_FIND_DATA FD;

Handle Hfind = :: FindfirstFile (Spath "*. *", & Fd);

IF (HFIND! = INVALID_HANDLE_VALUE)

{

While (: FindNextFile (HFIND, & FD))

{

// Judgment is a directory

IF (fd.dwfileAttributes & file_attribute_directory)

{

CString name;

Name = fd.cfilename;

// Judgment is whether it is. And ..

IF ((Name! = ") && (name! =". "))))

{

// If it is a real directory, recursive

DELETEDIRFILE (Spath fd.cfilename "//");

}

}

Else

Deletefile (Spath fd.cfilename);

}

:: FindClose (Hfind);

}

RemoveDirectory (SPATH);

}

7, change the size of the control font

Logfont logfont;

CFont * pfont = m_ctrshowMessage.getFont ();

PFONT-> GetLogFont; & logfont;

Logfont.lfheight = logfont.lfheight * 1.5;

Logfont.lfwidth = logfont.lfwidth * 1.5;

CFONT font;

Font.createFontIndirect (& logfont);

M_CTRSHOWMESSAGE.SETFONT (& font);

8, want to create a category with ClassWizard, but found that there is no base class I want in the baseclass column (Coblist class)

Class Type: Generic

Yourself

9, ctypedptrlist lineelist;

Why VC error:

C: / program / scribble/scribbleDoc.h (50): Error C2143: Syntax error: missing ';' Before '<' # include "afxtempl.h"

Is character L, not a number 1

10, do not prompt member functions, what should I do?

CString S1 = "DFDSF";

S1.

Try the following three methods:

1, build-> clean, build-> refuildall.

2, manual delete * .ncb

3, install auxiliary tool VC_Assist6

11, I have 6 Radio Box how to divide 3 groups

Put the three Radio Box's Group

It is divided into three groups.

Related to the order of focus

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

New Post(0)