|
To get a client rectangle of a child do the following:
CRect crChild;
pWndChild->GetWindowRect(&crChild);
ScreenToClient(&crChild);
Things to Note:
This is called from the CWnd derived class of the parent.
pWndChild is a pointer to the child window you want the coordinates for.
Ant.
|
|
|
|
|
Coordinates in a resource file are specified in dialog base units, while coordinates used in Windows get/move/size APIs are specified in pixels. GetDialogBaseUnits() is one method you can use to convert between the two.
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
class A<br />
{<br />
public:<br />
A() :<br />
i(0)<br />
{<br />
}<br />
int i;<br />
};<br />
<br />
class B : public A<br />
{<br />
public:<br />
void Inc()<br />
{<br />
i++;<br />
}<br />
};<br />
<br />
A *pa = new A;<br />
B *pb = (B *)pa;<br />
pb->Inc();
The pointer is not a type B, but class B has no own
data and if i run it it works.
Is this valid?
What happens when i do that?
/Magnus
- I don't necessarily agree with everything I say
|
|
|
|
|
It is valid
Inc uses i of B inherited from A
When u cast the pointer, pb has all the class info of B, he can find Inc (which is logic ) and can find i, so no problem
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
yes it works, and you're not even obliged to cast into (B*).
the pointer is a base class pointer and it calls the B::Inc() function.
you could do much interresting, with creating another Inc() function into class A...
what happens now? A::Inc() is called. strange ? no. it is a well known consequence of virtual keyword.
when you have many derives classes (as here), and into which many functions have the same name, you'll have to declare virtual (in the base class) the function if you want with such command lines that the B::Inc() is called.
in brief, :
without virtual :
<font style="color:blue;">class </font>A {
<font style="color:blue;">protected</font>:
<font style="color:blue;">int</font> i;
<font style="color:blue;">public</font>:
<font style="color:blue;">void</font> Inc (<font style="color:blue;">void</font>);
};
<font style="color:blue;">class </font>B : <font style="color:blue;">public class</font> A {
<font style="color:blue;">public</font>:
<font style="color:blue;">void</font> Inc (<font style="color:blue;">void</font>);
};
A* pa = <font style="color:blue;">new</font>(B);
pa->Inc(); <font style="color:green;">
with virtual :
<font style="color:blue;">class </font>A {
<font style="color:blue;">protected</font>:
<font style="color:blue;">int</font> i;
<font style="color:blue;">public</font>:
<font style="color:blue;">virtual void</font> Inc (<font style="color:blue;">void</font>);
};
<font style="color:blue;">class </font>B : <font style="color:blue;">public class</font> A {
<font style="color:blue;">public</font>:
<font style="color:blue;">void</font> Inc (<font style="color:blue;">void</font>);
};
A* pa = <font style="color:blue;">new</font>(B);
pa->Inc(); <font style="color:green;">
ps: why do you put your int i; into public statement ?
TOXCCT >>> GEII power
|
|
|
|
|
Yes, i know how virtual baseclasses work, what i was
wondering was about casting to a subclass with no data
from a baseclass.
toxcct wrote:
ps: why do you put your int i; into public statement ?
I did it so a could access it from B and i dont like
the protected keyword..=)
/Magnus
- I don't necessarily agree with everything I say
|
|
|
|
|
|
There are many cases where a member can be public without issue. Look at the CRect class. What would be the point of protecting left, top, right and bottom?
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
why do we protect data members ?
TOXCCT >>> GEII power
|
|
|
|
|
I am not saying you shouldn't, I am saying you don't have to.
This isn't a black/white issue. It isn't about always doing it or never doing it. It is about doing it when it is appropriate.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
I dont really see the point with the protected keyword.
Maybe someone inherits from the class and then it can be modified, either it is publicly modifiable or not, i dont see the point
with have it "maybemodifable".
/Magnus
- I don't necessarily agree with everything I say
|
|
|
|
|
no, a protected member is only modifiable by the derived classes objects (and of course objects from the class itself), and occasionately by friends functions too, but in any case by the outside.
TOXCCT >>> GEII power
|
|
|
|
|
Yes i know how it works i just dont see the point with the protected keyword.
/Magnus
- I don't necessarily agree with everything I say
|
|
|
|
|
_Magnus_ wrote:
i dont see the point
sorry, my english is poor sometimes
what do you mean with this ?
TOXCCT >>> GEII power
|
|
|
|
|
The protected keyword is for people who trust others to properly augment the implementation of a class in a derived class.
In the last three or four weeks I have wasted endless amounts of time because VCL (Borland's MFC) made far too many things private. I couldn't fix the bugs in their code without completely replacing their classes.
If they had made their stuff protected and trusted me, I would have saved a lot of time.
I hardly ever use the private keyword because I trust the programmers not to screw up the implantation. If they do screw things up, I beat them up and then make them fix it. I would rather them do that than have to reimplement my classes in their own stuff creating two implementations of the same theme.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
The protected keyword lets you differentiate between accesses of the value by users of the class from accesses by derived classes. Ordinary users of the class may not access a member marked protected . A derived class, which supplements or modifies the behavior of the base class, is granted privileged access.
Software Zen: delete this;
|
|
|
|
|
What you are doing will work then, but it is really bad code. Sometimes people do what you do when they have no choice and need to access protected and private members in a given class. This is usually do to poor design of the base class. (*cough* BORLAND VCL)
However, what you are doing is very questionable indeed. It looks like a maintenance nightmare.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
It works but it is very very bad (and wrong).
What if B had another variable J and a routine IncJ? If you invoked IncJ, you would be trashing memory and could cause your program to crash.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Yes i know its really ugly but i managed to do it by
misstake and got a bit surprised that it worked. =)
/Magnus
- I don't necessarily agree with everything I say
|
|
|
|
|
IT may work for your particular sutiation... but the proper way of casting in this sense is with dynamic_cast:
A *pa = new A;
B *pb = dynamic_cast<B *>(pa);
pb will either contain a valid pointer to a B object, or will be NULL if it can't be casted safely.
Sometimes I feel like I'm a USB printer in a parallel universe.
|
|
|
|
|
It works by accident. It's why you should never use the old style C cast. This compiles,
B * pb = (B*)0;
pb->Inc () ;
but goes bang.
Paul
|
|
|
|
|
Hmm
B * pc = reinterpret_cast < B *> (0);
This compiles too, so does that mean you should never use it either?
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
reinterpret_cast stands out like a sore thumb whereas the C style one doesn't. In either case you can blow your foot off by demanding the compilation of something absurd, like the original poster.
Paul
|
|
|
|
|
Hello Robert,
After going through the Code Project tutorial "Three Ways to inject Your Code into Another Process" I have started writing an application.
In that applcation in NewProc I want to use some local variables which are of types char* (char array of size 100) , int, HMODULE etc. I want to load a library in the address space of the remote process , and acess the library's function by using GetProcAddress API. But the problem that I am encountering
is that the remote process just crashes if I use assignment in the code in
NewProc, such as HMODULE h; h=LoadLibrary("libname");
such assignment operations even in case of integers are causing the remote process crash. Will you please help me in this regard? Thanks for any
suggestion in this regard.
Biswas
|
|
|
|
|
You should use GlobalAlloc and GlobalLock
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|