Application of MFC specific functions 20160720

Source: Internet
Author: User
Tags function prototype

1.SystemParametersInfo functions can get and set a large number of Windows system parameters

MFC can use the SystemParametersInfo (...) function to get and set system information, as shown in the following example, changing the height of the system menu bar. Example: Changing the height of the system menu bar

Nonclientmetrics NCM;
ncm.cbsize = sizeof (nonclientmetrics); This is very important, otherwise the following function call will return 0, that is, ret=0, indicating that the function call failed

int Ret=::systemparametersinfo (spi_getnonclientmetrics,sizeof (nonclientmetrics), &ncm,0);

CString str;
Str. Format ("Return value:%d,%d", ret,ncm.imenuheight);
AfxMessageBox (str);

ncm.imenuheight+=10; Set system Menu Height plus 1
Ret=::systemparametersinfo (Spi_setnonclientmetrics,sizeof (nonclientmetrics), &ncm,0);

Ret=::systemparametersinfo (Spi_setnonclientmetrics,sizeof (nonclientmetrics), &ncm,spif_sendchange);

The last parameter is 0 or Spif_sendchange, which indicates that the change will be written to the update Win.ini

Str. Format ("Return value:%d,%d", ret,ncm.imenuheight);
AfxMessageBox (str);

In particular, if you want to use it to set the application's related properties, be careful, because it changes the properties of the operating system, which means that the properties of other applications on the system are also changed.

Use it to get information about the system, such as screen width, height, menu bar, title bar height, and more useful.

function function Description: Query or set system-level parameters. The function can also update the user profile in the settings parameter. Function prototype b00l SystemParametersInfo (UINT uiaction,uint uiparam,pvoid pvparam,uint fwinlni); parameter uiaction: This parameter specifies the system-level parameters to query or set. The values are as follows; Spi_getaccesstimeout: Retrieves the time-out information associated with an accessible attribute, the Pvparam parameter must point to a accesstimeout structure for information, The value of the Cbsjze member and the Ulparam parameter in the structure is set to sizeof (Accesstimeout).

Spi_setworkarea: Sets the size of the work area. A workspace is a section of the screen that is not obscured by the system tray or desktop application desktop tools. The parameter pvparam is a pointer. Points to a RECT structure that specifies a new rectangular working area, which is expressed as a virtual screen coordinate. In a multi-monitor system, this function is used to set the display work area that contains a specific rectangle. If Pvparam is null, the function sets the work area of the primary display to full screen.
Spi_getworkarea: Retrieves the size of the workspace. A workspace is a portion of a screen that is not obscured by a task.
voidRuncashdialog::ongetminmaxinfo (minmaxinfo far* Lpmmi)//still a little bit bug, on this page to move the taskbar, and then back to the main interface has a bug{    //todo:add your message handler code here and/or call default//lpmmi->ptmaxsize.y = GetSystemMetrics (sm_cyfullscreen) + getsystemmetrics (sm_cycaption) + GetSystemMetrics (SM _cydlgframe);CRect RT; SystemParametersInfo (Spi_getworkarea,0, &rt,0); Lpmmi->ptmaxsize.x =Rt.    Width (); Lpmmi->ptmaxsize.y =Rt.      Height (); Cdialog::ongetminmaxinfo (Lpmmi);}

2.GetWindowRect and GetClientRect

GetWindowRect is the RECT coordinates of the window in the screen coordinate system (both client and non-client areas), which gives the window size and position relative to the upper-left corner of the screen (0,0).

GetClientRect gets the rect coordinates of the window client area (excluding the non-client area) in the client area coordinate system, can get the size of the window, and cannot get the relative screen position, because this matrix is in the customer area coordinate system (relative to the upper left corner of the window client area).

The ClientToScreen converts the RECT coordinates in the client-area coordinate system to the RECT coordinates under the screen coordinate system.

The screentoclient converts the RECT coordinates in the screen coordinate system to the RECT coordinates under the client-area coordinate system.//Use?

We getwindowrect the same window first to obtain a rect, and then convert the screentoclient to the customer coordinate system. Then GetClientRect gets a rect and then converts it to the screen coordinate system with ClientToScreen. Obviously, the matrix obtained by GetWindowRect is not less than the matrix obtained by GetClientRect. Because the former includes the non-client area, and then the customer area.

After GetWindowRect obtained matrix screentoclient, the size of the matrix is not smaller, ( -3,-29) is the coordinates of the upper left corner of the window, relative to the upper left corner of the window client area.

After the matrix ClientToScreen obtained by GetClientRect, the matrix is not larger, and the newly obtained matrix is a rect of the window client area on the screen coordinate system.

