mfc >> Three MFC Questions

by Joseph M. Newcomer » Sun, 25 Apr 2004 23:17:41 GMT

See below...
On Sun, 25 Apr 2004 10:00:39 -0500, "Todd L. Peters" < XXXX@XXXXX.COM >

>I am new to MFC and have three questions.
>1. I would like to write a dialog box that allows users to choose the color
>for various entities in the program, say apples and oranges. Next to
>"Apples", the user can press a button, and the standard MFC Dialog for color
>picking comes up. This much is not a problem. However, how can I, after
>(and before) the color is picked, color the face of the button with the
>currently selected color?
Use an owner-draw button, and in the OnDrawItem, draw the background using PatBlt or
>The only way I know of is by displaying bitmaps,
>and it certainly seems impractical to have 256 (or more) bitmaps to display
>each and every possible color pick. Perhaps there is a way to color the
>face of the button with a color code?
No bitmaps required. Besides, you'd need 16MB of bitmap, but if you used bitmaps, you
would want to create it on-the-fly. But as I point out, it isn't necessary.

You can get an example of an owner-draw button by taking a look at my Better Bitmap Button
on my MVP Tips site. Throw away the code that works with the bitmap, and just retain the
core structure.
>2. I have been writing Dialog Boxes that dynamically configure and resize
>based on user selections within the dialog box. I have been using
>this->setWindowPos within the dialog box class to resize the window.
>However, is there a way to ensure that my "Accept" and "Cancel" buttons stay
>docked at the bottom of the dialog box, or do I have to move them as well?
>How do I move them?
SetWindowPos, same as all your other buttons. There is no concept of "docking" at runtime.
>3. How do I control the size and position of the application when it starts
>up? Do I do this by calling setWindowPos in the OnInit or Create functions
>for my MainFrm and ChildFrm classes, or is there another way?
SetWindowPos your dialog in OnInitDialog. In a dialog-based (as opposed to
CFormView-based) app, there is no MainFrm.cpp or ChildFrm.cpp.

In a CFormView-based app, you would do this, as you point out, in your CMainFrame class,
probably in the OnCreate.
>Thank you,
>Todd L. Peters

Joseph M. Newcomer [MVP]
MVP Tips:

Similar Threads

1. Three questions

My develop tools is VS.Net 2003 and my OS is windows2000 sp4.


I had created a MFC project with unmanaged modle. Because I want to call
some .net frame windows that I had added to this project so that I could add
Crysatl report view contral in it, I had changed the switch  Use Managed
Extensions in Project Property pages to Yes.But,when I run the programe
after I build this project,there is an error occur.


when this macro called, the error while occur.

If I change the project setting to RELEASE modle, the programe will be OK.


In this project, After I added the .Net frame windows and changed the switch
Use Managed Extensions in Project Property pages to Yes ,when I build the
project in debug modle, there is an link error that told me swith setting
uncompatible .

by the way,my project created by VS 6.0, I upgrade the project to VS2003.


with managed modle, I want to create a project which just like a MFC SDI
project that I can display different View In the frame window. How can I do
this? please give me some souce code or advise.

Please give me help!



2. Three simple questions

3. three questions about CoInitialize/CoUninitialize

Hello everyone,

Am I correct for the understanding of the following three questions about 


For each thread which utilizes COM, we need to call 
CoInitialize/CoUninitialize. Not for each process.


There is no harm to call CoUninitialize even if we do not call CoInitialize? 
And if we call CoInitialize N times on a thread, we need to call 
CoUninitialize the same of times (N times, not only 1 time) on a thread to 
release all resources?


CoInitializeEx will invoke CoInitialize?

thanks in advance,

4. ConnectionManager: three questions - Windows CE

5. Three questions about signed/unsigned type representations

Following a discussion on another thread here... I have tried to understand 
what is actually standardized in C++ regarding the representing of integers 
(signed and unsigned) and their conversions. The reference should be 3.9.1 
(Fundamental types), and 4.7 (Integral conversions).

It seems to me that the Standard doesn't specify:

1) The "value representation" of any of these types, except that (3.9.1/3) 
"... The range of nonnegative values of a signed integer type is a subrange 
of the corresponding unsigned integer type, and the value representation of 
each corresponding signed/unsigned type shall be the same."

2) The conversion from an unsigned type to a signed type when the signed 
type can't represent the value.

I have three questions:

1) The cited sentence from 3.9.1/3 is not clear to me. How, for example, the 
value representation for int and unsigned int can be the same as they have 
different sets of values? Shouldn't this sentence be restricted just to 
representations of these values that are common to both types (i.e. 
nonnegative ints) ?

