Tag: C++ / MFC

  • Avoiding Memory Leaks using Boost Libraries

    Using boost::scoped_array

    When we want to dynamically allocate an array of objects for some purpose, the C++ programming language offers us the new and delete operators that are intended to replace the traditional malloc() and free() subroutines that are part of the standard library : (more…)

  • Using Template Classes to Handle Generic Data Types in STL Containers

    A code snippet with which utilizes both template classes and STL to handle generic data types. In keeping with the spirit of this blog, I have kept the explanation to a minimum and hopefully the code posted below should be self-explanatory.  The idea is for readers to get the gist of what I am saying so that they can go off and make up more relevant examples of their own. (more…)

  • Simple Multithreading in C++ / MFC

    Right now I am just starting with a very simple example – no fancy stuff – just a single thread to demonstrate a sequence that updates a counter 20 times per second. (more…)

  • Some neat examples of C++ String Formatting

    Some neat string formatting

    A pretty neat snippet found on stackoverflow some time ago (kudos David Rodriguez), that is worth repeating. Some time ago, a ‘moderator’ at StackOverflow removed this thread “for reasons of moderation”. Fair enough, but the result is that code that many would find pretty darn useful is no longer searchable on that site. How very constructive. Here is a snapshot of that particular StackOverflow page as preserved by the WayBack Machine:

    http://web.archive.org/web/20090420233428/http://stackoverflow.com/questions/469696/what-is-your-most-useful-c-c-snippet/470999

    I’m gonna start using this. Essentially it is a bit of in-house stream formatting. This class make_string produces an instance of an object derived from ostream, which has an implicit conversion to a std::string: (more…)

  • Sorting Objects Using STL

    Here’s an example of how using objects (hat-tip: Paul Wolfensberger)
    (more…)

  • Using Smart Pointers to Avoid Memory Leaks

    Using boost::scoped_array

    When we want to dynamically allocate an array of objects for some purpose, the C++ programming language offers us the new and delete operators that are intended to replace the traditional malloc() and free() subroutines that are part of the standard library :
    (more…)

  • User Defined Predicates

    Just some simple examples posted here as a means of easy lookup…
    (more…)

  • ‘Blitting’ a Bitmap Image to the Screen.

    I recently needed to revisit this to create simple monochrome bitmaps representing the sets of nozzles turned off/on on a Xaar microarrayer printhead.

    Sample code here.  Essential steps are outlined in the following code

    // 1. Create the uninitialized bitmap, that is compatible
    // with the the specified device context
    CPaintDC dc( this );
    CBitmap bitmap;
    bitmap.CreateCompatibleBitmap( &dc, 20, 20 );
    
    // 2. Create memory device context, that is compatible
    // with the specified device (dc in this case)
    CDC dcMem;
    dcMem.CreateCompatibleDC( &dc );
    
    // 3. Obtain/create the bitmap and select into
    // the memory device context.
    CBrush brushblue( RGB( 0, 0, 255 ) );
    CBrush brush( RGB( 0, 255, 255 ) );
    CBitmap* pOldBitmap = dcMem.SelectObject( &bitmap );
    dcMem.FillRect( CRect( 0, 0, 10, 10 ), &brushblue );
    dcMem.FillRect( CRect( 10, 0, 20, 10 ), &brush );
    dcMem.FillRect( CRect( 0, 10, 10, 20 ), &brush );
    dcMem.FillRect( CRect( 10, 10, 20, 20 ), &brushblue );
    
    // 4. Obtain the bitmap dimensions in pixels, making sure
    // they are then mapped into their corresponding logical
    // units
    BITMAP bm;
    bitmap.GetBitmap( &bm );
    CPoint size( bm.bmWidth, bm.bmHeight );
    CPoint org( 0, 0 );
    dc.DPtoLP( &size );
    dc.DPtoLP( &org );     
    
    // 5. Specify the memory device context’s mapping mode:
    // MM_ANISOTROPIC,  MM_HIENGLISH etc
    dcMem.SetMapMode( dc.GetMapMode() );
    
    // 6. ‘Blit’ the rectangular block of pixel data from
    // the source device context (dcMem) to the destination
    // device context (dc)
    dc.BitBlt( 12,
               12,
               size.x,
               size.y,
               &dcMem,
               org.x,
               org.y,
               SRCCOPY );
    
    // 7. Restore the default bitmap
    dcMem.SelectObject( pOldBitmap );
    

    A simple dialog application example to download:    Bitmaps.zip

  • Creating Bitmap Files from Raw Pixel Data in C++

    Creating the byte array

    This post describes a means of taking data in the form of raw pixels containing RGB values as well as the image height, width and the number of bits per pixel (24 in this case) and converting this into a bitmap (BMP) file.

    Example Visual Studio 2010 project is downloadable from here:

    www.technical-recipes.com/Downloads/BitmapRawPixels.zip

    In this this post I originally described how I needed a means of representing binary array values (inkjet printhead nozzles turned ON/OFF) as a set of colored ‘squares’ or pixels, so that nozzles switched ON/OFF could be represented by two different colours – black or white. The bitmap width was always going to be 128, while the bitmap length (“SizeValue”) was one of a set of discrete set of values in the range { 1000, 1250, 1251, 1350 }. For this example I did not have to worry about padding additional values (more…)

  • Multidimensional Arrays in C++

    Declaring and initializing multi-dimensional arrays in C++ can be done not just by way of traditional pointer arithmetic, but using the STL / Boost libraries as well.  Here are some examples:

    (more…)