Api:

To get the screen size, you can use the following functions: int cx = GetSystemMetrics (Sm_cxfullscreen);
int cy = GetSystemMetrics (Sm_cyfullscreen), obtained by the top two functions displays the size of the screen, but does not include areas such as the taskbar

int cx = GetSystemMetrics (Sm_cxscreen);
int cy = GetSystemMetrics (Sm_cyscreen); These two functions get the actual size of the screen.
3.WriteProfileString

WriteProfileString is written to the system directory, you need to use writeprivateprofilestring, so you can write to any directory of your own definition.

WriteProfileString can write to the registry or to the INI file, the key is Setregistrykey, if executed Setregistrykey, then will be written to the registry, if not, will be written to the INI file

Sometimes, we need to write the initialization information to the configuration file, and read the initialization information from this configuration file when the program starts. Most software now writes this information to the registry, and then reads the initialization information from the registry when the software is started. But some of the early procedures were to write this informationWin.ini FileIn The file is located under the Windows directory at the root of the system installation.
In the program, if you want to write some initialization information to the Win.ini file, you can use the WriteProfileString function to implement it. It is important to note that Windows provides this function only for compatibility with 16-bit versions of applications, and WIN32-based programs should use the registry to store initialization information. The WriteProfileString function is prototyped as follows:
BOOL writeprofilestring (
LPCTSTR
Lpappname,   Section name
LPCTSTRLpkeyname,   Key Name
LPCTSTRlpstringString to write
  );
Below, we create an empty MFC project file, which is used in the file programthe WriteProfileString function writes initialization information for the program to the Win.ini file, and place this action in the InitInstance function of the Cfileapp class, adding code after the Setregistrykey function call.


1BOOL cfileapp::initinstance ()
2{
3 AfxEnableControlContainer ();
4
5//Standard initialization
6//If You is not using these features and wish to reduce the size
7//of your final executable, should remove from the following
8//The specific initialization routines you does not need.
9
10#ifdef _afxdll
Enable3dcontrols (); Call the When using the MFC in a shared DLL
12#else
Enable3dcontrolsstatic (); Linking to MFC statically
14#endif
15
Setregistrykey (__t ("Local appwizard-generated Applications"));
+:: WriteProfileString ("http://www.cnblogs.com", "admin", "Lantionzy");
18
19
return TRUE;
21}

       This code will write some information to the Win.ini file, create a new segment with the name: "Http://www.cnblogs.com", create a new key on it, The key name is: admin, the value is: Lantionzy. Build and run the program, and then open the Win.ini file, you can see the newly added segment name, key name, and its value in the file.
       has a writeprofilestring function in the CWinApp class, so what's the difference between it and the corresponding function in Win32API?

The WriteProfileString function in the CWinApp class is declared as follows:
BOOL writeprofilestring ( lpctstr lpszsection, lpctstr lpszentry , LPCTSTR Lpszvalue );
you can see that the writeprofilestring function provided in the CWinApp class is consistent with the WIN32API provided. However, using the result is different, on my machine, after calling the CWinApp class's writeprofilestring function, the information will be written to the registry, not the Win.ini file.
Change the 17th line in the above code to this: ( remove the domain name Resolver ::)

WriteProfileString ("http://www.cnblogs.com", "admin", "Lantionzy");

Run the File program and open the registry and you'll see the information we just wrote at:hkey_current_user/software/local appwizard-generated applications/file subkey, and the registry key is the same as the information written in the file program.

Note: In different operating system environments, the WriteProfileString function provided in the CWinApp class stores the information in a different place, either written to the Win.ini file or written to the registry. For example, under the Windows NT system, the function stores the information in the registry, and under the Windows 3.X system, the information is written to the Win.ini file.

4.GetSystemMetrics

int WINAPI getsystemmetrics (__in intnIndex);

The following is the definition of the GetSystemMetrics function parameter nindex:

The SM_ARRANGE flag is used to illustrate how the system arranges minimized windows. For details, please refer to the remarks below.:

Depending on the display of the display, system data may vary.

The Sm_arrange setting specifies how the system arranges the minimized window and contains a starting position and orientation. The starting position can be one of the following values.

Value

Meaning

Arw_bottomleft

Start the screen in the lower left corner (the default location).

Arw_bottomright

Start at the bottom right corner of the screen. Equivalent to Arw_startright.

Arw_hide

In the visible area of the screen, hide and minimize the window ...

Arw_topleft

From the upper-left corner of the screen. Equivalent to Arv_starttop.

Arw_topright

