Инструменты пользователя

Инструменты сайта


Боковая панель

Связь

allocconsole

AllocConsole

#include<windows.h>
#include<commctrl.h>
#include <stdlib.h>
#pragma comment (lib, "comctl32.lib")
/*
Из графического приложения запускается консоль, в которую можно выводить например отладочную информацию
*/

void ShowError(DWORD);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void RegisterClass(TCHAR*, HINSTANCE);
HANDLE con = NULL;
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	AllocConsole();
	SECURITY_ATTRIBUTES s;
	s.bInheritHandle = TRUE;
	con = CreateFile(
		L"CONOUT$", 
		GENERIC_READ | GENERIC_WRITE ,
		FILE_SHARE_READ | FILE_SHARE_WRITE, 
		&s, 
		OPEN_EXISTING , 
		NULL,
		NULL);

	DWORD d;
	WriteFile(con,"Push the button\n",16,&d,NULL);
	
	TCHAR szAppName[] = L"Hello Win";
	HWND hwnd;
	MSG msg;
	RegisterClass(szAppName, hInstance);
	hwnd = CreateWindow( szAppName, L"my window", WS_OVERLAPPEDWINDOW | WS_HSCROLL, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
	InitCommonControls();
	ShowWindow( hwnd, iCmdShow );
	UpdateWindow( hwnd );

	while( GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}
	return (int)msg.wParam;
}

void RegisterClass(TCHAR * name, HINSTANCE hInst)
{
	WNDCLASSEX wc;
	wc.cbSize = sizeof(wc);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInst;
	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = ( HBRUSH ) GetStockObject( WHITE_BRUSH );
	wc.lpszClassName = name;
	wc.lpszMenuName = NULL;
	RegisterClassEx( &wc );

}
LRESULT CALLBACK WndProc( HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam )
{
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rect;
	switch( iMsg )
	{
		case WM_CREATE:
			{
				CreateWindow(L"Button",L"Push Me", WS_CHILD| WS_VISIBLE | BS_PUSHBUTTON,20,20,200,200, hwnd, (HMENU)0, GetModuleHandle(NULL), NULL); 
			return 0;
			}
		case WM_COMMAND:
			{
				DWORD d;
				WriteFile(con,"Button is pushed\n",17,&d,NULL);
				return 0;
			}
		case WM_PAINT:
			{
			hdc = BeginPaint( hwnd, &ps );
			GetClientRect( hwnd, &rect );
			EndPaint( hwnd, &ps );
			return 0;
			}
		case WM_DESTROY:
			PostQuitMessage( 0 );
			return 0;
		case WM_LBUTTONDOWN:
			
			return 0;
	}
	return DefWindowProc( hwnd, iMsg, wParam, lParam );
}

void ShowError(DWORD Err)
{
	LPVOID lpMsgBuf;
	if (!FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		Err,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &lpMsgBuf,
		0,
		NULL ))
	{
	return;
	}
	MessageBox( NULL, (LPCTSTR)lpMsgBuf, L"Error", MB_OK | MB_ICONINFORMATION );
	LocalFree( lpMsgBuf );
}



allocconsole.txt · Последние изменения: 2011/04/26 09:12 (внешнее изменение)