Thursday, May 27, 2010
Visual Studio (VS) 2005 IDE for IronPython (Python) - Yang Li's blog - Site Home - MSDN Blogs
Sandcastle (software) - Wikipedia, the free encyclopedia
Thursday, May 20, 2010
Pretty printing boolean values in C++
By default boolean values (type bool, values true and false) are printed as 1 and 0 in C++ iostreams. There is a simple way to print them as their names (true/false). The following code-example demonstrates how to do that using boolalpha manipulator.
1: #include <iostream>2: using namespace std;3:4: int main()5: {6: // By default true prints as 1 and false prints as 07: cout << true << " " << false << endl;8: // We can enable boolalpha formatting flag9: cout << boolalpha;10: // Now true and false will be inserted as their names instead11: // of integral values12: cout << true << " " << false << endl;13: // We can later on unset boolalpha with noboolalpha format manipulator14: cout << noboolalpha << endl;15: cout << true << " " << false << endl;16: // The boolalpha flag is not set in standard streams on initialization17: return 0;18: }19:20:
The output of the program looks as follows:
1: 1 02: true false3: 1 0
We can notice that on the first line the output is integral. After setting boolalpha, the output becomes textual. Later on the output becomes integral again after setting noboolalpha.
Input streams
The manipulator can similarly be used on input streams for reading boolean values as true/false or 1/0.
Wednesday, May 19, 2010
Adding custom draw functionality to an MFC CButton
We will start by defining a subclass CBDButton to the MFC class CButton.
1: class CBDButton : public CButton2: {3: public:4: // overwrite void DrawItem function and draw your button5: void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);6: };7:
As you can notice above, we have overridden one function DrawItem in this class. Lets now look at its implementation.
1: void CBDButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)2: {3: // TODO: Add your code to draw the specified item4: UINT uStyle = DFCS_BUTTONPUSH;5:6: // This code only works with buttons.7: ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);8:9: // If drawing selected, add the pushed style to DrawFrameControl.10: if (lpDrawItemStruct->itemState & ODS_SELECTED)11: {12: uStyle |= DFCS_PUSHED;13: }14:15: CDC dc;16: dc.Attach(lpDrawItemStruct->hDC);17:18: // Draw the button frame.19: dc.DrawFrameControl(&lpDrawItemStruct->rcItem, DFC_BUTTON, uStyle);20:21: CRect rect = lpDrawItemStruct->rcItem;22: rect.left = rect.left + 1;23: rect.right = rect.right - 2;24: rect.top = rect.left + 1;25: rect.bottom = rect.bottom - 2;26: dc.FillSolidRect(rect, m_clrBk);27:28: // Get the button's text.29: CString strText;30: GetWindowText(strText);31: // Draw the button text using the text color.32: COLORREF crOldColor = dc.SetTextColor(m_crText);33: dc.DrawText(strText, strText.GetLength(), &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);34: dc.SetTextColor(crOldColor);35:36: dc.Detach();37: }
Wednesday, May 5, 2010
Testing windows live writer
Windows Live Writer is expected to help me write source code examples easily and post them to my blog seamlessly. In this post I am trying to test its functions.
Here is some C++ code:
1: class A
2: {3: public:4: A();5: ~A();6: void f();7: };8:9: // an example of implementation inheritance10: class B : private A11: {12: public:13: B();14: ~B();15: void g();16: };17:18:
Following is some Python code:
1: import os2: import sys3:4: def main():5: filepath = sys.argv[1]6: contents = open(filepath).readlines()7: print 'Number of lines: ' , len(contents)8:9: if __name__ == '__main__':10: main()
A Haskell code example:
1: double:: Integer -> Integer2: double x = x + x3:4: square :: Integer -> Integer5: square x = x * x6:7: main = do8: print $ square 109: print $ double 2010:11:
Some JavaScript:
1: fucntion f()2: {3: alert('Hello World');4: }5:
Some HTML:
1: <html>2: <head>3: <title>Hello World </title>4: </head>5: <body>6: <p> Don't Panic </p>7: </body>8: </html>9:
Some CSS:
1: .header2: {3: forground-color: black;4: background-color: red;5: text-size: 20;6: }7:
I guess this is more than enough for me. I am happy it works nicely.
Tuesday, May 4, 2010
Side effects and functional programming
- The function has some local variables which are lost after the function execution completes (no side effect)
- The function may compute a value which it returns (no side effect)
- If the arguments are references/pointers to some objects, the function may modify them (a side effect to the input arguments)
- The function may modify some global variable (a side effect)
- The function may output something on output or to a file or a socket (a side effect to the outside world with which the program is associated)
- The function may read something from input or from a file or a socket (a side effect to the outside world with which the program is associated)
- A member function may alter the state of the object with which it is associated (a side effect)