473,287 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

How to add text to a bitmap, then show that bitmap with visible text and a transparent background?

SwissProgrammer
220 128KB
I am using C and C++, whichever works. Prefer Win32 if that works. CodeBlocks. No dot net. No visual studio. No MFC.

I tried writing this on my own, but it is beyond me.

My latest attempt has been the following:


I have been studying DrawText function from microsoft.com to write text to a bitmap.

Then I tried using similar to from stackoverflow.com but I did not get that to work. My compiler chokes on that. I tried studying that and fixing it, but my many attempts did not work.

Also, I tried adjusting this another from codeproject.com from whatever that is written in. I think it is MFC or cling-on or confused-us or something. My compiler pukes out almost every line of that.

Now I have this:

Expand|Select|Wrap|Line Numbers
  1. HBITMAP hbitmap = hMainWindowBitmapForRegion;
  2. //        MAKEINTRESOURCE(IDB_BITMAP1));
  3.  
  4. //BITMAP bm;
  5. GetObject( hbitmap, sizeof(BITMAP), &bm );
  6. long width=bm.bmWidth;
  7. long height=bm.bmHeight;
  8.  
  9.  
  10. //prepare the bitmap attributes
  11. BITMAPINFO bmInfo;
  12. memset(&bmInfo.bmiHeader,0,sizeof(BITMAPINFOHEADER));
  13. bmInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
  14. bmInfo.bmiHeader.biWidth=width;
  15. bmInfo.bmiHeader.biHeight=height;
  16. bmInfo.bmiHeader.biPlanes=1;
  17. bmInfo.bmiHeader.biBitCount=24;
  18. //create a temporary dc in memory.
  19. HDC pDC = ::GetDC(0);
  20. HDC TmpDC=CreateCompatibleDC(pDC);
  21. //create a new bitmap and select it in the memory dc
  22. BYTE *pbase;
  23. HBITMAP TmpBmp=CreateDIBSection(pDC,
  24.     &bmInfo,DIB_RGB_COLORS,(void**)&pbase,0,0);
  25. HGDIOBJ TmpObj=SelectObject(TmpDC,TmpBmp);
  26.  
  27.  
  28. //draw the background
  29. HDC dcBmp=CreateCompatibleDC(TmpDC);
  30. HGDIOBJ TmpObj2 = SelectObject(dcBmp,hbitmap);
  31. BitBlt(TmpDC,0,0,width,height,dcBmp,0,0,SRCCOPY);
  32. SelectObject(TmpDC,TmpObj2);
  33. DeleteDC(dcBmp);
  34.  
  35. //choose the font
  36. //CFont m_Font;
  37. HFONT m_Font;   // = (HFONT)lParam;
  38.  
  39. LOGFONT* m_pLF;
  40. m_pLF=(LOGFONT*)calloc(1,sizeof(LOGFONT));
  41.  
  42. HFONT hFont = CreateFont (13, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
  43.       OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  44.       DEFAULT_PITCH | FF_DONTCARE, TEXT("Tahoma"));
  45.  
  46.  
  47.  
  48. //Set text position;
  49. RECT pos = {40,40,50,50};
  50.  
  51. //draw the text
  52. HDC dcBmp2=CreateCompatibleDC(TmpDC);
  53.  
  54.     SetTextColor(dcBmp2,RGB(0,255,0));
  55.     SetBkMode(dcBmp2, TRANSPARENT);
  56.     SetBkColor(dcBmp2,RGB(0,0,255));
  57.  
  58. //SetBkMode(TRANSPARENT);
  59. DrawTextW(dcBmp2,(LPCWSTR)"TestTestTestTestTestTestTestTestTestTestTest",4,&pos,DT_CALCRECT);
  60. DrawTextW(dcBmp2,(LPCWSTR)"TestTestTestTestTestTestTestTestTestTestTest",4,&pos,0);
  61.  
  62. //cleanup
  63. free(m_pLF);
  64.  
  65. DeleteDC(dcBmp2);
It does not work.

I tried various adjustments. I studied what the debugger said. I am getting overwhelmed by this. Would you please help me to get this to work?

Thank you.



