Friday, July 24, 2009

How to Assert on Object Slicing?

If derived object is assigned to Base object, then the derived object will be sliced off and only the base part will be copied. Indeed it will cause abnormalities. But is there any mechanism, atleast to assert while object slicing?
You can do it by adding an overloaded constructor for derived in Base class and then assert in it. For instance,

// Forward Declaration.
class Derived;

// Base class.
class Base
{
public:
// Default Constructor.
Base() {}
Base( Derived& derived ) { ASSERT( FALSE ); }
};

// Derived class.
class Derived
{
};
...

// Test code.
Base ObjBase;
Derived ObjDerived;

ObjBase = ObjDerived;

Read more...

Thursday, July 23, 2009

How to Pass Array by Reference?

Receiving arrays by reference have special syntax. The arrayname and & symbol should be enclosed in parenthesis. And you should specify the size of array. Have a look at the following code snippet.

// Receive Array by reference.
void GetArray( int (&Array) [10] )
{
}

// Test array by reference.
void CRabbitDlgDlg::TestArray()
{
// Pass array by reference.
int Array[10] = { 0 };
GetArray( Array );
}

Read more...

How to get the CPU Name String?

You can use the function – __cpuid(), which generates the instruction – cpuid. Have a look at the code snippet. Code taken and modified from MSDN.

#include 
#include

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
// Get extended ids.
int CPUInfo[4] = {-1};
__cpuid(CPUInfo, 0x80000000);
unsigned int nExIds = CPUInfo[0];

// Get the information associated with each extended ID.
char CPUBrandString[0x40] = { 0 };
for( unsigned int i=0x80000000; i<=nExIds; ++i)
{
__cpuid(CPUInfo, i);

// Interpret CPU brand string and cache information.
if (i == 0x80000002)
{
memcpy( CPUBrandString,
CPUInfo,
sizeof(CPUInfo));
}
else if( i == 0x80000003 )
{
memcpy( CPUBrandString + 16,
CPUInfo,
sizeof(CPUInfo));
}
else if( i == 0x80000004 )
{
memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
}
}

cout << "Cpu String: " << CPUBrandString;
}

Read more...

How to set timer in mfc dialog?

For creating timers you can use the api – SetTimer(). In SetTimer() you’ve to specify the time interval in milliseconds, a timer id as UINT, then TimerProc. For each tick of specified timer, windows will call the TimerProc. If you didn’t specify any TimerProc, then a WM_TIMER message will be posted to your window.

You can utilize the Timer ID to set multiple timers using the same TimeProc. You can use KillTimer() to remove the timer. Have a look at the code snippet. In the given code snippet, timer is handled by WM_TIMER message.

// Message Map
BEGIN_MESSAGE_MAP(CDlgDlg, CDialog)
...
ON_WM_TIMER()
END_MESSAGE_MAP()

...

// Timer ID constants.
const UINT ID_TIMER_MINUTE = 0x1001;
const UINT ID_TIMER_SECONDS = 0x1000;

// Start the timers.
void CDlgDlg::StartTimer()
{
// Set timer for Minutes.
SetTimer( ID_TIMER_MINUTE, 60 * 1000, 0 );

// Set timer for Seconds.
SetTimer( ID_TIMER_SECONDS, 1000, 0 );
}

// Stop the timers.
void CDlgDlg::StopTimer()
{
// Stop both timers.
KillTimer( ID_TIMER_MINUTE );
KillTimer( ID_TIMER_SECONDS );
}

// Timer Handler.
void CDlgDlg::OnTimer( UINT nIDEvent )
{
// Per minute timer ticked.
if( nIDEvent == ID_TIMER_MINUTE )
{
// Do your minute based tasks here.
}

// Per minute timer ticked.
if( nIDEvent == ID_TIMER_SECONDS )
{
// Do your seconds based tasks here.
}
}

Read more...

How to disable maximizing the dialog from Task manager?

You can do it by handling – WM_GETMINMAXINFO message. Before resizing, this message is fired to the dialog to get the minimum and maximum window dimensions. Since, we don’t need to change our dimensions, we have to set the max dimensions as current window dimension. Have a look at the code snippet.

// Message map
BEGIN_MESSAGE_MAP(CRabbitDlg, CDialog)
...
ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()

void CRabbitDlg::OnGetMinMaxInfo( MINMAXINFO FAR* pMinMaxInfo )
{
// Window rect.
RECT rect = { 0 };
GetWindowRect( &rect );
CRect WindowRect( &rect );

// Set the maximum size. Used while maximizing.
pMinMaxInfo->ptMaxSize.x = WindowRect.Width();
pMinMaxInfo->ptMaxSize.y = WindowRect.Height();

// Set the x,y position after maximized.
pMinMaxInfo->ptMaxPosition.x = rect.left;
pMinMaxInfo->ptMaxPosition.y = rect.top;
}



Read more...

How to set Transparent Dialogs?

The secret is Layered windows. For that you’ve to enable WS_EX_LAYERED style and set the alpha of dialog by calling SetLayeredWindowAttributes(). See the code snippet below.

// Enable WS_EX_LAYERED window extended style.
LONG ExtendedStyle = GetWindowLong( GetSafeHwnd(),
GWL_EXSTYLE );
SetWindowLong( GetSafeHwnd(),
GWL_EXSTYLE,
ExtendedStyle | WS_EX_LAYERED );

// Select the transparency percentage.
// The alpha will be calculated accordingly.
double TransparencyPercentage = 50.0;

// Set the alpha for transparency.
// 0 is transparent and 255 is opaque.
double fAlpha = TransparencyPercentage * ( 255.0 /100 );
BYTE byAlpha = static_cast( fAlpha );
SetLayeredWindowAttributes( GetSafeHwnd(),
0,
byAlpha,
LWA_ALPHA );

Layered windows are available from Windows 2000 onwards. So don’t forget to add
_WIN32_WINNT=0×0500
to project settings for preparing the dialog invisible portion.


Read more...

How to convert CString to char* or LPTSTR?

Well, you can use CString::GetBuffer() to access the internal buffer of CString. But one thing to take care is that – you should release the buffer by calling CString::ReleaseBuffer() after use. Check the code snippet below,

// Our CString object.
CString String = "HelloWorld";

// Get the internal buffer pointer of CString.
LPTSTR pString = String.GetBuffer( 0 );
...

// Use the pString and then release it.
String.ReleaseBuffer();

Read more...

  © Free Blogger Templates Blogger Theme II by Ourblogtemplates.com 2008

Back to TOP