|
What is the simplest way to change font of MDI? For example I want a menu with bold, Arial, size 10 font.
Create a menu and use SetFont() function seem to be a official way, but actually it doesn't work.
I found some articles by Google but not help much.
??
|
|
|
|
|
Setting Fonts for Menu-Item Text Strings
This topic contains an example from an application that uses owner-drawn menu items in a menu. The menu contains items that set the attributes of the current font, and the items are displayed using the appropriate font attribute.
Here is how the menu is defined in the resource-definition file. Note that the strings for the Regular, Bold, Italic, and Underline menu items are assigned at run time, so their strings are empty in the resource-definition file.
MainMenu MENU
BEGIN
POPUP "&Character"
BEGIN
MENUITEM "", IDM_REGULAR
MENUITEM SEPARATOR
MENUITEM "", IDM_BOLD
MENUITEM "", IDM_ITALIC
MENUITEM "", IDM_ULINE
END
END
The application's window procedure processes the messages involved in using owner-drawn menu items. The application uses the WM_CREATE message to do the following:
Set the MF_OWNERDRAW flag for the menu items.
Set the text strings for the menu items.
Obtain handles of the fonts used to draw the items.
Obtain the text and background color values for selected menu items.
The text strings and font handles are stored in an array of application-defined MYITEM structures. The application-defined GetAFont function creates a font that corresponds to the specified font attribute and returns a handle to the font. The handles are destroyed during the processing of the WM_DESTROY message.
During the processing of the WM_MEASUREITEM message, the example gets the width and height of a menu-item string and copies these values into the MEASUREITEMSTRUCT structure. The system uses the width and height values to calculate the size of the menu.
During the processing of the WM_DRAWITEM message, the menu item's string is drawn with room left next to the string for the check-mark bitmap. If the user selects the item, the selected text and background colors are used to draw the item.
Top
LRESULT APIENTRY MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
typedef struct _MYITEM
{
HFONT hfont;
LPSTR psz;
} MYITEM; // structure for item font and string
MYITEM *pmyitem; // pointer to item's font and string
static MYITEM myitem[CITEMS]; // array of MYITEMS
static HMENU hmenu; // handle to main menu
static COLORREF crSelText; // text color of selected item
static COLORREF crSelBkgnd; // background color of selected item
COLORREF crText; // text color of unselected item
COLORREF crBkgnd; // background color unselected item
LPMEASUREITEMSTRUCT lpmis; // pointer to item of data
LPDRAWITEMSTRUCT lpdis; // pointer to item drawing data
HDC hdc; // handle to screen DC
SIZE size; // menu-item text extents
WORD wCheckX; // check-mark width
int nTextX; // width of menu item
int nTextY; // height of menu item
int i; // loop counter
HFONT hfontOld; // handle to old font
BOOL fSelected = FALSE; // menu-item selection flag
size_t * pcch;
HRESULT hResult;
switch (uMsg)
{
case WM_CREATE:
// Modify the Regular, Bold, Italic, and Underline
// menu items to make them owner-drawn items. Associate
// a MYITEM structure with each item to contain the
// string and font handle for each item.
hmenu = GetMenu(hwnd);
ModifyMenu(hmenu, IDM_REGULAR, MF_BYCOMMAND |
MF_CHECKED | MF_OWNERDRAW, IDM_REGULAR,
(LPTSTR) &myitem[REGULAR]);
ModifyMenu(hmenu, IDM_BOLD, MF_BYCOMMAND |
MF_OWNERDRAW, IDM_BOLD, (LPTSTR) &myitem[BOLD]);
ModifyMenu(hmenu, IDM_ITALIC, MF_BYCOMMAND |
MF_OWNERDRAW, IDM_ITALIC,
(LPTSTR) &myitem[ITALIC]);
ModifyMenu(hmenu, IDM_ULINE, MF_BYCOMMAND |
MF_OWNERDRAW, IDM_ULINE, (LPTSTR) &myitem[ULINE]);
// Retrieve each item's font handle and copy it into
// the hfont member of each item's MYITEM structure.
// Also, copy each item's string into the structures.
myitem[REGULAR].hfont = GetAFont(REGULAR);
myitem[REGULAR].psz = "Regular";
myitem[BOLD].hfont = GetAFont(BOLD);
myitem[BOLD].psz = "Bold";
myitem[ITALIC].hfont = GetAFont(ITALIC);
myitem[ITALIC].psz = "Italic";
myitem[ULINE].hfont = GetAFont(ULINE);
myitem[ULINE].psz = "Underline";
// Retrieve the text and background colors of the
// selected menu text.
crSelText = GetSysColor(COLOR_HIGHLIGHTTEXT);
crSelBkgnd = GetSysColor(COLOR_HIGHLIGHT);
return 0;
case WM_MEASUREITEM:
// Retrieve a device context for the main window.
hdc = GetDC(hwnd);
// Retrieve pointers to the menu item's
// MEASUREITEMSTRUCT structure and MYITEM structure.
lpmis = (LPMEASUREITEMSTRUCT) lParam;
pmyitem = (MYITEM *) lpmis->itemData;
// Select the font associated with the item into
// the main window's device context.
hfontOld = SelectObject(hdc, pmyitem->hfont);
// Retrieve the width and height of the item's string,
// and then copy the width and height into the
// MEASUREITEMSTRUCT structure's itemWidth and
// itemHeight members.
hResult = StringCchLength(pmyitem->psz,STRSAFE_MAX_CCH, pcch);
if (FAILED(hResult))
{
// Add code to fail as securely as possible.
return;
}
GetTextExtentPoint32(hdc, pmyitem->psz,
*pcch, &size);
lpmis->itemWidth = size.cx;
lpmis->itemHeight = size.cy;
// Select the old font back into the device context,
// and then release the device context.
SelectObject(hdc, hfontOld);
ReleaseDC(hwnd, hdc);
return TRUE;
break;
case WM_DRAWITEM:
// Get pointers to the menu item's DRAWITEMSTRUCT
// structure and MYITEM structure.
lpdis = (LPDRAWITEMSTRUCT) lParam;
pmyitem = (MYITEM *) lpdis->itemData;
// If the user has selected the item, use the selected
// text and background colors to display the item.
if (lpdis->itemState & ODS_SELECTED)
{
crText = SetTextColor(lpdis->hDC, crSelText);
crBkgnd = SetBkColor(lpdis->hDC, crSelBkgnd);
fSelected = TRUE;
}
// Remember to leave space in the menu item for the
// check-mark bitmap. Retrieve the width of the bitmap
// and add it to the width of the menu item.
wCheckX = GetSystemMetrics(SM_CXMENUCHECK);
nTextX = wCheckX + lpdis->rcItem.left;
nTextY = lpdis->rcItem.top;
// Select the font associated with the item into the
// item's device context, and then draw the string.
hfontOld = SelectObject(lpdis->hDC, pmyitem->hfont);
hResult = StringCchLength(pmyitem->psz,STRSAFE_MAX_CCH, pcch);
if (FAILED(hResult))
{
// Add code to fail as securely as possible.
return;
}
ExtTextOut(lpdis->hDC, nTextX, nTextY, ETO_OPAQUE,
&lpdis->rcItem, pmyitem->psz,
*pcch, NULL);
// Select the previous font back into the device
// context.
SelectObject(lpdis->hDC, hfontOld);
// Return the text and background colors to their
// normal state (not selected).
if (fSelected)
{
SetTextColor(lpdis->hDC, crText);
SetBkColor(lpdis->hDC, crBkgnd);
}
return TRUE;
// Process other messages.
case WM_DESTROY:
// Destroy the menu items' font handles.
for (i = 0; i < CITEMS; i++)
DeleteObject(myitem[i].hfont);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return NULL;
}
HFONT GetAFont(int fnFont)
{
static LOGFONT lf; // structure for font information
// Get a handle to the ANSI fixed-pitch font, and copy
// information about the font to a LOGFONT structure.
GetObject(GetStockObject(ANSI_FIXED_FONT), sizeof(LOGFONT),
&lf);
// Set the font attributes, as appropriate.
if (fnFont == BOLD)
lf.lfWeight = FW_BOLD;
else
lf.lfWeight = FW_NORMAL;
lf.lfItalic = (fnFont == ITALIC);
lf.lfItalic = (fnFont == ULINE);
// Create the font, and then return its handle.
return CreateFont(lf.lfHeight, lf.lfWidth,
lf.lfEscapement, lf.lfOrientation, lf.lfWeight,
lf.lfItalic, lf.lfUnderline, lf.lfStrikeOut, lf.lfCharSet,
lf.lfOutPrecision, lf.lfClipPrecision, lf.lfQuality,
lf.lfPitchAndFamily, lf.lfFaceName);
}
|
|
|
|
|
It seem to be not very easy. I'll try it later.
Thank you very much, halibobo.
|
|
|
|
|
I am a Chinese girl.My English is not very good. I wish we can advance together.
|
|
|
|
|
There is no easy way. The menu is part of the non-client area, and windows is kindly handling for you.
The idea is that a user can pick a font / size she likes, and all her software will use it. You'd better have a damn good reason to override her preferences, so it's fine for it to be hard for you.
There are several good owner drawn menu articles here on codeproject for you to dig through, or look at halibobo's reply.
Iain.
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
|
|
|
|
|
sorry all, I tried to use another solution: codejock software.
it's ok now, thank you friends!
|
|
|
|
|
When using an STL with iterators, what is the right way to determine if the iterator is valid? Start with:
Vector < CMyClass * > MyList;
Vector < CMyClass * >::iterator Iter_1;
Vector < CMyClass * >::iterator Iter_2;
The program creates object of type CMyClass and puts pointers to those objects in the container. The program will compare various objects in the container with other objects, hence the need for two iterators.
For various reasons, either of the iterators might not be valid at any given time. If not, then I want to do Iter_1.begin(). If the iterator is valid, I don’t want to reset it. What test should be done to determine if it is valid?
If I delete an item that an iterator is referencing, is it valid to increment or decrement the iterator to get to either the next item in the list or the end of the list?
Thanks for your time
|
|
|
|
|
There is no portable way to tell if an iterator is valid.
Steve
|
|
|
|
|
dear ali rafiee
please send the mail for me
I am not speck EN language
i'm sorry for me
then
send mail to Dehghani.Fazel@gmail.com
or
o2_ir@yahoo.com
thanks
|
|
|
|
|
Here's the mail: what are you talking about and why are you talking about it here?
Steve
|
|
|
|
|
Hi, I'm trying to alter control properties at runtime.
I've got two images in a Picture Control (BMP) and I only want one visible at a time as they will overlap. I'm already animating them but I only want to see one Animation Frame at a time.
So, how can I set properties of controls at runtime? (I've already got control variables attached.)
Pointing out articles to me would also suffice.
thanks for your time,
|
|
|
|
|
Like2Byte wrote: how can I set properties of controls at runtime?
Which properties?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
This should be fun to watch
led mike
|
|
|
|
|
Not as fun as the conversation going on over at the dotnet-related board
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Same old noise, different day. At this point I don't believe it will improve, it might get worse I suppose.
Have you seen Graus's new article[^]? This is what we have come to! It gave me an idea for new Article, ok, an idea for an article title, "If you're an idiot go away". If they don't know how to determine that, they can ask in the forum for the article.
led mike
|
|
|
|
|
led mike wrote: Have you seen Graus's new article[^]? This is what we have come to!
Yes! I've linked to it before
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hello to all,
i am working to develop a media player in MFC. I am useing the FMODCE library to play the audio files. The problem is that the FSOUND_Stream_Open( "File name") function takes very large time to open the .MP# file format. So bcoze of this i seems that the application is going to halt.
Can any body help/guide me to overcome this difficulty. Or can tell me any other function/library to play .mp3, .wav,midi format file more accurately / and fastly. I shall be very thankful to all of u.
thanx in Advance.
|
|
|
|
|
shinewine wrote: The problem is that the FSOUND_Stream_Open( "File name") function takes very large time to open the .MP# file format.
Wouldn't the fmod forums be a more appropriate place to ask about this?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
i am guessing u are using the FSOUND_MPEGACCURATE mode?
if so just read the file into memory first then use FSOUND_LOADMEMORY mode instead.
might help maybe
|
|
|
|
|
Hi,
I have a SDI application that I wrote in .NET 2005 C++ and in unmanaged code, I have used the ST_SplitterWnd to split the windows, but now what I need to do is, I need to create tabs so that all the views are contained in one, so that I can make another instance of tabs when the next tab is clicked.
Now I have no clue how to do this. Is there anyone else out there that knows how to do this?
Thanks in advance
|
|
|
|
|
You want to use tabs instead of splitters?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Mark,
I still want the splitters, but I want say 3 splitters in 1 tab and 2 splitters in another.
|
|
|
|
|
You can use a tab window as the client of the main frame.
Then use frame windows as tab windows and use the splitters in those frame
windows as usual.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Do you by any chance have like a mock example?
Thanks for the help
|
|
|
|
|
For some reason I was thinking MFC, sorry. You can ignore the frame window comments.
I don't have a complete example, Which part is giving you trouble?
You just need to build your window hierarchy the way you want it.
For example:
Create the main window
Create a tab window as a child of the main window. See Tab Controls[^]
Add code to so the tab window gets resized to fit the client area of the main window.
Add tab windows to the tab control
Add code that resizes/repositions the tab windows appropriately within the tabs.
Add splitters to the tab windows.
etc.
etc.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|