In case you want more to see what I have: This is my entire program. It currently shows some red text on a bitmap that I have used to create a custom region. That works. But, I want to be able to use that and bitblt the blank bmp background out of it and just leave the text for my program to show. My intent is to have just the text showing with a transparent background. I want to make this dynamically. NOT in some image editor. I want to be able to show a changing text. That is where I am going with this. Help! Please!

Expand|Select|Wrap|Line Numbers
  1. // BitmapToCustomShapedRegion.cpp
  2.  
  3.  
  4. #define _UNICODE
  5. #define UNICODE
  6.  
  7. #define STRICT      // Require more care in declaring and using types.
  8.  
  9. #include <windows.h>
  10. #include <string>       // for std::char_traits
  11. //#include <iostream>     // std::cout
  12. #include <algorithm>    // std::min
  13.  
  14. // Globals
  15.     TCHAR szClassName[] = L"WIN32TEST";
  16.  
  17.         HWND Handle_of_MainWindow;
  18.         HDC HDC_of_MainWindow;
  19.  
  20.         HINSTANCE MainWindow_hInstance;
  21.  
  22. // Function prototypes with default arguments
  23.     HBITMAP hMainWindowBitmapForRegion;
  24.  
  25.     BITMAPINFOHEADER RGB32BITSBITMAPINFO;
  26.  
  27.  
  28. #include "resource.h"
  29. #include "bitmap_to_region.h"
  30. #include "callback_winproc_main.h"
  31.  
  32. //#include "ShObjIdl.h"
  33.  
  34.  
  35. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  36.     {
  37.         // Parameters being set but maybe not being used
  38.             HINSTANCE hNotUsing001;
  39.                 hNotUsing001 = hPrevInstance;
  40.                 hPrevInstance = hNotUsing001;
  41.  
  42.             LPSTR lpNotUsing002;
  43.                 lpNotUsing002 = lpCmdLine;
  44.                 lpCmdLine = lpNotUsing002;
  45.  
  46.         // Parameters being set and being used
  47.             WNDCLASS wc;
  48.             HWND hWnd;
  49.             MSG msg;
  50.  
  51.         // HBITMAP hMainWindowBitmapForRegion;
  52.         hMainWindowBitmapForRegion = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
  53.  
  54. HWND HWND_of_TestBitmap_001= (HWND)hMainWindowBitmapForRegion;
  55.  
  56.         if (hMainWindowBitmapForRegion)
  57.             {
  58.                 ZeroMemory(&wc, sizeof(wc));
  59.                 wc.style         = CS_HREDRAW | CS_VREDRAW;
  60.                 wc.lpszClassName = szClassName;
  61.                 wc.lpfnWndProc   = (WNDPROC)WndProc_for_main;
  62.                 wc.hInstance     = hInstance;
  63.  
  64.                 if (RegisterClass(&wc))
  65.                     {
  66.                         BITMAP bm;
  67.                         GetObject(hMainWindowBitmapForRegion, sizeof(bm), &bm);
  68.  
  69.                         // For Agents
  70.                         //hWnd = CreateWindow(szClassName, NULL, WS_POPUP, 100, 10, bm.bmWidth, bm.bmHeight, NULL, NULL, hInstance, NULL);
  71.  
  72.                         // For Anti-Agents
  73.                         hWnd = CreateWindow(szClassName, NULL, WS_POPUP, 650, 10, bm.bmWidth, bm.bmHeight, NULL, NULL, hInstance, NULL);
  74.  
  75.  
  76.  
  77.  
  78. /// test..........START
  79. HBITMAP hbitmap = hMainWindowBitmapForRegion;
  80.  
  81. //BITMAP bm;
  82. GetObject( hbitmap, sizeof(BITMAP), &bm );
  83. long width=bm.bmWidth;
  84. long height=bm.bmHeight;
  85.  
  86.  
  87. //prepare the bitmap attributes
  88. BITMAPINFO bmInfo;
  89. memset(&bmInfo.bmiHeader,0,sizeof(BITMAPINFOHEADER));
  90. bmInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
  91. bmInfo.bmiHeader.biWidth=width;
  92. bmInfo.bmiHeader.biHeight=height;
  93. bmInfo.bmiHeader.biPlanes=1;
  94. bmInfo.bmiHeader.biBitCount=24;
  95. //create a temporary dc in memory.
  96. HDC pDC = ::GetDC(0);
  97. HDC TmpDC=CreateCompatibleDC(pDC);
  98. //create a new bitmap and select it in the memory dc
  99. BYTE *pbase;
  100. HBITMAP TmpBmp=CreateDIBSection(pDC,
  101.     &bmInfo,DIB_RGB_COLORS,(void**)&pbase,0,0);
  102. HGDIOBJ TmpObj=SelectObject(TmpDC,TmpBmp);
  103.  
  104.  
  105. //draw the background
  106. HDC dcBmp=CreateCompatibleDC(TmpDC);
  107. HGDIOBJ TmpObj2 = SelectObject(dcBmp,hbitmap);
  108. BitBlt(TmpDC,0,0,width,height,dcBmp,0,0,SRCCOPY);
  109. SelectObject(TmpDC,TmpObj2);
  110. DeleteDC(dcBmp);
  111.  
  112. //choose the font
  113. HFONT m_Font;
  114.  
  115. LOGFONT* m_pLF;
  116. m_pLF=(LOGFONT*)calloc(1,sizeof(LOGFONT));
  117.  
  118. HFONT hFont = CreateFont (13, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
  119.       OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  120.       DEFAULT_PITCH | FF_DONTCARE, TEXT("Tahoma"));
  121.  
  122.  
  123. //Set text position;
  124. RECT pos = {40,40,50,50};
  125.  
  126. //draw the text
  127. HDC dcBmp2=CreateCompatibleDC(TmpDC);
  128.  
  129.     SetTextColor(dcBmp2,RGB(0,255,0));
  130.     SetBkMode(dcBmp2, TRANSPARENT);
  131.     SetBkColor(dcBmp2,RGB(0,0,255));
  132.  
  133. //SetBkMode(TRANSPARENT);
  134. DrawTextW(dcBmp2,(LPCWSTR)"TestTestTestTestTestTestTestTestTestTestTest",4,&pos,DT_CALCRECT);
  135. DrawTextW(dcBmp2,(LPCWSTR)"TestTestTestTestTestTestTestTestTestTestTest",4,&pos,0);
  136.  
  137. //cleanup
  138. free(m_pLF);
  139.  
  140. DeleteDC(dcBmp2);
  141.  
  142. /// test..........END
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.                         if (hWnd)
  159.                             {
  160.                                 Handle_of_MainWindow = hWnd;  // Using this for closing the program.
  161.  
  162.                                 HDC_of_MainWindow = GetDC( Handle_of_MainWindow );
  163.  
  164.                                 MainWindow_hInstance = hInstance;
  165.  
  166.                                 HRGN hBtRgn = BitmapToRegion(hMainWindowBitmapForRegion);
  167.  
  168.                                 if (hBtRgn)
  169.                                     {
  170.                                         SetWindowRgn(hWnd, hBtRgn, TRUE);
  171.                                     }
  172.  
  173.  
  174.                                 ShowWindow(hWnd, nCmdShow);
  175.                                 UpdateWindow(hWnd);
  176.  
  177.  
  178.                                 // Main message loop:
  179.  
  180.                                 while (GetMessage(&msg, nullptr, 0, 0))
  181.                                     {
  182.                                         TranslateMessage(&msg);
  183.                                         DispatchMessage(&msg);
  184.                                     }
  185.  
  186.                                 return (msg.wParam);
  187.                             }
  188.                     }
  189.             }
  190.  
  191.         return 0;       // successfully executed.
  192.         // The return value is the exit code of your program, the shell
  193.         //    (or any other application that ran it) can read and use it.
  194.         // nonzero indicates a failure of some kind.
  195.         //     return 1;    // executed but some error may have occured.
  196.         //     return -1;   // unexpected error may have occured.
  197.     }
  198.  

