سورس متصل کردن کنسول به پنجره
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#define WIN_X 300
#define WIN_Y 200
TCHAR WIN_NAME[]=_T( "WinCon by Mike Leon" );
TCHAR WIN_CLASS[]=_T( "__WIN_SHELL__" );
LRESULT CALLBACK WindowProc( HWND,UINT,WPARAM,LPARAM );
HANDLE CreateOutputConsole( LPCTSTR );
int ConPrintf( HANDLE,DWORD,LPCTSTR,... );
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevinstance,
LPTSTR lpCmdline,
int nCmdshow )
{
HWND hWnd;
MSG mSg;
WNDCLASSEX winClass;
winClass.cbSize=sizeof( WNDCLASSEX );
winClass.style= 0;
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_CLASS;
winClass.hIconSm =NULL;
RegisterClassEx( &winClass );
hWnd=CreateWindow( WIN_CLASS,
WIN_NAME,
WS_CAPTION | WS_MINIMIZEBOX |
WS_MAXIMIZEBOX | WS_SIZEBOX |
WS_SYSMENU,
CW_USEDEFAULT,CW_USEDEFAULT,
WIN_X,WIN_Y,
NULL,
NULL,
hInstance,
NULL );
ShowWindow( hWnd,nCmdshow );
UpdateWindow( hWnd );
while( GetMessage( &mSg,NULL,0,0 ) )
{
TranslateMessage( &mSg );
DispatchMessage ( &mSg );
}
return ( mSg.wParam );
}
LRESULT CALLBACK WindowProc( HWND hWnd,
UINT mSg,
WPARAM wParam,
LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR sBuffer[ 255 ];
static HANDLE hCon;
switch( mSg )
{
case WM_CREATE:
hCon=CreateOutputConsole( _T( "Debug Shell" ) );
if( INVALID_HANDLE_VALUE==hCon )
MessageBox( hWnd,_T( "ERROR:Failed" ),
_T( "FAILED" ),MB_OK );
else
ConPrintf( hCon,255,"WM_CREATE: value of %d\n",
WM_CREATE );
break;
case WM_PAINT:
hdc=BeginPaint( hWnd,&ps);
EndPaint( hWnd,&ps );
ConPrintf( hCon,255,"WM_PAINT: value %d\n",
WM_PAINT );
break;
case WM_CHAR:
ConPrintf( hCon,255,_T( "WM_CHAR: value of %c\n" ),
( TCHAR )wParam );
break;
case WM_LBUTTONDOWN:
ConPrintf( hCon,255,_T( "WM_LBUTTONDOWN: value of %d\n" ),
WM_LBUTTONDOWN );
break;
case WM_LBUTTONUP:
ConPrintf( hCon,255,_T( "WM_LBUTTONUP: value of %d\n" ),
WM_LBUTTONUP );
break;
case WM_SYSKEYDOWN:
if( lParam & 0x20000000 &&
wParam==VK_RETURN )
ConPrintf( hCon,255,_T( "ALT+Enter key is was pressed\n" ) );
break;
case WM_MOVING:
ConPrintf( hCon,255,_T( "WM_MOVING: value of %d\n" ),
WM_MOVING );
break;
case WM_SIZE:
lstrcpy( sBuffer,_T( " " ) );
if( wParam==SIZE_MINIMIZED )
lstrcpy( sBuffer,_T( "MINIMIZED WINDOW" ) );
else if( wParam==SIZE_MAXIMIZED )
lstrcpy( sBuffer,_T( "MAXIMIZED WINDOW" ) );
else if( wParam==SIZE_RESTORED )
lstrcpy( sBuffer,_T( "RESTORED WINDOW" ) );
if( lstrcmp( sBuffer,_T( " " ) )!=0 )
ConPrintf( hCon,255,"%s\n",sBuffer );
break;
case WM_RBUTTONUP:
FreeConsole();
break;
case WM_DESTROY:
FreeConsole();
PostQuitMessage( 0 );
break;
default: return( DefWindowProc( hWnd,mSg,
wParam,lParam ) );
}
return 0;
}
HANDLE CreateOutputConsole( LPCTSTR lpConTitle )
{
HANDLE hCon;
if( !AllocConsole() ) return NULL;
hCon=CreateFile( _T( "CONOUT$" ),GENERIC_WRITE,0,0,
CREATE_NEW,FILE_ATTRIBUTE_NORMAL,0 );
if( hCon!=INVALID_HANDLE_VALUE && lpConTitle )
SetConsoleTitle( lpConTitle);
return hCon;
}
int ConPrintf( HANDLE hCon,DWORD dwSize,LPCTSTR lpText,... )
{
TCHAR *sMsg;
DWORD dwBytes;
va_list ptr;
int nReturn;
sMsg=NULL;
nReturn=0;
if( dwSize>0 )
sMsg=( TCHAR* )HeapAlloc( GetProcessHeap(),0,
sizeof( TCHAR )*dwSize );
if( !sMsg )
return -1;
va_start( ptr,lpText );
if( vsprintf( sMsg,lpText,ptr )<0 )
nReturn=-2;
if( nReturn!=-2 )
nReturn=( !WriteConsole( hCon,sMsg,lstrlen( sMsg ),
&dwBytes,NULL ) ) ?
-3 : dwBytes;
HeapFree( GetProcessHeap(),0,sMsg );
return nReturn;
}