⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asc-art.cpp

📁 convert a small image to a text image ascii art
💻 CPP
字号:
/*
                                                
                                                  
                         00000                    
                      00000000000                 
                     0000000000000                
                    00000000000000                
                   00000000000000 00        0     
        00        00000000000000 000      000     
       0000       0000000000000 0000      000     
     000000      000000000000  00000      00 00   
    0000 000     000  000000 0000 00     000 000  
    00000  00    000000000  00000 00     00  000  
     000000 00   0000000   0000  000    0000000   
           00000           0     00    00000      
             0000          0  00 0    000         
               000         0     000 00           
                  00          0000000             
                  000  000   0000 00              
                  00   000000000                  
                        00000 0000                
                    00  00000 0 00                
                     0    000   00                
                     00        000                
                     0000 0  0000                 
                     00 00000000                  
                   000  0000000  0000             
                 0000    00  0    0000000         
            00000000               0000000        
            00000000               0000 0         
            00 0000                 000           
             000000                 000           
              0000                  000     

  */
                                                  

#include <windows.h>

#include <commctrl.h>
#pragma comment(lib,"comctl32.lib")


////////////////////
#include <richedit.h>
#pragma comment(lib,"riched20.lib")
// The IID in riched20.lib in the Plaform SDK appears to be incorrect.
// This one is from the Microsoft Knowledge Base, article Q270161.
const IID IID_ITextServices = {
  // 8d33f740-cf58-11ce-a89d-00aa006cadc5
  0x8d33f740, 0xcf58, 0x11ce, {0xa8, 0x9d, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5}
};

 
#include <commctrl.h>
#include <atlstr.h>
///////////////////////

#include "Asc-art.h"
#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
#define	say(s)	MessageBox(NULL,s,"Error",MB_OK | MB_ICONASTERISK	);
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

void PickFile(  char *FileName,char ext[4], const HWND hHandle );
HDC Backbuff(  HBITMAP hBitmap,int ratio);

extern "C" HBITMAP Ole2BITMAP(char *szFile, LPPICTURE *pIPicture);
extern "C" int OleGetPicWidth(LPPICTURE pIPicture);
extern "C" int OleGetPicHeight(LPPICTURE pIPicture);
//HBITMAP image;



LPPICTURE 	picture;
static HBITMAP  picture_bitmap;


HINSTANCE	hRich;
HWND main;
HWND hConteneur;

HWND back=NULL;
 int ix=0,iy=0;
 int l=0;
 static HDC		hDC=NULL;
 int hRes=0;




int APIENTRY  WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_ASCART, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ASCART);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_ASCART);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH) GetStockObject (BLACK_BRUSH) ;
	wcex.lpszMenuName	= (LPCTSTR)IDC_ASCART;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

 


//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   int X=800;
   int Y=600;
   CHARFORMAT cf;
   

   InitCommonControls();
   CoInitialize(NULL);  
 
 
	//static HMODULE hRtLib =LoadLibrary("msftedit.dll"); //richedit 4.0
	 //if(hRtLib==NULL) hRtLib=LoadLibrary("RICHED20.DLL"); //richedit 2.0

	  static HMODULE hRtLib=LoadLibrary("RICHED20.DLL"); //richedit 2.0

 

   hInst = hInstance; 

   back=main = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

 
   

 
hConteneur=CreateWindowEx(WS_EX_CLIENTEDGE,RICHEDIT_CLASS, //2.0
//hConteneur=CreateWindowEx(WS_EX_CLIENTEDGE,(LPCTSTR) MSFTEDIT_CLASS, //4.0
	 "",
	  WS_CHILD | WS_VISIBLE |ES_MULTILINE | WS_VSCROLL| WS_HSCROLL |ES_SUNKEN,
	  0,0,800,600,
	 main,0,0,0);

 SetWindowLong( main, 0, (LONG)hConteneur );

 
 
 SetFocus(hConteneur);

 
  
 NONCLIENTMETRICS ncm;
  ZeroMemory(&ncm,sizeof ncm);
  ncm.cbSize = sizeof ncm;
  SystemParametersInfo(SPI_GETNONCLIENTMETRICS,sizeof ncm,&ncm,0);
 

 
			ZeroMemory(&cf,sizeof(CHARFORMAT));
			cf.cbSize =  sizeof(CHARFORMAT);
 			SendMessage(hConteneur, EM_GETPARAFORMAT, 0, (LPARAM) &cf);
			//cf.dwMask = CFM_BOLD|CFM_CHARSET|CFM_COLOR|CFM_FACE|CFM_ITALIC|CFM_OFFSET|  CFM_PROTECTED|CFM_SIZE|CFM_STRIKEOUT|CFM_UNDERLINE;
            cf.bPitchAndFamily== DEFAULT_PITCH|FF_DONTCARE; 
 

	cf.dwMask =  CFM_FACE | CFM_SIZE | CFM_CHARSET| CFM_COLOR ; //|CFM_BOLD;
	//cf.dwEffects = CFE_BOLD | CFM_FACE ;

	cf.yHeight = 150;
	cf.crTextColor=RGB(10,100,230);
	//strcpy(cf.szFaceName, "Tahoma");
	strcpy(cf.szFaceName, "courier new");
	
    //SendMessage(hConteneur, EM_SETCHARFORMAT,(WPARAM)SCF_ALL, (LPARAM)&cf );
    SendMessage(hConteneur,  EM_SETFONTSIZE, (WPARAM)32, (LPARAM)NULL);
	//SendMessage( hConteneur, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf);
	SendMessage(hConteneur,  EM_SETCHARFORMAT,(WPARAM)SCF_ALL, (LPARAM)&cf );

	PARAFORMAT pf;
	ZeroMemory(&pf,sizeof (PARAFORMAT));
	pf.cbSize = sizeof (PARAFORMAT);
	pf.dwMask =PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT  ;
	pf.wAlignment = PFA_CENTER;
 
	pf.dxOffset=10;
 
	SendMessage( hConteneur, EM_SETPARAFORMAT, FALSE, (LPARAM) &pf);
	SendMessage( hConteneur, EM_REPLACESEL, TRUE, (LPARAM) "\n");

 
 

   ShowWindow(main, SW_MAXIMIZE);
   UpdateWindow(main);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	char file[260];
	


	switch (message) 
	{

		case WM_CREATE:
		
		break;
	case WM_COMMAND:
		wmId    = LOWORD(wParam); 
		wmEvent = HIWORD(wParam); 
		// Parse the menu selections:
		switch (wmId)
		{

		case 	ID_VIEWNOTEPAD:
			WinExec("notepad ascii.txt",SW_SHOW);

			break;

		case 	ID_LOADIMAGE:

			PickFile(  file,"*.*",hWnd );
			picture_bitmap=Ole2BITMAP(file, &picture);
			if(picture_bitmap==NULL) { say("雛ror loading image?"); }
			else  { l=1;
			   
                  hDC=Backbuff(  picture_bitmap,0);
			}

			 

			break;

			case ID_ASCI: 
				{
					DWORD	dwPixel,
							i = 0;
					int U=0;
					 
	                static char  szText[15][256] = { 
						{".-~:;+!xo0O*8X&%#@.-~:;+!xo0O*8X&%#@"},
						{"*-o%x+)"},
						{"^%~+-=:;<>[]{}"},
						{"xXyYOo~-*()$#%^:;<>,.{}[]"},
						{"1234567890"},
						{" -~:+o0O::@@@::O0o+:-~ "},
						{" ~-+=]})*#@#*({[=+-~"},
						{"$ ! ~ & * _ - + = | "},
						{"ΖΟ

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -