Re: Altert boxes in MFC
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Thu, 24 May 2007 09:17:14 -0400
But it is generally a Really Bad Idea to do so. First, it requires having access to a
parent window (AfxMessageBox takes care of this for you). It requires supplying caption
text, which then causes confusion because the Microsoft recommended practice is to put the
name of the app as the caption (AfxMessageBox takes care of this for you). It requires
actual strings, while there is an overloaded form of AfxMessageBox that allows you to
specify the numeric ID of a STRINTTABLE resource and AfxMessageBox takes care of loading
the string.
You can supply the various icons and options to any form of message box creation calls,
including AfxMessageBox. See my MessageBox Explorer on my MVP Tips site.
Note also the code below is rather dated because it does not include the strings inside
_T(), which means that the strings must be 8-bit strings and you can't recompile this as
Unicode. It is important to develop good programming habits early in the MFC experience,
and there are several good rules to follow
1. Never put a local-language literal string in your program. If someone who does
not speak your language would need to see that string, then placing
it in your program is a design error. Use STRINGTABLE resources
2. When you need string or character literals, always enclose them in _T()
3. char does not exist except in very rare and exotic circumstances, and
if you do not have those, forget it exists as a data type
4. Use CString for all strings except in rare and exotic circumstances
5. strcpy and strcat no longer exist as usable functions. Prior to VS2005,
use StringCch... functions. VS2005 allows the use of strcpy_s and
strcat_s, but avoid ANY 'str'-named function; instead use the
Unicode-aware _tcs... functions
6. sprintf no long exists. See above point
7. new of arrays of objects should be avoided. Use CArray<type> or
std::vector<type> for arrays of objects
8. If the lifetime of the array is limited to a single scope, do not use 'new'
at all. Use a construct that is automatically deleted at the end
of the scope, e.g.,
LPBYTE data = new BYTE[n]; // bad style
...
delete [] data;
===================================
CByteArray data; // good style
data.SetSize(n);
9. If you use a global variable, you are wrong. If you think about it carefully,
and still decide to use one, you are still wrong. If you think about
it very hard, and it actually still makes sense, you might be right.
Exception (sometimes): global variables that start with 'const'.
10. Avoid putting anything in your CWinApp-derived class that can be used
by any other class anywhere else in your program. If you ever
write ((CMyApp*)AfxGetApp())-> you have a serious design error
in your program.
11. There is exactly one place your CWinApp-derived header file should appear
in your source files: in the CWinApp-derived .cpp file. In all other
files its appearance should be deleted. In most cases, simply replace it
by #include "resource.h". In some cases, such as subclassed
control classes, it can often be simply deleted with no replacement.
Alas, the VS tools throw this in everywhere, requiring constant vigilance
to see that it is deleted.
12. Never put anything in stdafx.h that belongs to your project. Until you have
enough experience to know the implications of doing this, you can
generally consider that any #define of constants, other than
build-configuration issues, is a mistake, inclusion of any variable
reference is a mistake, and including any header file via #include ""
is a mistake. If it isn't a #include<> of some standard library file,
assume it does not belong there.
13. Never edit stdafx.cpp. Ever.
This is just a start, but you might want to also check out my essay about "The n Habits of
Highly Unsuccessful Windows Programs" on my MVP Tips site.
joe
On Thu, 24 May 2007 02:13:03 -0700, rodream <rodream@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
You can use this function in any language.Joseph M. Newcomer [MVP]
MessageBox ( HWND, MessageText, CaptionText, ICONINFORMATION );
e.g.
::MessageBox( m_hWnd, "Message to Show", "Title of Message Box",
MB_ICONINFORMATION|MB_OK);
(*in C++, :: means use the global namespace)
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: Altert boxes in MFC
- From: MrAsm
- Re: Altert boxes in MFC
- References:
- Altert boxes in MFC
- From: manasa20
- Altert boxes in MFC
- Prev by Date: Re: Seeing VERSIONINFO under Vista?
- Next by Date: Re: MFC visual C++ help
- Previous by thread: Re: Altert boxes in MFC
- Next by thread: Re: Altert boxes in MFC
- Index(es):
Relevant Pages
|