And then:


Expand|Select|Wrap|Line Numbers
  1. // bitmap_to_region.h
  2.  
  3. //
  4. //    BitmapToRegion :    Create a region from the "non-transparent" pixels of a bitmap
  5. //    Author :            Jean-Edouard Lachand-Robert (http://www.geocities.com/Paris/LeftBank/1160/resume.htm), June 1998.
  6. //
  7. //    hMainWindowBitmapForRegion :    Handle of a source bitmap
  8. //    cTransparentColor : Color base for the "transparent" pixels (default is black)
  9. //    cTolerance :        Color tolerance for the "transparent" pixels.
  10. //
  11. //    A pixel is assumed to be transparent if the value of each of its 3 components (blue, green and red) is
  12. //    greater or equal to the corresponding value in cTransparentColor and is lower or equal to the
  13. //    corresponding value in cTransparentColor + cTolerance.
  14. //
  15.  
  16. COLORREF cTransparentColor = RGB(254, 243, 222);   // Used as Blue Green Red
  17. COLORREF cTolerance = 0x000000;
  18.  
  19. // Previous declaration
  20. HRGN BitmapToRegion (HBITMAP hReceivedBitmap);
  21.  
  22. HRGN BitmapToRegion (HBITMAP hReceivedBitmap)
  23.     {
  24.         HRGN hRgn = NULL;
  25.  
  26.         if (hReceivedBitmap)
  27.             {
  28.  
  29.  
  30.                 // Create a memory DC inside which we will scan the bitmap content
  31.                 HDC hMemDC = CreateCompatibleDC(nullptr);
  32.  
  33.                 if (hMemDC)
  34.                     {
  35.                         // Get bitmap size
  36.                         BITMAP bm;
  37.                         GetObject(hReceivedBitmap, sizeof(bm), &bm);
  38.  
  39.                         // Create a 32 bits depth bitmap and select it into the memory DC
  40.                         //BITMAPINFOHEADER RGB32BITSBITMAPINFO;
  41.                         RGB32BITSBITMAPINFO = {
  42.                                 sizeof(BITMAPINFOHEADER),    // biSize
  43.                                 bm.bmWidth,                    // biWidth;
  44.                                 bm.bmHeight,                // biHeight;
  45.                                 1,                            // biPlanes;
  46.                                 32,                            // biBitCount
  47.                                 BI_RGB,                        // biCompression;
  48.                                 0,                            // biSizeImage;
  49.                                 0,                            // biXPelsPerMeter;
  50.                                 0,                            // biYPelsPerMeter;
  51.                                 0,                            // biClrUsed;
  52.                                 0                            // biClrImportant;
  53.                         };
  54.  
  55.             //            VOID * pbits32;
  56.             //            HBITMAP hbm32 = CreateDIBSection(hMemDC, (BITMAPINFO *)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, &pbits32, nullptr, 0);
  57.  
  58.                         HBITMAP hbm32 = CreateDIBSection(hMemDC, (BITMAPINFO *)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, nullptr, nullptr, 0);
  59.  
  60.  
  61.  
  62.  
  63.  
  64.                         if (hbm32)
  65.                             {
  66.                                 HBITMAP holdBmp = (HBITMAP)SelectObject(hMemDC, hbm32);
  67.  
  68.  
  69.  
  70.  
  71.                                 // Create a DC just to copy the bitmap into the memory DC
  72.                                 HDC hDC = CreateCompatibleDC(hMemDC);
  73.  
  74.  
  75.                                 if (hDC)
  76.                                     {
  77.                                         // Get how many bytes per row we have for the bitmap bits (rounded up to 32 bits)
  78.                                         BITMAP bm32;
  79.                                         GetObject(hbm32, sizeof(bm32), &bm32);
  80.                                         while (bm32.bmWidthBytes % 4)
  81.                                             bm32.bmWidthBytes++;
  82.  
  83.                                         // Copy the bitmap into the memory DC
  84.                                         holdBmp = (HBITMAP)SelectObject(hDC, hReceivedBitmap);
  85.  
  86.                                         BitBlt(hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY);
  87.  
  88.  
  89.  
  90.                                         // For better performances, we will use the ExtCreateRegion() function to create the
  91.                                         // region. This function take a RGNDATA structure on entry. We will add rectangles by
  92.                                         // amount of ALLOC_UNIT number in this structure.
  93.                                         #define ALLOC_UNIT    1000
  94.                                         DWORD maxRects = ALLOC_UNIT;
  95.                                         HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects));
  96.                                         RGNDATA *pData = (RGNDATA *)GlobalLock(hData);
  97.                                         pData->rdh.dwSize = sizeof(RGNDATAHEADER);
  98.                                         pData->rdh.iType = RDH_RECTANGLES;
  99.                                         pData->rdh.nCount = pData->rdh.nRgnSize = 0;
  100.                                         SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
  101.  
  102.                                         // Keep on hand highest and lowest values for the "transparent" pixels
  103.                                         BYTE lr = GetRValue(cTransparentColor);
  104.                                         BYTE lg = GetGValue(cTransparentColor);
  105.                                         BYTE lb = GetBValue(cTransparentColor);
  106.                                         BYTE hr = std::min(0xff, lr + GetRValue(cTolerance));
  107.                                         BYTE hg = std::min(0xff, lg + GetGValue(cTolerance));
  108.                                         BYTE hb = std::min(0xff, lb + GetBValue(cTolerance));
  109.  
  110.                                         // Scan each bitmap row from bottom to top (the bitmap is inverted vertically)
  111.                                         BYTE *p32 = (BYTE *)bm32.bmBits + (bm32.bmHeight - 1) * bm32.bmWidthBytes;
  112.  
  113.                                         for (int y = 0; y < bm.bmHeight; y++)
  114.                                             {
  115.                                                 // Scan each bitmap pixel from left to right
  116.  
  117.                                                 for (int x = 0; x < bm.bmWidth; x++)
  118.                                                     {
  119.                                                         // Search for a continuous range of "non transparent pixels"
  120.                                                         int x0 = x;
  121.                                                         LONG *p = (LONG *)p32 + x;
  122.  
  123.                                                         while (x < bm.bmWidth)
  124.                                                             {
  125.                                                                 BYTE b = GetRValue(*p);
  126.  
  127.                                                                 if (b >= lr && b <= hr)
  128.                                                                     {
  129.                                                                         b = GetGValue(*p);
  130.  
  131.                                                                         if (b >= lg && b <= hg)
  132.                                                                             {
  133.                                                                                 b = GetBValue(*p);
  134.  
  135.                                                                                 if (b >= lb && b <= hb)
  136.                                                                                     {
  137.                                                                                         // This pixel is "transparent"
  138.                                                                                         break;
  139.                                                                                     }
  140.                                                                             }
  141.                                                                     }
  142.                                                                 p++;
  143.                                                                 x++;
  144.                                                             }
  145.  
  146.                                                         if (x > x0)
  147.                                                             {
  148.                                                                 // Add the pixels (x0, y) to (x, y+1) as a new rectangle in the region
  149.                                                                 if (pData->rdh.nCount >= maxRects)
  150.                                                                     {
  151.                                                                         GlobalUnlock(hData);
  152.                                                                         maxRects += ALLOC_UNIT;
  153.                                                                         hData = GlobalReAlloc(hData, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), GMEM_MOVEABLE);
  154.                                                                         pData = (RGNDATA *)GlobalLock(hData);
  155.                                                                     }
  156.  
  157.                                                                 RECT *pr = (RECT *)&pData->Buffer;
  158.                                                                 SetRect(&pr[pData->rdh.nCount], x0, y, x, y+1);
  159.  
  160.                                                                 if (x0 < pData->rdh.rcBound.left)
  161.                                                                     {
  162.                                                                         pData->rdh.rcBound.left = x0;
  163.                                                                     }
  164.  
  165.                                                                 if (y < pData->rdh.rcBound.top)
  166.                                                                     {
  167.                                                                         pData->rdh.rcBound.top = y;
  168.                                                                     }
  169.  
  170.  
  171.                                                                 if (x > pData->rdh.rcBound.right)
  172.                                                                     {
  173.                                                                         pData->rdh.rcBound.right = x;
  174.                                                                     }
  175.  
  176.  
  177.                                                                 if (y+1 > pData->rdh.rcBound.bottom)
  178.                                                                     {
  179.                                                                         pData->rdh.rcBound.bottom = y+1;
  180.                                                                     }
  181.  
  182.  
  183.                                                                 pData->rdh.nCount++;
  184.  
  185.                                                                 // On Windows 98, ExtCreateRegion() may fail if the number of rectangles is too
  186.                                                                 // large (ie: > 4000). Therefore, we have to create the region by multiple steps.
  187.  
  188.                                                                 if (pData->rdh.nCount == 2000)
  189.                                                                     {
  190.                                                                         HRGN h = ExtCreateRegion(nullptr, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
  191.  
  192.                                                                         if (hRgn)
  193.                                                                             {
  194.                                                                                 CombineRgn(hRgn, hRgn, h, RGN_OR);
  195.                                                                                 DeleteObject(h);
  196.                                                                             }
  197.                                                                         else
  198.                                                                             {
  199.  
  200.                                                                                 hRgn = h;
  201.                                                                                 pData->rdh.nCount = 0;
  202.                                                                                 SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
  203.                                                                             }
  204.                                                                     }
  205.                                                             }
  206.                                                     }
  207.  
  208.                                                 // Go to next row (remember, the bitmap is inverted vertically)
  209.                                                 p32 -= bm32.bmWidthBytes;
  210.                                             }
  211.  
  212.                                         // Create or extend the region with the remaining rectangles
  213.                                         HRGN h = ExtCreateRegion(nullptr, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
  214.  
  215.  
  216.                                         if (hRgn)
  217.                                             {
  218.                                                 CombineRgn(hRgn, hRgn, h, RGN_OR);
  219.                                                 DeleteObject(h);
  220.                                             }
  221.                                         else
  222.                                             {
  223.                                                 hRgn = h;
  224.                                             }
  225.  
  226.  
  227.                                         // Clean up
  228.                                         SelectObject(hDC, holdBmp);
  229.                                         DeleteDC(hDC);
  230.                                     }
  231.  
  232.                                 DeleteObject(SelectObject(hMemDC, holdBmp));
  233.                             }
  234.  
  235.                         DeleteDC(hMemDC);
  236.                     }
  237.             }
  238.  
  239.         return hRgn;
  240.     }
  241.  


