Put a Bitmap Image on your Console (C)

vegaseat 2 Tallied Votes 9K Views Share

You have to mildly trick Windows, but it is possible to put a bitmap image on your console. Uses a flock of WinApi calls. Works well with the free PellesC compiler and Dev-C++. This is a Windows Console Application. Drop me a comment, if you have luck with another compiler.

// put a bitmap image on a Windows Console display
// BCX basic original by Joe Caverly and Kevin Diggins
// BCX generated C code modified for PellesC/Dev-C++
 
#include <stdio.h>
#include <string.h>
#include <windows.h>    // Win32Api Header File 
    
static HWND  hConWnd;

HWND BCX_Bitmap(char*,HWND=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0);
HWND GetConsoleWndHandle(void);


int main()
{
  hConWnd = GetConsoleWndHandle();
  if (hConWnd)
  {
    // select a bitmap file you have or use one of the files in the Windows folder
    // filename, handle, ID, ulcX, ulcY, width, height   0,0 auto-adjusts
	  BCX_Bitmap("C:\\Windows\\greenstone.bmp",hConWnd,123,1,1,0,0);
	  
    getchar();  // wait
  }
  return 0;
}


// draw the bitmap
HWND BCX_Bitmap(char* Text,HWND hWnd,int id,int X,int Y,int W,int H,int Res,int Style,int Exstyle)
{
  HWND A;
  HBITMAP hBitmap;
  
  // set default style
  if (!Style) Style = WS_CLIPSIBLINGS|WS_CHILD|WS_VISIBLE|SS_BITMAP|WS_TABSTOP;
  
  // form for the image
  A = CreateWindowEx(Exstyle,"static",NULL,Style,X,Y,0,0,hWnd,(HMENU)id,GetModuleHandle(0),NULL);
                      
  // Text contains filename
  hBitmap=(HBITMAP)LoadImage(0,Text,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);

  // auto-adjust width and height
  if (W || H) hBitmap = (HBITMAP)CopyImage(hBitmap,IMAGE_BITMAP,W,H,LR_COPYRETURNORG);
  SendMessage(A,(UINT)STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hBitmap);
  if (W || H) SetWindowPos(A,HWND_TOP,X,Y,W,H,SWP_DRAWFRAME);
  return A;
}


// tricking Windows just a little ...
HWND GetConsoleWndHandle(void)
{
  HWND hConWnd;
  OSVERSIONINFO os;
  char szTempTitle[64], szClassName[128], szOriginalTitle[1024];

  os.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
  GetVersionEx( &os );
  // may not work on WIN9x
  if ( os.dwPlatformId == VER_PLATFORM_WIN32s ) return 0;

  GetConsoleTitle( szOriginalTitle, sizeof ( szOriginalTitle ) );
  sprintf( szTempTitle,"%u - %u", GetTickCount(), GetCurrentProcessId() );
  SetConsoleTitle( szTempTitle );
  Sleep( 40 );
  // handle for NT and XP
  hConWnd = FindWindow( NULL, szTempTitle );
  SetConsoleTitle( szOriginalTitle );

  // may not work on WIN9x
  if ( os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
  {
    hConWnd = GetWindow( hConWnd, GW_CHILD );
    if ( hConWnd == NULL ) return 0;
    GetClassName( hConWnd, szClassName, sizeof ( szClassName ) );
    // while ( _stricmp( szClassName, "ttyGrab" ) != 0 )
    while ( strcmp( szClassName, "ttyGrab" ) != 0 )
    {
      hConWnd = GetNextWindow( hConWnd, GW_HWNDNEXT );
      if ( hConWnd == NULL ) return 0;
      GetClassName( hConWnd, szClassName, sizeof( szClassName ) );
    }
  }
  return hConWnd;
}
botter911 -2 Light Poster

The code is good and excellent it works. It only hangs the Command prompt. Any solution to this?

Asbah93 0 Newbie Poster

Hello, the code is good. It works on Visual C++ 06. But there is a problem that if we want to load two images then the first image appears but the second doesnot appear on it. The 2nd one can be displaced on a side of the 1st one but not on it. Please help me.

np complete 8 Newbie Poster

Sir,
can you suggest some tutorials for these?
thanks

Member Avatar for I_m_rude
I_m_rude

can u tell me how to begin all these things like the code you have made ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.