Equivalent to Arw_starttop | Srw_startright.

The direction of the arrangement can be one of the following values.

Value

Meaning

Arw_down

Arranged vertically, from top to bottom.

Arw_left

Arranged horizontally, from left to right.

Arw_right

Arranged horizontally, right to left.

Arw_up

Arranged vertically, from bottom to top.

5.MoveWindow and SetWindowPos

MoveWindow can only set the size and position of the window;

SetWindowPos has all the functions of MoveWindow, you can also set the Cascade Relationship of the window (for example, place the specified window at the top of all Windows--always on top to use this function, or place the specified window on the lower level of the other window, etc.).

void   movewindow (    int   x,   int   y,   int    nwidth,   int   nheight,   bool  brepaint    =   TRUE   );   
Void   movewindow (    LPCRECT   lpRect,   BOOL   bRepaint    =   TRUE   );   

Parameters
x Specifies the new position to the left of CWnd.
y Specifies the new position at the top of the CWnd.
NWIDTH Specifies the new width of CWnd.
nheight specifies a new height for CWnd.

BREPAINT Specifies whether to repaint CWnd. If true, CWnd receives a WM_PAINT message in the OnPaint message handler function as usual. If this parameter is False, no redraw action of any kind will occur. This applies to the customer area, the non-client area (including the title bar and scroll bar), and any portion of the parent window that is exposed as a result of CWnd movement. When this parameter is false, the application must explicitly invalidate or redraw the parts of CWnd and the parent window that must be redrawn. A Lprectcrect object or RECT structure that specifies the new size and position. Indicates that the function changes the position and size of the window. For the top-level CWnd object, the x and Y parameters are relative to the upper-left corner of the screen. For child objects, they are relative to the upper-left corner of the client area of the parent window.
The MoveWindow function sends a WM_GETMINMAXINFO message. When handling this message, CWnd gets an opportunity to change the maximum and minimum window default values. If the arguments passed to the MoveWindow member function exceed these values, the values can be substituted with the minimum or maximum values in the Wm_getminmaxinfo handler.


BOOL SetWindowPos (const cwnd* pwndinsertafter, int x, int y, int cx, int cy,uint nflags);
return value if the function succeeds, it returns a value other than 0, otherwise 0 is returned.
The parameter pWndInsertAfter identifies the CWnd object that precedes the CWnd object in the Z-order. This parameter can be a pointer to a CWnd object, or a pointer to the following values: L WNDBOTTOM the window at the bottom of the z-axis order. If this CWnd is a top-level window, the window loses its top-level state; the System places the window at the bottom of all other windows. L Wndtop Place the window at the top of the z-axis order.

L WNDTOPMOST the window on top of all non-top-level windows. This window will hold its top-level position even if it loses its active state. Wndnotopmost the window to the top of all non-top-level windows (which means under all top-level windows). This flag does not work for windows that are already non-top-level windows. See the Description section for the usage rules for this function and these parameters. x Specifies the new position to the left of the window. y Specifies the new position at the top of the window. CX Specifies the new width of the window. CY Specifies the new height of the window. NFLAGS Specifies the size and location options. This parameter can be a combination of the following values: L Swp_drawframe draw a border around the window (defined when creating the window).

L Swp_framechanged sends a WM_NCCALCSIZE message to the window, even if the size of the window does not change. If this flag is not specified, the Wm_nccalcsize message is sent only when the size of the window changes.

L Swp_hidewindow hidden window. Swp_noactivate does not activate the window. If this flag is not set, the window is activated and moved to the top or non-top-level window group (dependent on the setting of the pWndInsertAfter parameter).

L Swp_nocopybits Discard the content of this client area. If this parameter is not specified, the valid contents of the client area are saved and copied back to the customer area after the window's size or position is changed. L Swp_nomove Keep the current position (ignoring the x and Y parameters).

L Swp_noownerzorder does not change the position of the owner window in the z-axis order.

L Swp_noredraw do not redraw changes. If this flag is set, no change of any kind occurs. This applies to the customer area, the non-client area (including the title and scroll bar), and any part of the parent window that is covered by the moved window. When this flag is set, the application must explicitly invalidate or redraw any part of the window and the parent window to repaint.

L Swp_noreposition is the same as Swp_noownerzorder.

L swp_nosendchanging prevent Windows from receiving wm_windowposchanging messages.

L Swp_nosize maintains the current size (ignoring CX and CY parameters).

L Swp_nozorder Keep the current order (ignore pWndInsertAfter).

L Swp_showwindow Display window.

Application of MFC specific functions 20160720

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.