#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define NAME_OF_PROG "Calculator Mirror."
#define BLT_TIMER 1
#define WIN_X 260
#define WIN_Y 235
#define WINPOS_X 20
#define WINPOS_Y 90
HWND hMainWin = NULL;
HINSTANCE hInstance= NULL;
LRESULT CALLBACK WindowProc( HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam );
bool Get_Calc_Client( HWND hMain );
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
HWND hwnd;
MSG msg;
WNDCLASSEX winclass;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground= (HBRUSH) GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName= "WIN_SHELL";
winclass.hIconSm = NULL;
if( !RegisterClassEx( &winclass ) ) return FALSE;
if(!( hwnd= CreateWindow("WIN_SHELL",
NAME_OF_PROG,
WS_VISIBLE | WS_OVERLAPPEDWINDOW,
WINPOS_X,WINPOS_Y,
WIN_X,WIN_Y,
NULL,NULL,
hinstance,NULL)))
return FALSE;
hMainWin= hwnd;
while ( true )
{
if( PeekMessage( &msg,NULL,0,0,PM_REMOVE ) )
{
if( msg.message == WM_QUIT ) break;
TranslateMessage( &msg );
DispatchMessage ( &msg );
}
}
return ( msg.wParam );
}
LRESULT CALLBACK WindowProc( HWND hWnd,
UINT mSg,
WPARAM wParam,
LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hdc;
switch ( mSg )
{
case WM_CREATE:
if ( !Get_Calc_Client( hWnd ) )
MessageBox( hWnd,"Calc. is not opened.",NAME_OF_PROG,MB_OK );
SetTimer( hWnd,BLT_TIMER,500,NULL );
return 0;
case WM_PAINT:
hdc= BeginPaint( hWnd,&ps);
EndPaint( hWnd,&ps );
return 0;
case WM_DESTROY:
KillTimer( hWnd,BLT_TIMER );
PostQuitMessage( 0 );
return 0;
case WM_KEYDOWN:
Get_Calc_Client( hWnd );
return 0;
case WM_TIMER:
Get_Calc_Client( hWnd );
return 0;
default:
break;
}
return ( DefWindowProc( hWnd, mSg, wParam, lParam ) );
}
bool Get_Calc_Client( HWND hMain )
{
HWND hCalc;
HDC mainDC;
HDC calcDC;
RECT calcRect;
hCalc= FindWindowEx( NULL,NULL,"SciCalc","Calculator" );
if ( !hCalc ) return false;
mainDC= GetDC( hMain );
calcDC= GetDC( hCalc );
GetClientRect( hCalc,&calcRect );
if( !BitBlt( mainDC,
calcRect.top,
calcRect.left,
calcRect.right,
calcRect.bottom,
calcDC,0,0,SRCCOPY ) )
MessageBox( hMain,"Calc. Blt failed.",NAME_OF_PROG,MB_OK );
ReleaseDC( hMain,mainDC );
ReleaseDC( hCalc,calcDC );
return true;
}
The above code uses the raw api to create a simple window. A few more api calls are made to retrieve the HWND of the calc. This HWND is used to capture the image of the calculators client area so that we may bitblt() the contents of it onto our own programs client area. I implemented a timer so that the image would be refreshed every half second.
Mike L.G.
mlg400@blazemail.com