|
Hi guys.
I'm new to MFC. I'm doing an SDI app with a ListBox on one pane. There are three panes on the app. I can't change the font style of the listbox control. Any idea on how to do that?
Regards,
_chew
|
|
|
|
|
_cheewex wrote: I can't change the font style of the listbox control.
Why not? What have you tried?
_cheewex wrote: Any idea on how to do that?
What's wrong with the SetFont() method?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I have tried these three methods so far...
// First way
CFont Font;
Font.CreateFont(12, // Height
0, // Width
0, // Escapement
0, // Orientation
FW_BOLD, // Weight
FALSE, // Italic
TRUE, // Underline
0, // StrikeOut
ANSI_CHARSET, // CharSet
OUT_DEFAULT_PRECIS, // OutPrecision
CLIP_DEFAULT_PRECIS, // ClipPrecision
DEFAULT_QUALITY, // Quality
DEFAULT_PITCH | FF_SWISS, // PitchAndFamily
"Arial")); // Facename
pListBox->SetFont(&Font);
// Second way
CFont Font;
LOGFONT lfLogFont;
memset(&lfLogFont, 0, sizeof(lfLogFont));
lfLogFont.lfHeight = 12; // 12-pixel-height
lfLogFont.lfWeight = FW_BOLD; // Bold
lfLogFont.lfUnderline = TRUE; // Underlined
strcpy(lfLogFont.lfFaceName, "Arial"); // Arial
Font.CreateFontIndirect(&lfLogFont);
pListBox->SetFont(&Font);
// Third way
CFont Font;
LOGFONT lfLogFont;
memset(&lfLogFont, 0, sizeof(lfLogFont));
lfLogFont.lfHeight = 120; // 12-pixel-height
lfLogFont.lfWeight = FW_BOLD; // Bold
lfLogFont.lfUnderline = TRUE; // Underlined
strcpy(lfLogFont.lfFaceName, "Arial"); // Arial
Font.CreatePointFontIndirect(&lfLogFont);
pListBox->SetFont(&Font);
|
|
|
|
|
Try using a font that doesn't go out of scope.
i.e. move your CFont object to someplace its lifetime lasts longer than the control
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
It appears that the CFont object is going out of scope. It must exist at least as long as the CListBox object. Can you confirm or deny?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I think it's not going out of scope. I noticed that it responded to size change only. I was not able to distinguish the change before because I just copied the size from MSDN. When I changed it significantly, it did change with respect to size but not with the font style ("Tahoma"). Don't know why it won't change.
|
|
|
|
|
Where are you calling the code you showed?
All those CFont objects look local to me, thus they are probably going out of scope and being destroyed,
before the control even has a chance to reflect the change I reckon..
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
|
Hello everyone,
I have tried to initialize an auto_ptr array, but failed. My C++ Programming Language book does not contain a sample about how to initialize an auto_ptr array.
(not an auto_ptr pointing to an array, which is not legal)
Any solutions?
#include <memory>
using namespace std;
int main()
{
auto_ptr<int[]> pi (new int[10]);
auto_ptr<int> pi (new int[10]);
return 0;
}
thanks in advance,
George
|
|
|
|
|
Like this :
auto_ptr<int> p(new int(10));
|
|
|
|
|
Hi _AnShUmAn_,
It is not correct code. Because the destructor of auto_ptr will use delete other than delete[]. And it will lead to memory leak.
Any ideas or comments?
regards,
George
|
|
|
|
|
George_George wrote: It is not correct code.
TRUE. Actually the code is correct, but is not a solution for your OP one.
George_George wrote: Because the destructor of auto_ptr will use delete other than delete[]. And it will lead to memory leak.
FALSE. It allocates just 1 integer (and initialise it to 10) hence no memory leaks after delete .
BTW: no compiler error for the code below
auto_ptr<int[10]> pi(new int[10]);
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Hi, CPallini,
1.
CPallini wrote: George_George wrote:
Because the destructor of auto_ptr will use delete other than delete[]. And it will lead to memory leak.
FALSE. It allocates just 1 integer (and initialise it to 10) hence no memory leaks after delete.
I am confused. We are talking about destructor of auto_ptr? Why do you say "allocates"?? Could you provide more description please?
2.
CPallini wrote: BTW: no compiler error for the code below
auto_ptr<int[10]> pi(new int[10]);
No, I have compile errors in MSVC 2008.
1>Compiling...
1>main.cpp
1>d:\visual studio 2008\projects\test0401\test0401\main.cpp(7) : error C2664: 'std::auto_ptr<_Ty>::auto_ptr(_Ty (*)) throw()' : cannot convert parameter 1 from 'int *' to 'int (*)[10]'
1> with
1> [
1> _Ty=int [10]
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
regards,
George
|
|
|
|
|
George_George wrote: I am confused. We are talking about destructor of auto_ptr? Why do you say "allocates"?? Could you provide more description please?
I report below _AnShUmAn_ code for reference.
auto_ptr<int> p(new int(10));
In the above expression, the new operator allocates one int and initialise it with the number 10 .
George_George wrote: No, I have compile errors in MSVC 2008.
Well, I have VS2005, and no errors here. However I've to admit I overlooked the following (serious) warning:
warning C4156: deletion of an array expression without using the array form of 'delete'; array form substituted
[...].
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Thanks CPallini,
1.
--------------------
deletion of an array expression without using the array form of 'delete'
--------------------
Means auto_ptr will use delete other than delete[] is destructor of auto_ptr?
2.
--------------------
array form substituted
--------------------
What means "array form substituted"?
regards,
George
|
|
|
|
|
George_George wrote: Means auto_ptr will use delete other than delete[] is destructor of auto_ptr?
I think so.
George_George wrote: What means "array form substituted"?
as the compiler output window shows
_Ty=int [10]
i.e. internal type _Ty (blindly) substitutes an array form.
Your example, IMHO shows:
(1) The std::auto_ptr though helpful is not a panacea.
(2) VC++ compiler is smart.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Thanks CPallini,
1.
The compiler deduce _Ty,
_Ty=int [10]
is because of the following code?
auto_ptr<int[10]>
2.
In your sample, compiler will make
A. one auto_ptr object wrapps an int array on heap;
or
B. ten auto_ptr objects and each object wrapps an int on heap?
regards,
George
|
|
|
|
|
1. Yes.
2. A.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Thanks CPallini,
I read the compile error message you posted,
--------------------
warning C4156: deletion of an array expression without using the array form of 'delete'
--------------------
I think your code will have memory leak potentially? Because delete other than delete[] will work on the array?
regards,
George
|
|
|
|
|
George_George wrote: I think your code will have memory leak potentially? Because delete other than delete[] will work on the array?
The delete syntax will be used (insted of the delete [] one).
About potential memory leaks, from MSDN [^]
The following two cases produce undefined results: using the array form of delete (delete [ ]) on an object and using the nonarray form of delete on an array.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
You mean your code will cause undefined behavior? CPallini?
regards,
George
|
|
|
|
|
Why do you ask people to repeat themselves all the time? Why?
led mike
|
|
|
|
|
Sorry led mike,
What is your reply to my original question? How to make an auto_ptr array and initialize it?
regards,
George
|
|
|
|
|
George_George wrote: What is your reply to my original question?
I have no answer for it. I don't understand the premise. auto_ptr should be used to implement exception safe locality and I don't understand the need to have an array of int pointers for local use. I would just put the ints on the stack and I would not use an array I would use a vector.
led mike
|
|
|
|
|
Hi led mike,
int is just used for demo purpose. You can use user defined data types, like class Foo. How to define an array of auto_ptr<foo> and initialization at the same time?
regards,
George
|
|
|
|