Tuesday, March 11, 2008

4 Steps to Recreate a Control (when there is no other workaround)

To recreate a Control you must:


1. Store rectangle and previous window in Z order (to keep Tab work normally)
2. Destroy Control by DestroyWindow;
3.Create Control by CreateWindow(); // put here your styles.
4. move window to proper Z order by:
SetWindowPos(&wndPrev, 0,0,0,0, SWP_NOMOVE_SWP_NOSIZE|SWP_NOZCTIVATE);

The second argument of the DYNAMIC_DOWNCAST macro cannot be const for the IBM platform

In Windows platform DYNAMIC_DOWNCAST macro can take const or non const pointer as second argument.

But the second argument of DYNAMIC_DOWNCAST macro cannot be const for the IBM platform.

How to become a good programmar

I’m putting some suggestions here for the fresh graduates and the college freshmen on how to become a good programmer.

In my own definition, a fresh graduate good/skilled programmer should have the followings:

  1. Strong skill of one or more good languages like C++, Java and C#.
    1. Must have strong skills with control structures. Don’t mess up if you’re asked to print out triangle or other shaped piles of ‘x’s with loops.
    2. Must have strong skills with recursion. You must know how to transform a looped task into a recursive one and vice versa, for example: multiplication using addition recursively.
    3. If your language is C++, you must know how to play with pointers and references.
    4. Understand pass by value and reference.
    5. Clearly understand scopes and memory allocation, de-allocation. Know when a object is destroyed and when to destroy.
    6. Know the usage of all operators including bit-wise ones.

  1. In-depth knowledge of OOP.
    1. Only being able to write classes and doing encapsulation and inheritance is not what you should call good OOP.
    2. Clearly understand how function overloading, overriding, polymorphism works.
    3. Clearly understand how constructor/destructor (if any) works with inheritance.
    4. Clearly know the difference and use of Interfaces and Abstract classes.
    5. Know how to overload operators. Why and how copy constructor is defined/used.

  1. Know common data structures
    1. At least know the common data structures like stack, queue, linked list, doubly linked list (know circular version of all of them) and trees.
    2. Be a skilled implementer of any of those, have clear concept of push, pop, add, delete, peek etc method works on those data structures.

  1. Know most common algorithms well
    1. You don’t need to memorize pseudo codes line by line but you need to have clear concept of most common algorithms of sorting(bubble, quick, merge, heap, bucket, etc), searching (including DFS, BFS), etc.
    2. As a fresher you must know their time and space complexities, pitfalls and improvements (if any).

  1. General computing concepts:
    1. Know processes and threads, how are they related to each other, how to program them, etc.
    2. Understand TCP/IP: Don’t think it’s only the network administrator’s task to understand TCP/IP. All programmers ever doing any network or web programming should have clear TCP/IP concepts and understanding.

  1. Be skilled in debugging in IDE:
    1. Be skilled in any or many of Visual Studio/Visual Studio.Net, Eclipse, Netbeans, KDevelop, etc.
    2. Know how to debug your code.

  1. Have basic knowledge of Software Engineering and SDLC.

Some advice for college freshmen:

  1. Start with C++ or Java, avoid starting with scripting languages:
    1. If you’re learning programming for the first time, avoid starting with scripting or loosely typed languages like: PHP, ASP, Perl, etc or Visual Basic. It may destroy your understanding of program execution, data types, memory allocation, etc.
    2. Start with C++ or Java. If you want to me to be specific, start with C++, you’ll love it for the rest of your life.. :) It’ll be easier for you to learn (almost) any other language (like: C#, PHP, ASP, etc).
    3. If you ask, do you need to know C to start with C++? Or should you learn C first and then C++? The answer is ‘no’.

  1. If you want to be good programmer, keep on coding at least 20 hours a week for next 4 years :).

  1. Never stop learning new technologies that are coming out everyday.

Know something’s of many things but be master of one. Know at least one language very well.

Replace Color of a bitmap with a new color

1. Fill the DC with the new color

2. Draw the bitmap transparently into the DC with the Old Color (the color to be replaced) as the mask color

Example Code :

BOOL CMFCBitmap::ReplaceColor(COLORREF crOld, COLORREF crNew)

{

CDC memDC;

CBitmap bmpTrans;

CBitmap *pOldBitmap;

POINT ptSize;

BOOL bResult;

// Create one DC to hold temporary data.

bResult = memDC.Attach(::CreateCompatibleDC(NULL));

if(bResult == FALSE)

return FALSE;

// Compute the bitmap dimension

if(!this->GetBitmapSize(&(ptSize.x), &(ptSize.y)))

return FALSE;

// Convert from device

memDC.DPtoLP(&ptSize, 1);

BITMAP bm;

GetObject(m_hBitmap, sizeof(BITMAP), (LPSTR)&bm);

CMFCBitmap bmp;

if(bmp.CopyBitmap(m_hBitmap, MFCBI_NORMAL) == FALSE)

return FALSE;

// Create the transparent bitmap

bmpTrans.Attach(this->Detach());

Attach(bmp.Detach());

// Select the bitmap

pOldBitmap = (CBitmap*)memDC.SelectObject(&bmpTrans);

CRect rc(0,0, ptSize.x, ptSize.y);

memDC.FillSolidRect(rc, crNew);

DrawTransparent(&memDC, 0,0, crOld);

memDC.SelectObject(pOldBitmap);

// Destroy the previous bitmap

DeleteObject(Detach());

HBITMAP hMaskedBitmap = (HBITMAP)bmpTrans.Detach();

// Attach to the transparent bitmap

if(!Attach(hMaskedBitmap))

{

DeleteObject((HGDIOBJ)hMaskedBitmap);

return FALSE;

}

return TRUE;

}

#DEFINE VS ENUM revisited

Tried to declare an enum inside a class like CEMCHotButton:

enum TextAlignment

{

TA_LEFT = 0

};

And there were 100 errors showing up without any traceable reason.

Finally I found that, a same DEFINE is declared in WINGDI.H header file and it’s made a conflict with my DEFINE value.

Now the problem is, define cannot be put under a namespace or any scope, therefore I have to change the enum type name.

This is forcing me to name TA_LEFT, to TF_LEFT.