I am trying to load the bmp image from my C to the console and it works. The image opens. But the problem now is the console hangs. Is there something wrong in my code? Or what should I change to avoid making the console hang.
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
HWND CopyBitmap(char*,HWND=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0);
HWND ConsoleWindow(void);
int main()
{
if (HWND myWin = ConsoleWindow())
{
CopyBitmap("C:\\sample.bmp",myWin,123,1,1,0,0);
getch();
}
return 0;
}
HWND CopyBitmap(char* Text,HWND hWnd,int id,int X,int Y,int W,int H,int Res,int Style,int Exstyle)
{
HWND A;
HBITMAP hBitmap;
if (!Style) Style = WS_CLIPSIBLINGS|WS_CHILD|WS_VISIBLE|SS_BITMAP|WS_TABSTOP;
A = CreateWindowEx(Exstyle,"static",NULL,Style,X,Y,0,0,hWnd,(HMENU)id,GetModuleHandle(0),NULL);
hBitmap=(HBITMAP)LoadImage(0,Text,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
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;
}
HWND ConsoleWindow(void)
{
HWND myWin;
OSVERSIONINFO os;
char szTempTitle[64], szClassName[128], szOriginalTitle[1024];
os.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
GetVersionEx( &os );
if ( os.dwPlatformId == VER_PLATFORM_WIN32s ) return 0;
GetConsoleTitle( szOriginalTitle, sizeof ( szOriginalTitle ) );
sprintf( szTempTitle,"%u - %u", GetTickCount(), GetCurrentProcessId() );
SetConsoleTitle( szTempTitle );
Sleep( 40 );
myWin = FindWindow( NULL, szTempTitle );
SetConsoleTitle( szOriginalTitle );
if ( os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
{
myWin = GetWindow( myWin, GW_CHILD );
if ( myWin == NULL ) return 0;
GetClassName( myWin, szClassName, sizeof ( szClassName ) );
while ( strcmp( szClassName, "ttyGrab" ) != 0 )
{
myWin = GetNextWindow( myWin, GW_HWNDNEXT );
if ( myWin == NULL ) return 0;
GetClassName( myWin, szClassName, sizeof( szClassName ) );
}
}
return myWin;
}