And then more:



Expand|Select|Wrap|Line Numbers
  1. // callback_winproc_main.h
  2.  
  3. ///
  4.  
  5.  
  6. // Previous declaration
  7. LRESULT CALLBACK WndProc_for_main(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  8.  
  9. LRESULT CALLBACK WndProc_for_main(
  10.                          HWND hWnd,     // A handle to the window procedure to receive the message.
  11.                          UINT message,  // The message.
  12.                          WPARAM wParam, // Additional message-specific information. The contents of this parameter depend on the value of the Msg parameter.
  13.                          LPARAM lParam  // Additional message-specific information. The contents of this parameter depend on the value of the Msg parameter.
  14.                          )
  15.                           // The return value specifies the result of the message processing and depends on the message sent.
  16.     // WNDPROC is either the address of a window or dialog box procedure,
  17.     //    or a special internal value meaningful only to CallWindowProc.
  18.     {
  19.         PAINTSTRUCT ps;
  20.         HDC hdc;
  21.  
  22.         switch (message)
  23.             {
  24.  
  25.                 case WM_NCHITTEST:
  26.                     {
  27.                         return HTCAPTION;
  28.                     }
  29.  
  30.                 case WM_LBUTTONDOWN:
  31.                     {
  32.                         break;
  33.                     }
  34.  
  35.                 case WM_PAINT:
  36.                     {
  37.                         hdc = BeginPaint (hWnd, &ps);
  38.  
  39.                         if (hMainWindowBitmapForRegion)
  40.                             {
  41.                                 BITMAP bm;
  42.                                 GetObject(hMainWindowBitmapForRegion, sizeof(bm), &bm);
  43.  
  44.                                 HDC memdc = CreateCompatibleDC(nullptr);
  45.                                 HBITMAP h = (HBITMAP)SelectObject(memdc, hMainWindowBitmapForRegion);
  46.  
  47.                                 BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, memdc, 0, 0, SRCCOPY);
  48.  
  49.                                 SelectObject(memdc, h);
  50.                             }
  51.  
  52.  
  53. ///     THIS WORKS ! 
  54.  
  55. /// Text to the bitmap   START
  56.  
  57. HDC hMemDC = CreateCompatibleDC(nullptr);
  58.  
  59. HBITMAP hbm32 = CreateDIBSection(hMemDC, (BITMAPINFO *)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, nullptr, nullptr, 0);
  60.  
  61. HBITMAP holdBmp = (HBITMAP)SelectObject(hMemDC, hbm32);
  62.  
  63. SelectObject(hdc, (HGDIOBJ) holdBmp);
  64.  
  65.     HINSTANCE hInstance_of_TestBitmap_001 = GetModuleHandleW(L"Anti-Agent_Belinda.bmp");
  66.  
  67.     LPCWSTR SomeLText = L"1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z";
  68.  
  69.     int LengthOfSomeLText = std::char_traits<wchar_t>::length (SomeLText);
  70.  
  71.     SetTextColor(hdc,RGB(255,0,0));
  72.     SetBkMode(hdc, TRANSPARENT);
  73.     SetBkColor(hdc,RGB(0,255,0));
  74.  
  75.  
  76.     for (int i = 0; i < 10; i++)
  77.         {
  78.             TextOutW ( hdc, 10, (i * 15), SomeLText, LengthOfSomeLText );
  79.         }
  80.  
  81. /// Text to the bitmap   END
  82.  
  83.  
  84.                         EndPaint (hWnd, &ps);
  85.                         break;
  86.                     }
  87.  
  88.                 case WM_KEYDOWN:
  89.                     {
  90.                         if (wParam != VK_ESCAPE)
  91.                             {
  92.                                 break;
  93.                             }
  94.                     }
  95.  
  96.                 case WM_DESTROY:
  97.                     {
  98.                         PostQuitMessage(0);
  99.                         break;
  100.                     }
  101.  
  102.                 default:
  103.                     {
  104.                         return (DefWindowProc(hWnd, message, wParam, lParam));
  105.                     }
  106.  
  107.             }
  108.  
  109.         return (0);
  110.     }
  111.  
  112.  
  113.  


