Visual c ++ programming skills

Source: Internet
Author: User
Visual c ++ programming skills

 

57. Why is the menu item still disabled even after the enablemenuitem menu item is called?

58. How to add a menu item to the System Menu

59. How to determine the number of menu lines occupied by top-level menus

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

61. How to query and set System Parameters

62. How to use a predefined windows cursor

63. How to determine the current screen resolution

64. How to retrieve the task list used by the original Task Manager Application

65. How to Determine windows and Windows Directories

66. Where can I create a temporary file?

67. How to access the desktop window

 

57. Why is the menu item still disabled even after the enablemenuitem menu item is called?

You need to set cframewnd: m_bautomenuenable to false. If the data member is true (default), the Work box will automatically Disable menu items without on_update_command_ui or on_command.

// Disable MFC from automatically disabling 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

To add a menu item to the system menu, perform the following three steps:

First, use the resource symbols Dialog (in the View menu, select resource symbols...

Can display this dialog box) define the menu item ID, which should be greater than 0x0f and less than 0xf000;

Next, call cwnd: getsystemmenu to obtain the pointer of the System menu and call cwnd: appendmenu to add the menu item to the menu. In the following example, two new menu items are added to the system menu:

Int cmainframe: oncreate (maid)

{

...

// 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_mysysitem, strmenuitem );

...

}

Now, when selecting the system menu item, the user should perform detection. Use classwizard for processing

Wm_syscommand:

Void cmainframe: onsyscommand (uint NID, lparam)

{

// Determine if our system menu item was selected.

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 message in the status bar when the system menu item is highlighted, this can be achieved by adding an entry to the string table containing the system menu base ID.

59. How to determine the number of menu lines occupied by top-level menus

This can be achieved through simple subtraction and division. First, you need to calculate the height of the main box window and the customer area. Second, subtract the height of the customer area, box border, and title from the height of the main box window. Finally, divide it by the height of the menu bar. In the following example, a member function is used to calculate the number of lines occupied by the Main Box menu.

Int cmainframe: getmenurows ()

{

Crect rcframe, rcclient;

Getwindowrect (rcframe );

Getclientrect (rcclient );

Return (rcframe. 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

Call the SDK function getsyscolor to obtain the color of a specific display element. The following example shows how to set the title Color of the window by calling the function cmainframewnd: onncpaint In the MFC function.

Void cminiframewnd: onncpaint ()

{

...

DC. settextcolor (: getsyscolor (m_bactive?

Color_captiontext: color_inactivecaptiontext ));

...

}

61. How to query and set System Parameters

We have introduced the SDK function systemparametersinfo in the Windows 3.1 SDK. You can call this function to query and set system parameters, such as the repeat rate setting of buttons, the delay time of double-clicking the mouse, the icon font, and the desktop overlay bitmap.

// 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_set1_wallpaper, 0,

_ T ("forest.bmp"), spif_updateinifile );

62. How to use a predefined windows cursor

Call cwinapp: loadstandardcursor and send 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 to retrieve information about Windows display, such as the title size, boundary size, and scroll bar size.

// Initialize csize object with screen size.

Csize sizescreen (getsystemmetrics (sm_cxscreen ),

Getsystemmetrics (sm_cyscreen ));

64. How to retrieve the task list used by the original Task Manager Application

The previous Task Manager application displays the list of top-level windows. To display the list

Must be visible, contain a title, and cannot be owned by other windows. You can call cwnd: getwindow.

To retrieve the list of top-level windows, call iswindowvisible, getwindowtextlength, and getowner

You can determine whether the window should be in the list. In the following example, the title of the TaskManager window is filled in the list.

Void gettadklist (clistbox & List)

{

Cstring strcaption; // caption 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 windows and Windows Directories

There are two SDK functions to complete this function. 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 can I create a temporary file?

Call the SDK function gettempath to determine the directory of the temporary file. This function first detects the TMP environment variable for the temporary path: If the TMP environment variable is not specified, check the TMP environment variable and return it to the current directory. The following example shows how to create a temporary file.

...

// Get unique temporary file.

Cstring strfile;

Getuniquetempname (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 getuniquetempname (cstring & strtempname)

{

// Get the temporary files directory.

Tchar sztemppath [max_path];

DWORD dwresult =: gettemppath (max_path, sztemppath );

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

The static function cwnd: getdomaintopwindow returns the pointer of the desktop window. The following example illustrates how the MFC function cframewnd: beginmodalstae uses this function to enter the internal window list.

Void cframewnd: beginmodalstate ()

{

...

// First count all windows that need to be disabled

Uint ncount = 0;

Hwnd =: getwindow (: get1_topwindow (), gw_child );

While (hwnd! = NULL)

{

If (: isw.wenabled (hwnd )&&

Cwnd: fromhandlepermanent (hwnd )! = NULL &&

Afxisdescendant (pparent-> m_hwnd, hwnd )&&

: Sendmessage (hwnd, wm_disablemodal, 0, 0) = 0)

{

++ Ncount;

}

Hwnd =: getwindow (hwnd, gw_hwndnext );

}

...

}

Related Article

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.