Visual C ++ programming skills

xiaoxiao2021-03-06  42

57. Why do the menu items are in a disabled state even after calling the EnableMenuItem menu item.

You need to set the cframeWnd :: m_bautomenuenable to false, if the data member is true (default), the workfrup will automatically disable the menu item without ON_UPDATE_COMMAND_UI or ON_COMMAND.

// disable mfc from automaticly disable menu items.

m_bauomenuenable = false;

// Now enable the menu item.

Cmenu * pmenu = getMenu ();

Assert_Valid (Pmenu);

PMenu-> EnableMenuItem (ID_MENU_ITEM, MF_BYCOMMAND | MF_ENABLED);

58, how to add a menu item to the system menu

Add a menu item to the system menu to make the following three steps:

First, use the Resource Symbols dialog (select Resource Symbols in the View menu ...

You can display this dialog) Define the menu item ID, which should be greater than 0x0f and less than 0xF000;

Second, call the CWnd :: GetSystemMenu to get the pointer to the system menu and call CWnd :: Appendmenu to add the menu item to the menu. The following example adds two new menu items to the system menu:

INT CMAINFRAME :: OnCreate (lpcreatestruct lpcreatestruct)

{

...

// Make Sure System Menu Item Is in The Right Range.

Assert (IDM_MYSYSITEM & 0xFFF0) == IDM_MYSYSITEM);

Assert (IDM-mysysItem <0xF000);

// Get Pointer to System Menu.

CMenu * psysmenu = getSystemMenu (false);

Assert_Valid (psysmenu);

// Add a separator and our menu item to system menu.

CSTRING STRMENUITEM (_T ("New Menu Item");

Psysmenu-> appendmenu (mf_separator);

Psysmenu-> appendmenu (mf_string, idm_mysysiSISISISISIM, STRMENUITEM);

...

}

Now, the user should detect when the system menu item is selected. Treatment with ClassWizard

WM_SYSCOMMAND message and detect NID parameters of the user menu:

Void CMAINFRAME :: OnSysCommand (uint nid, lparam lparam)

{

// determine if our system menu item was success.

IF ((NID & 0xFFF0) == IDM_MYSYSITEM)

{

// Todo-Process System Menu Item

}

Else

CmdiframeWnd :: OnSysCommand (NID, LPARAM);

}

Finally, a well-designed UI application should display a help information when the system menu item is highlighted, which can be implemented by adding a skewer containing the system menu base ID.

59. How do I determine the number of rows of menus occupied by topplates

This can be achieved by simple subtraction and division. First, the user needs to calculate the height and customer area of ​​the primary frame window; secondly, subtract the height of the client area, frame boundary, and title from the height of the mainframe window; finally, divide the menu bar. The following member function is a code implementation that calculates the number of rows occupied by the main box menu. INT CMAINFRAME :: getMenUROWS ()

{

CRECT RCFRAME, RCCLIENT;

GetWindowRect (rcframe);

GetClientRect (rcclient);

Return (rcclient.height () -rcclient.height () -

:: GetSystemMetrics (SM_CYCAPTION) -

(:: getSystemMetrics (sm_cyframe) * 2)) /

:: getSystemMetrics; SM_CYMENU;

}

60. How to determine the color of the system display element in the user environment

Calling the SDK function getSysColor can get a color of a specific display element. The following example shows how to call this function setting window title color in the MFC function cmainframewnd :: onncpaint.

Void cminiframewnd :: onncpaint ()

