VC programming experience summary

zhaozj2021-02-16  139

VC programming experience summary (1) Source: laiyiling column

1. The window maximizes, minimizes the implementation

When we cannot maximize, minimize and recover buttons in the title bar, you need to implement these features in other places, you can add:

WINDOWPLACEMENT WNDPL;

Windowplacement * PWNDPL;

PWNDPL = & WNDPL;

GetWindowPlacement (PWNDPL);

PWNDPL-> showcmd = sw_showminmized; // Implement the window minimization

SetwindowPlacement (PWNDPL);

Where the getWindowplacement () function gets the structure variable pointer of the Structure WindowPlacement of the current window layout, the structure of Windowplacement is defined as:

Typedef struct tagwindowplacement {

Uint Length;

Uint flags;

Uint showcmd;

Point PtminPosition;

Point ptmaxposition;

Rect rcNORMALPSITION;

WINDOWPLACEMENT;

The member variable showcmd determines the state of the current window, and the value is generally:

· SW_HIDE: Hide Window

· SW_MINIMIZE: Minimize the specified window

· SW_RESTORE: Restore the original size

· SW_SHOW: Activated and displayed in the original size

· SW_SHOWMAXIMIZED: Activate and maximize windows

The setWindowPlacement () function is to display the window according to the settings of WindowPlacement.

2. Why use a getsafehwnd () function

When we want to get a window object (CWnd derived object) pointer's handle (HWnd), the safest way is to use the getsafehwnd () function, to see the reason by the following example:

CWND * PWND = FindWindow ("ExplorewClass", NULL; // I hope to find the resource manager

HWND HWND = PWND-> m_hwnd; // Get it HWnd

This code will appear "General Protection Error" when the PWND started is empty, and closes the application because it is generally not accessing a NULL pointer, if using the following code:

CWND * PWND = FindWindow ("ExplorewClass", NULL; // I hope to find the resource manager

HWND HWND = PWND-> getsafehwnd (); // Get it HWnd

There will be no problem, because although when the PWND is NULL, getSafehWnd is still available, just returns NULL, and cleares the code through getSafehWnd (). Clear:

_AFXWIN_INLINE HWND CWND :: GetsafehWnd () Const

{

Return this == NULL? NULL: M_HWND;

}

3. How to make the program in a very small state

If we don't want the program's window to be seen by others, you can keep it in a very small state: When the recovery program window, Window sends a WM_QUERYOPEN message as long as false is returned in its message handler.

Bool cmainframe :: ONQUERYOPEN ()

{

Return False;

}

4. How to prohibit and use the shutdown button

CMenu * pmenu = AFXGETMAINWND () -> GetSystemMenu (false); if (pmenu)

{

PMenu-> EnableMenuItem (sc_close, mf_bycommand | mf_grayed);

}

Simply change MF_GRAYED to MF_ENABLED when recovering

5. How to delay in the program

method one:

Use the SLEEP function, such as delay 2 seconds, with SLEEP (2000);

Method Two:

Using the SLEEP function is unfavorable when delayed periods cannot handle other messages, if time is too long, it is like a crash, which does not have such problems with the COLEDATETIME class and the COLEDATETIMESPAN class:

COLEDATETIME Start_time = COLEDATETIME :: getcurrenttime ();

COLEDATETIMESPAN END_TIME = COLEDATETIME :: getcurrenttime () - start_time;

While (end_time.gettotalseconds () <= 2)

{

MSG msg;

GetMessage (& MSG, NULL, 0, 0);

PretranslateMessage (& MSG);

End_time = COLEDATETIME :: getcurrenttime-start_time;

}

In this way, we can also handle other messages when delayed.

I have seen some summary articles in the forum very well. I have written a little out of it. I don't know if there is any repetition. I hope I can have some micro role.

VC programming experience summary (2) 6. How to create a scalable dialog

In the design of the dialog, we sometimes need to design a scalable dialog that pops up or hide the lower part of the dialog box when the user presses a button.

(1) First, establish a picture control in the dialog box to set the ID to IDC_DIVIDER, TYPE set to rectangle, and the color is set to black, and set it to a line, drag and drop in the appropriate position as a scaling dialog box. The split line is invisible.

(2), the principle of implementation: First obtain the size of the dialog box, then determine the reduction dialog size according to the position, in fact, the change in the dialog expansion is the value, after the reduction dialog, we want to make it invisible Some controls are disabled to prohibit the operation of the accelerator and the Tab key, after the extended dialog, the originally prohibited control is also enabled.

Add a message processing function on the dialog box first:

Void c *** dlg :: onbuttonexpand ()

{

Static int bexpand = false; // set the initial time to expand

ExpandDialog (IDC_DIVIDER, BEXPAND); // Call the extension or reduction processing function

BEXPAND =! Bexpand; // Inverse, prepare for the next click

}

/ / Add a member function expandDialog in the dialog, used to extend or reduce

Void C *** DLG :: ExpandDialog (int NResourceID, Bool BEXPAND)

{

// Parameter nresourceID represents the ID of the split line

/ / The parameter bexpand indicates that the dialog is to be extended when the parameter bexpand is True. Otherwise, the dialog box

Static CRECT RCLARGE;

Static CRECT RCSMALL;

IF (RcLarge.Inectnull ()) // Maximum, Minimum size of the dialog box in the first time

{

CRECT RCLANDMARK;

CWND * pwndland = getdlgitem (nresourceId); assert (pwndland);

GetWindowRect (rcline);

PWNDLAND-> getWindowRect (rclandmark);

Rcsmall = rclarge;

Rcsmall.bottom = rclandmark.bottom;

}

IF (BEXPAND)

{

SetwindowPos (NULL, 0, 0, RCLARGE.WIDTH (), RCLARGE.HEIGHT (),

SWP_NOMOVE | SWP_NOZORDER);

Enablevisible ();

}

Else

{

SetwindowPos (NULL, 0, 0, RCSMALL.WIDTH (), RCSMall.Height (),

SWP_NOMOVE | SWP_NOZORDER);

Enablevisible ();

}

}

/ / Add a member function enablevisible in the dialog box to use and disable part controls

Void C *** DLG :: enablevisible ()

{

CWND * PWND = getDLGITEM (gw_child);

CRECT RETEST;

CRECT RCCONTROL;

CRECT RCSHOW;

GetWindowRect (rcshow);

While (PWND! = Null)

{

PWND-> GetWindowRect (RcControl);

IF (rctest.intersectRect (rcshow, rccontrol))

PWND-> EnableWindow;

Else

PWND-> EnableWindow (false);

PWND = PWND-> getWindow (GW_HWndNext);

}

}

7. Why is there a richedit control dialog that cannot be displayed?

If you put a RicheDit control on the dialog, the discovery dialog is not normal, because the application has not prepared for the RicheDit control editing function, the solution is the application's initInstance () function call Afxinitrichedit ( ) Function initialization Richedit control

8. How to specify the default button of the dialog

When establishing a dialog, under the default condition, the determination button (IDOK) is the default button, if you need to change the default button, there are two methods:

One: Directly remove the Default Button style option directly in the properties of the button (IDOK)

2: Implement with code when running, such as:

// Remove the default button of the determination button (IDOK)

CButton * pokbutton = (cbutton *) getdlgitem (IDOK);

Pokbutton-> ModifyStyle (BS_DEFPUSHBUTTON, 0);

// Add iDCancel's default button style

CButton * pcancelbutton = (cbutton *) getdlgitem (IDCANCEL);

PcancelButton-> setButtonStyle (BS_DEFPUSHBUTTON);

VC programming experience summary (3) 1. Display and hide the title bar

Method 1: Using API implementation

// Hide Titlebarlong LStyle = :: getWindowlong (this-> m_hwnd, gwl_style); :: setWindowlong (this-> m_hwnd, gwl_style, lstyle & ~ ws_caption); :: setwindowpos (this-> m_hwnd, null, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); // display TitleBar :: SetWindowLong (this-> m_hWnd, GWL_STYLE, lStyle | WS_CAPTION); :: SetWindowPos (this-> m_hWnd, NULL, 0, 0, 0, 0, ?? SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); method two: use the CWnd member function ModifyStyle achieve // ​​hide TitleBarModifyStyle (WS_CAPTION, 0, SWP_FRAMECHANGED); // display TitleBarModifyStyle (0, WS_CAPTION, SWP_FRAMECHANGED);

2. How to use SendMessage () to send a message to clear its content? HWND HEDITWND = getdlgitem (idc_edit1) -> getsafehwnd (); :: SendMessage (HeditWnd, WM_SETTEXT, (WPARAM) 0, (LPARAM) "")

3. The file property window pop SHELLEXECUTEINFO ShExecInfo = {0}; ShExecInfo.cbSize = sizeof (SHELLEXECUTEINFO); ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST; ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = "properties"; ShExecInfo.lpFile = "c: / "; // can also be file shexecinfo.lpparameters =" "; shexecinfo.lpdirectory = null; shexecinfo.ns = sw_show; shexecinfo.hinstapp = null; shellexecuteex (& shexecInfo);" SHELLEXECUTEEX ";

4. Delete all files in a directory

Bool DeletedIRectory (LPCTSTR DIRNAME) {cfileFind Tempfind; // Declare a cfilefind class variable to search for Char TempFileFind [200]; // Used to define Sprintf (TempFileFind, "% S //*.*", DIRNAME ); // matching format address *. *, All files Bool isfile = (bool) tempfind.FindFile (TempFileFind); // finds the first file while (isfinded) {isfinded = (bool) TempFind. FindNextFile (); // Removable Search Other Files IF (! Tempfind.isdots ()) // If not "." Directory {Char FoundFileName [200]; strcpy (FoundFileName (). GetBuffer (200)) , if (TempFind.Indirectory ()) // If it is a directory, recursively call {// deletedirectory char Tempdir [200]; Sprintf (Tempdir, "% S / / /% S", DIRNAME, FOUNDFILENAME); DELETEDIRECTory (Tempdir );} else {// If it is a file, CHAR TEMPFILENAME [200]; Sprintf (TempFileName, "% S / /% S", DIRNAME, FOUNDFILENAME); deletefile (TempFileName);}}} tempfind.close ); if (! RemoveDirectory) // Delete Directory {AFXMessageBox ("Delete Directory Failed!", MB_OK; Return False;} Return True;} 5. • LIB and DLL File Differences and Contact

.dll is the file that is only connected when you run it, so it is a relatively small executable file format, and there are other file formats such as .ocx, all .dll files are executable. .

.lib is a file that is connected when your program is compiled, so you must inform the compiler to connect the lib file there. In general, the LIB file is also referred to as a static connection library than the dynamic connection file. When you compile the code into files in these formats, they can't be changed later. If you want to use the lib file, you must: 1? Contains a corresponding header file to inform the compiler lib file specific content 2? Set the lib file to allow the compiler to find the compiled binary code

If you want to isolate a DLL file from your code instead of a static connection library, you still need a lib file. This lib file will be connected to the program to tell the operating system When you run, what DLL file you want to use, in general, the name of the corresponding DLL file in the lib file and a sequence table indicating the DLL output function entry. If you don't want to use a lib file or there is no lib file, you can use Win32 API functions to LoadLibrary, getProcAddress. In fact, we can open the lib file in a binary form in Visual C IDE, most of the case of see the C function of the ASCII code or some of the overloaded functions.

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

New Post(0)