|
I have never seen anyone post a joke/game message so I'm going to now. Here are three games that I have written. There on my web site and I know there not that great but I want to shere them any way. You probably will not be interested in "grace game" but the other two may be kinda neat. Any comments are welcome.
http://www.productionkey.com/programs/[^]
----------------------------------------------------------------
"To an optimist the glass is half full.
To the pessimist the glass is half empty.
To the programmer the glass is twice as big as it needs to be."
GC13: "Mature-Ages 16 and up. 'your mature, right?'"
FARGORE: "no, but I am over 16."
-- modified at 9:04 Sunday 26th February, 2006
|
|
|
|
|
Hi,
I'd like to know how the 'address of (&)'
applies to methods in Visual C++.
Here is an example :
<br />
<br />
void CMainWnd::OnRButtonDown(UINT nFlags, CPoint point)<br />
{<br />
CWindowDC dc(this);<br />
<br />
CRect rc;<br />
GetWindowRect(&rc);<br />
<br />
}<br />
<br />
Whats the difference between :
GetWindowRect(&rc); and
GetWindowRect(rc);
I would appreciate some more examples along with an explanation with
what it does, why and how? I know I'm asking alot here but I would very appreciative.
Thanks in Advance
Tom
|
|
|
|
|
Hi,
in the case u describe it is used to pass the object by address or by reference.
It will depend on the signature of the called function. If I'm not mistaken this
function GetWindowRect expects a pass by address (a pointer) because its
based on the an API call which is written in plain C where there are no references
int iValue = 3;
int* pValue = &iValue;
*pValue = 4;
int& rValue = &iValue;
rValue = 5; Here you van see that when using a pointer (pValue) you need the to dereference it
using the '*' operator;
When using the reference (rValue) you can use it like you would have used iValue;
Now this brings the following benefit, look at the following code:
struct Point{
int x;
int y;
};
void FunctionA(Point ptMouse)
{
ptMouse.x = 100;
}
void FunctionB(Point* pMouse)
{
pMouse->x = 100;
}
void FunctionC(Point& rMouse)
{
rMouse.x = 100;
} When using FunctionA the Point object will be copied onto the stack and
in the function you only use this copy. [pass by value]
When using FunctionB only the address is placed on the stack and inside the function
you can access the original object. So when changing the object inside the function is will
be changed outside the function (its the same object) [pass by address]
When using FunctionC alsoo only the address is placed on the stack and inside we are using the original object. So when changing the object inside the function is will
be changed outside the function. [pass by reference]
When passing large objects (classes with many members, or large ones) the pass by value is not so good (slow, stack space consuming). So either pass by address or pass by reference are preferred.
codito ergo sum
|
|
|
|
|
Thanks I think I understand!
The reason why is? When windows was created using GDI things were written in good old C!
Unlike C++, C didnt use pass references, so you had to use pass by address.
Also since pass by Value such as :
<br />
CClientDC dc(this);<br />
<br />
CPen penBlue = new CPen();<br />
penBlue.Create(PS_SOLID,1,RGB(0,0,255));<br />
<br />
dc.SelectObject(penBlue)
<br />
<br />
dc.SelectObject(&penBlue)
<br />
also you could use<br />
<br />
CPen& rPenBlue = &penBlue;<br />
dc.SelectObject(rPenBlue)
<br />
Thanks Tom
|
|
|
|
|
Tom Moore wrote: CClientDC dc(this);
CPen penBlue = new CPen();
penBlue.Create(PS_SOLID,1,RGB(0,0,255));
dc.SelectObject(penBlue) // pass by value, makes a copy - uses up a lot of memory (BAD)
Actually, this code will not even compile. CDC::SelectObject expects a CPen* pointer, so you have to pass by address.
You may be right
I may be crazy
But it just may be a lunatic you’re looking for
-- Billy Joel --
Within you lies the power for good - Use it!
|
|
|
|
|
I think I understand some of it now, but theres still some things that elude me.
An example is
<br />
CClient* pDC(this);<br />
CPen penRed = new CPen()<br />
<br />
penRed.Create(PS_SOLID,1,RGB(255,0,0);
<br />
CPen penOld = dc->SelectObject(&penRed);
<br />
dc.DrawText(300,400,penRed,"Hello World");
<br />
dc.SelectObject(penOld)
I still am a bit confused like the previous snippet
<br />
CRect rc;<br />
GetWindowRect(&rc);<br />
Thanks in advance again
Tom
|
|
|
|
|
CClient* pDC(this);
CPen *penRed = new CPen();
penRed->Create(PS_SOLID,1,RGB(255,0,0));
CPen *penOld = dc->SelectObject(penRed);
dc.SelectObject(penOld) I've marked up the code you posted. Notice the bold areas.
1. The new operator returns the address of an object allocated on the heap. The value on the left side of the assignment therefore has to be a pointer variable.
2. If you refer to a member of an object through a pointer, you have to use the -> operator, as in penRed->Create .
3. The SelectObject function in the CDC class takes a pointer to a CGdiObject .
4. The CDC class doesn't have a DrawText member that takes a pen argument.
Software Zen: delete this;
|
|
|
|
|
hihi,
u should learn C++ before u know GetWindowRect().
&rc is address of the rc.
rc is rc.
address means memory place in which the data of rc stored.
it sounds very dangerous u go so far in programming.
A nice tool for optimizing your Microsoft html-help contents.
Includeh10
|
|
|
|
|
Hi,
I do know C++!
I did a programming course on it, but I covered a small portion of address of operator in it, and we didnt cover address of operator with objects.
Thanks
Tom
|
|
|
|
|
Hi Tom Moore,
int* m_pNumber1; //this operator is pointer
int* m_pNumber2;//this operator is pointer
int m_Number1;//It's a integer
int m_Number2;//It's a integer
m_Number1=20;//Value is Integer
m_Number2=30;//Value is Integer
The Follow these statement Address of Operator m_Number1 and m_Number2
m_pNumber1=&m_Number1;//////////& is Address of Operator
m_pNumber2=&m_Number2;
////////////////////////Now
int iResult=m_Number1+ m_Number2;///->>>>>>>>Result=50
iResult=*m_pNumber1+ *m_pNumber2;///->>>>>>>>Result=50
|
|
|
|
|
Thanks to everyone!
I think I understand everything now
You use address of the object to access the originl object,
eg. dc.SelectObject(&penBlue); // Uses the original CPen
plus the Windows GDI libary is written in Pure C therefore it requires the address of the object.
Where as dc.SelectObject(penBlue); // Creates a copy and is slow.
I think GDI+ was written in either .NET or C++ instead of C and therefore is slightly simpler to use.
Thanks again
Tom
|
|
|
|
|
Tom Moore wrote: Whats the difference between :
GetWindowRect(&rc); and
GetWindowRect(rc);
The function declaration for GetWindowRect is CWnd::GetWindowRect(LPRECT lpRect) const; so it takes a pointer to a RECT structure as it's parameter. The reason why you can pass a CRect object, either as a pointer or by 'reference' (remember that the values of the window rectangle are copied into the CRect object, so you can not pass it in using the copy c'tor) is because the CRect class is derived from the RECT structure and has member operator LPRECT. If you pass it by address, GetWindowRect() assumes that you passed in the address of a RECT structure, or the base class, and it works from there. If you pass it by reference, then the CRect::operator LPRECT() kicks in to pass it's own address ( return this; ).
It's all in the magic of overloaded operators. So the difference between the two examples you gave is that there is really no difference functionally, although passing by address will save a few CPU cycles as it saves a call to the CRect::operator LPRECT
You may be right
I may be crazy
But it just may be a lunatic you’re looking for
-- Billy Joel --
Within you lies the power for good - Use it!
|
|
|
|
|
In C, variables can be passed to a function either by-value or by-reference. For example:
void foo( int num )
{
num = 12;
}
...
int x = 5;
printf("%x\n", x);
foo(x);
printf("%x\n", x);
void foo( int *num )
{
*num = 12;
}
...
int x = 5;
printf("%x\n", x);
foo(&x);
printf("%x\n", x); Another consideration is the amount of memory pushed onto the stack when one or the other is used. For integral types such as int , char , and double , there is little, if any, difference between the two. A char is 1 byte, a double is 8 bytes, and a pointer to either is 4 bytes. Where this matters more is when large data structures are being passed to a function. Imagine an object that is 1497 bytes in size. Passing the object by-value will result in 1497 bytes being piushed onto the stack, whereas passing the object by-reference will result in only 4 bytes being pushed onto the stack.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
Hi all,
I have some message boxes that are triggered from some buttons on a dialog. The dialog is set to open in full screen mode. The funny thing is that when the message boxes are triggered they get focus but they go behind my dialog.
Any idea why this is happening and how to get the message box on top of the dialog?
Thanks in advance.
|
|
|
|
|
MessageBox(....,HWND hWnd).
if hWnd is hidden, the message box is hidden also.
A nice tool for optimizing your Microsoft html-help contents.
Includeh10
|
|
|
|
|
I'm quite new to MFC programming ... how do i make it visible?
|
|
|
|
|
Set your dialog as its parent .
int MessageBox( HWND hWnd (Set it as your dialog handle),
LPCTSTR lpText,
LPCTSTR lpCaption,
UINT uType
);
Regards,
FarPointer
|
|
|
|
|
Sorry i'm unfamiliar with how to create or use a handle. Could you please show me how to?
Thanks in advance
|
|
|
|
|
Hi,
Please try this out:-
m_hWnd :- this is the handle of the dialog
MessageBox(m_hWnd,"AM I Working");
Regards,
FarPointer
|
|
|
|
|
If you are using MFC, you can do the following:
CMyDialog::OnButton()
{
::MessageBox(m_hWnd,
"Your message text goes here",
"Caption for your message box",
(MB_OK | MB_ICONINFORMATION));
} In this case, CMyDialog is the name of your dialog class, derived from CDialog (which is in turn derived from CWnd ). One of the members of all CWnd -derived objects is m_hWnd , which is the window handle.
Software Zen: delete this;
|
|
|
|
|
I've just tried m_Hwnd as follows:
<br />
::MessageBox(m_hWnd,"This is the LAST record","Last Record",MB_ICONINFORMATION | MB_SYSTEMMODAL);<br />
But its still not coming to the front. Could there be something else wrong?
|
|
|
|
|
Kinda off topic...
Don't use MB_SYSTEMMODAL unless you absolutely have to. It prevents the user from doing anything else on the computer other than interact with your message box. It's an exceptionally good way to cause extreme frustration and loss of confidence in your product...
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Thanks for the information ... i'll sure take it into account
|
|
|
|
|
braveheartkenya wrote: ::MessageBox(m_hWnd,"This is the LAST record","Last Record",MB_ICONINFORMATION | MB_SYSTEMMODAL);
You can try this one...
MessageBox(_T("This is the LAST record"), _T("Last Record"), MB_ICONINFORMATION | MB_SETFOREGROUND);
Since you are using MFC you don't have to use m_hWnd here. MFC does that for you.
Nibu thomas
Software Developer
|
|
|
|
|
It didn't work, but i got another that did.
Replace the MB_SETFOREGROUND with MB_DEFAULT_DESKTOP_ONLY.
I don't know what it means, but it brings it on top and freezes the dialog. Any idea how i can bring it to the front but not freeze the dialog?
|
|
|
|