{

...

Dc.SetTextColor (:: getSyscolor (m_bactive?

Color_captionText: Color_INActiveCaptionText);

...

}

61, how to query and set system parameters

In the Windows 3.1 SDK, the SDK function systemParametersInfo is called, and the function can query and set the system parameters, such as the repeating rate setting of the button, the mouse double-click delay time, icon font, and desktop overlay, etc..

// Create a Font That Is Used for icon titles.

Logfont stfont;

:: SystemParametersInfo (Spif_Geticontitlelogfont,

Sizeof (logfont), & stfont, spif_sendwininichange;

m_font.createfontIndirect (& Stfont);

// Change the wallpaper to leaves.bmp.

:: SystemParametersInfo (SPI_SETDESKWALLPAPER, 0,

_T ("forward.bmp"), spif_updateinifile;

62. How to use a predefined Windows cursor

Call CWINAPP :: LoadStandardCursor and transfer the cursor identifier.

Bool Csampledialog :: OnsetCursor (CWND * PWND, UINT NHITTEST, UINT MESSAGE)

{

// Display Wait Cursor if Busy.

IF (m_bbusy)

{

SetCursor (AFXGetApp () -> LoadStandardCursor (IDC_WAIT));

Return True;

}

Return CDIALOG :: OnsetCursor (PWnd. Nhittest, Message);

}

63, how to determine the current screen resolution

Call the SDK function GetSystemMetrics, which can retrieve the Windows display information, such as title size, boundary size, and scroll bar size.

// Initialize CSIZE Object with screen size.

CSIZE SIZESCREEN (SM_CXSCREEN),

GetSystemMetrics (SM_CYSCREEN));

64. How to retrieve the original task list used by the Task Manager application The original Task Manager application displays a list of top-level windows. In order to display the list, the window

It must be visible, including a title and cannot be owned by other windows. Call CWnd :: getWindow

Retrieve the list of top-level windows, call iswindowvisible, getWindowTextLength, and getOwner

You can determine if the window should be in the list. The next example will populate the title of the TaskManager window into the list.

Void getTadklist (Clistbox & List)

{

CString strcaption; // capen of window.

List.resetContent (); // clear list box.

// Get First Window in Window List.

ASSERT_VALID (AFXGETMAINWND ());

CWND * PWND = AFXGETMAINWND () -> getWindow (gw_hwndfirst);

// Walk Window List.

While (PWND)

{

// I WINDOW Visible, HAS a CAPTION, AND DOES NOT HAVE AN OWNER?

IF (pwnd-> iswindowvisible () &&

PWnd -> getWindowTextLength () &&! pwnd -> getowner ())

{

// Add Caption o Window to List Box.

PWnd -> getWindowText (STRCAPTION);

List.Addstring (STRCAPTION);

}

// Get next window in window list.

PWND = PWND-> getWindow (GW_HWndNext);

}

}

65, how to determine the Windows and Windows system directory

There are two SDK functions to complete this feature. GetWindowsDirectory and GetSystemDirectory, the following example shows how to use these two functions:

Tchar Szdir [MAX_PATH];

// Get The Full Path of the Windows Directory.

:: getWindowsDirectory (SZDIR, MAX_PATH);

Trace ("Windows Directory% S / N", SZDIR);

// Get The Full Path of The Windows System Directory.

:: GetSystemDirectory (SZDIR, MAX_PATH);

Trace ("Windows System Directory% S / N", SZDIR);

66, where to create a document

Call the SDK function getTemPath to determine the directory of the temporary file, the function first detects the TMP environment variable for the temporary path: If TMP is not specified, the TMP environment variable is detected, and then returned to the current directory. The following example shows how to create a temporary file.

...

// Get Unique Temporary File.

CString Strfile;

GetUnique; Strfile;

Try

{

// Create File and Write Data.note That File Is Closed

// in the destructor of the cfile object.

Cfile File (Strfile, CFile :: MODECREATE | CFILE :: ModeWrite);

// Write Data

}

Catch (cfileException, e) {

// error Opening File

}

END_CATCH

...

Void getUniqueMpName (CString & Strtempname)

{

// Get The Temporary Files DIRECTORY.

Tchar sztemppath [max_path];

DWORD dWResult = :: getTemppath (max_path, sztempppath);

Assert (dwresult);

// CREATE A UNIQUE TEMPORARY FILE.

Tchar sztempfile [MAX_PATH];

Uint nresult = getTempFileName (Sztemppath, _T ("~ ex"), 0, sztempfile;

Assert (NRESULT);

strTempName = SzTempFile;

}

67, how to access the desktop window

Static function CWnd :: getDesktopWindow Returns the pointer to the desktop window. The following example illustrates how the MFC function cframeWnd :: BeGinModalStae uses this function to enter a list of internal window.

Void cframeWnd :: beginmodalstate ()

{

...

// First Count All Windows That Need To Be Disabled

Uint ncount = 0;

HWND HWND = :: getWindow (:: getdesktopWindow (), gw_child);

While (hwnd! = null)

{

IF (: iswindowenabled (hwnd) &&

CWnd :: fromHandlepermanent (hwnd)! = Null&&

AfxisDescendant (PParent-> M_HWND, HWND) &&

:: SendMessage (HWnd, WM_DISABLEMODAL, 0, 0) == 0)

{

NCOUNT;

}

HWnd = :: getWindow (hwnd, gw_hwndnext);

}

...

}

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

New Post(0)