2) Am I missing something, or there is really no word in the Standard about 
the "value representations" of unsigned types? 3.9.1/4 obviously describes 
just the arithmetic laws for unsigned types, but has nothing with their 
"value representations". Imagine we use 2 bits to represent unsigned int, 
with the set of values 0-3 and set of bit patterns 00, 01, 10, 11. One would 
expect that the following condition is always true:

n == (n & 1) + (n & (1 << 1));

(deliberately mixing arithmetic and bitwise operators). However, what if the 
value 1 is represented as 11 and 2 is represented as 00 (does anything in 
the Standard preclude such a representation?)? Then 1 << 1 is 2 (i.e. 00), 
the first operand of + is equal to n, the second is equal to 00, i.e. 2, and 
the result on the right side is actually n+2 (mod 4), not n.

(see also 5.8: operators << and >> are actually arithmetic operators, not 
bitwise operators, as they are defined on values, not on representations. 
This means that even replacing + with | wouldn't help in some cases).

If this is possible (at least on some weird but conforming implementation of 
the Standard), then one could hardly find a portable nontrivial C++ program 
in the world.

3) I haven't seen any additional rules for conversions from unsigned to 
signed types in case the signed type has the same (or greater - can it 
happen with a conforming implementation???) number of bits as the unsigned 
type, but still can't contain the value from the unsigned type. For 
example - conversions from unsigned int to int when the unsigned int value 
is greater than INT_MAX... (the opposite conversion is well-defined by 
4.7/2). If this is implementation-defined, that would mean, for example:

// -1 is int and is converted to 2^n-1 by 4.7/2
unsigned minusOneUnsigned = -1;

// implementation-defined, may be -0 on 1's complement machine
int minusOneInt = minusOneUnsigned;

// modulo 2^n conversion again, but of which value ?
unsigned minusOneUnsignedAgain = minusOneInt;

- we can't tell what the value of minusOneUnsignedAgain will be, after a 
double conversion.

My question is: does C++ standard specify anything in this case? I can 
understand that our minusOneUnsignedAgain can be 0, after all, instead of 
2^n-1, but this seems counterintuitive to me.


6. Three C/C++ Programming Questions

7. Rule of three example and some questions

Dear all,

I made a simple example case to experiment with the rule of three. The
code is below:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Exmpl{
  //default constructor
  Exmpl(){std::cout << "Default constructor of Exmpl" << std::endl; }
  // copy constructor
  Exmpl(const Exmpl &);
  // assignment operator
  Exmpl & operator=(const Exmpl&);
  // constructor with parameters
  Exmpl(std::string &str,int a, double b);
  // destructor
    std::cout << "Destructor for class is used" << std::endl;
  // getter function for the class variables
  void getVals();
  std::string *pstring;
  int i;
  double d;

Exmpl::Exmpl(const Exmpl& NN)
  std::cout << "Copy constructor is called" << std::endl;

Exmpl & Exmpl::operator=(const Exmpl& NN)
  std::cout << "Assignment operator is used" << std::endl;

Exmpl::Exmpl(std::string &str, int dec, double doub)
  std::cout << "Constructor with parameters is used" << std::endl;

void Exmpl::getVals(){

  cout << i << '\n'
       << d << '\n'
       << *pstring << '\n';

int main(){
  string str("try example");
  vector<Exmpl> vectorExmpl(5);
  Exmpl a(str,0,4.5);
  Exmpl b(a);
  Exmpl c=b;
  Exmpl *ptrExmpl=new Exmpl(c);
  delete ptrExmpl;
  return 0;

The output of the compiled code with g++ on debian etch is:

Default constructor of Exmpl **
Copy constructor is called
Copy constructor is called
Copy constructor is called
Copy constructor is called
Copy constructor is called
Destructor for class is used **
Constructor with parameters is used
Copy constructor is called
Copy constructor is called **
try example
try example
Copy constructor is called
try example
Destructor for class is used
Destructor for class is used
Destructor for class is used
Destructor for class is used
Destructor for class is used
Destructor for class is used
Destructor for class is used
Destructor for class is used
Destructor for class is used

My questions are on the two starred lines,

+ If I do not use a default constructor I am getting a compile time
+ Why is my assignment operator not working.
+ Why the destructor of the class is run in the middle, is that run
for the vector, if yes, is not vector still in the scope?

Many thanks for the replies and best regards,

8. A question about the rule of three....