Also:


Expand|Select|Wrap|Line Numbers
  1. // resource.h
  2.  
  3. #define IDB_BITMAP1                     101
  4.  
  5.  


And last:


Expand|Select|Wrap|Line Numbers
  1. // resource.rc
  2.  
  3. #include "resource.h"
  4.  
  5. // The DISCARDABLE keyword is ignored for 32-bit Windows, but remains for compatibility.
  6.  
  7. IDB_BITMAP1             BITMAP     "Blank_single_color.bmp"   // To have a background that I can remove later.
  8.  
  9.  
  10. #define SUBLANG_NEUTRAL    0x00    // This was in winnt.h which seems to be called by afxres.h
  11.                                 // I do not need all of that shit for just this one line.
  12.                                 // I am getting tired of stripping vast amounts of Visual Studio
  13.                                 // crap from examples just to get to the line or lines that I need.
  14.  
  15. LANGUAGE 0, SUBLANG_NEUTRAL
  16.  
  17.  

Oh, the picture that I am working on: It is local and I did not get it to upload to here. Just pick a bmp and use it yourself.
Sep 30 '22 #1
0 8591

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: chris | last post by:
please be gentle im a real beginner at vb6 howw do i show something on the screen without it being in a box, for example some text with a transparent background so you can see whats behind it ...
3
by: shaqattack1992-newsgroups | last post by:
Hello, I have a report with 2 subreports in the detail section. I have just changed the report so that if a "Ready" checkbox is checked yes, the background color of the detail section of the...
6
by: charsh | last post by:
Hi, I using the code below to draw a text in a pictureBox1. //Start--------------------------------------------------------------- private void button1_Click(object sender, System.EventArgs e)...
1
by: Efkas | last post by:
My application have some level : 1. MyButton class with Label inheritance 2. MyComponent as User Control loading and positionning some of MyButtons 3. MyApp loading and positionning MyComponent ...
2
by: madhuri.eerupula | last post by:
Hi Actually my task is to select some text in bitmap file and then copy paste somewhere else may in some word document or notepad or anything. My problem is how to read text from a bitmap file...
1
by: Patrick Blackman | last post by:
How do you create a Treeview Control with a Transparent background?, Any help would be appreciated.
4
by: Chris | last post by:
Hello, I have two div's, div1 is a lot bigger and is the parent of div2, and div1 also has a background image. What I would like to do, is set a background color for the smaller nested div2,...
11
by: vbt | last post by:
I am having difficult time drawing a image using Visual Studio Image Editor. I can draw the image and save it as .bmp file load it into a pictureBox image but when the program is run the...
3
by: NickP | last post by:
Hi there, I have a usercontrol that inherits from Windows.Forms.Button. In the OnCreateControl method I set the style of the control so that it supports transparent backcolor and set the...
8
by: salmobytes | last post by:
Making thumbnail images isn't all that hard, for the most part. There is lots of shrink-wrapped code out there you can download and/or munge from. But nothing I've yet seen does the right thing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.