Exemple #1
0
void GDisplay::SaveAsBitmap(LPCTSTR pszFileName)
{
	HDC hdcClient = GetDC(m_hWnd); 
	HDC hdcCompatible = CreateCompatibleDC(hdcClient); 
 

	HBITMAP hBmp = CreateCompatibleBitmap(hdcClient, m_nClientWidth, m_nClientHeight);
	HGDIOBJ hOldBmp = SelectObject(hdcCompatible, hBmp); 

	BitBlt(hdcCompatible, 
		0,0, 
		m_nClientWidth, m_nClientHeight, 
		hdcClient, 
		0,0, 
		SRCCOPY);
	
	
	PBITMAPINFO pbi = CreateBitmapInfoStruct(hBmp);
	if (!CreateBMPFile(pszFileName, pbi, hBmp, hdcCompatible))
	{
		AfxGetApp()->Error(IDS_ERROR_CREATE_BMP);
	}

	SelectObject(hdcCompatible, hOldBmp);
	DeleteDC(hdcCompatible);
	DeleteObject(hBmp); 

}
Exemple #2
0
void WINAPI GenerateBMPW(LPWSTR fileName, LPWSTR text, int margin, int size)
{
	if (fileName == NULL)
		return;

	HBITMAP bmp = GetHBitmap(text, margin, size);
	if (bmp == NULL)
		return;
	
	PBITMAPINFO info = CreateBitmapInfoStruct(bmp);
	if (info != NULL)
	{
		HDC hdc = GetDC(0);
		CreateBMPFile(fileName, info, bmp, hdc);
		ReleaseDC(0, hdc);
		DeleteObject(bmp);
		LocalFree(info);
	}

}
Exemple #3
0
void generateBitmap(Map<Surface> map, int n, int m, LPTSTR name)
{
	int ppc = ::config::pixels_per_cell;

	HWND hwnd = ::GetActiveWindow();
	HDC hDC = GetDC(hwnd);
	HDC memDC = CreateCompatibleDC(hDC);
	HBITMAP memBM = CreateCompatibleBitmap(hDC, n * ppc, m * ppc);
	SelectObject(memDC, memBM);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			for (int x = i * ppc; x < (i + 1) * ppc; ++x)
				for (int y = j * ppc; y < (j + 1) * ppc; ++y)
					SetPixel(memDC, x, y, map[i][j].color);
		}
	}

	CreateBMPFile(hwnd, name, CreateBitmapInfoStruct(hwnd, memBM), memBM, memDC);

	DeleteObject(memBM);
	ReleaseDC(hwnd, memDC);
}
// void CaptureScreen(const char *filename)
char* CaptureScreenToYuv( size_t &len )
{
    int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
    int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
    HWND hDesktopWnd = GetDesktopWindow();
    HDC hDesktopDC = GetDC(hDesktopWnd);
    HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
    HBITMAP hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC,
        nScreenWidth, nScreenHeight);
    SelectObject(hCaptureDC, hCaptureBitmap);
    BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight,
        hDesktopDC, 0, 0, SRCCOPY | CAPTUREBLT);

    // Draw cursor
    CURSORINFO cursor = { sizeof(cursor) };
    ::GetCursorInfo(&cursor);
    if (cursor.flags == CURSOR_SHOWING) {
        RECT rcWnd;
        ::GetWindowRect(hDesktopWnd, &rcWnd);
        ICONINFOEXW info = { sizeof(info) };
        ::GetIconInfoExW(cursor.hCursor, &info);
        const int x = cursor.ptScreenPos.x - rcWnd.left - info.xHotspot;
        const int y = cursor.ptScreenPos.y - rcWnd.top - info.yHotspot;
        BITMAP bmpCursor = { 0 };
        ::GetObject(info.hbmColor, sizeof(bmpCursor), &bmpCursor);
        ::DrawIconEx(hCaptureDC, x, y, cursor.hCursor, bmpCursor.bmWidth, bmpCursor.bmHeight,
            0, NULL, DI_NORMAL);
    } // if

    PBITMAPINFO bmpInfo = CreateBitmapInfoStruct(hCaptureBitmap);
    // CreateBMPFile(filename, bmpInfo, hCaptureBitmap, hDesktopDC);
    char *pFrame = AppendToYUV(len, bmpInfo, hCaptureBitmap, hDesktopDC);

    ReleaseDC(hDesktopWnd, hDesktopDC);
    DeleteDC(hCaptureDC);
    DeleteObject(hCaptureBitmap);

    return pFrame;
}
  //
  // Entry point for conversion
  //
UT_Error IE_ImpGraphic_Win32Native::_convertGraphic(UT_ByteBuf * pBB, std::string& mimetype)
{	
    IPicture* pPicture = NULL;
    IStream* stream;	
    HGLOBAL hG;		
    HBITMAP hBitmap;
    OLE_HANDLE* hB;
    PBITMAPINFO	bi;
    UT_ByteBuf bBufBMP;
    UT_Error err;	

	/* If the system has GDI+, use it*/
	if (isGDIPlusAvailable())
	{
		m_pBB = new UT_ByteBuf();
		return GDIconvertGraphic(pBB, m_pBB, mimetype);		
	}

	// the code below always writes out PNG's for now; we could update it to support
	// native JPEG images as well, or just delete it and always use GDI+.
	mimetype = "image/png";

    // We need to store the incoming bytebuffer in a Windows global heap	
    size_t nBlockLen = pBB->getLength();   	
    hG = GlobalAlloc(GPTR, nBlockLen);
    if (!hG) 
		return UT_IE_NOMEMORY;	
    
    CopyMemory(hG, pBB->getPointer(0), nBlockLen);   	
    
    // Create a stream from heap
    HRESULT hr = CreateStreamOnHGlobal(hG,false,&stream);
    if (!SUCCEEDED(hr) || !stream)
	{
		GlobalFree(hG);
		return UT_IE_NOMEMORY;
	}
    
    hr = OleLoadPicture(stream,0,false,IID_IPicture,(void**)&pPicture);	

    stream->Release();
    GlobalFree(hG);
    
    if (!SUCCEEDED(hr) || !pPicture)
	{
		return UT_IE_UNKNOWNTYPE;
	}

    pPicture->get_Handle((unsigned int*)&hB);
    
    hBitmap = (HBITMAP)CopyImage(hB,IMAGE_BITMAP,0,0,LR_COPYRETURNORG);
    
    HWND hWnd = GetDesktopWindow();
    
    // Create a BMP file from a BITMAP	
    bi = CreateBitmapInfoStruct(hBitmap);						
    CreateBMPFile(hWnd, bBufBMP, bi, hBitmap, GetDC(hWnd));
    LocalFree ((HLOCAL)bi);
    
    InitializePrivateClassData();
    
    /* Read Header Data */
    err = Read_BMP_Header(&bBufBMP);
	
	/* 
	   It's not a bitmap, then we have to rendered it into a device
	   context and get a bitmap from there. Case wmf graphics
	*/
	if (err) 
	{
		if (err!=UT_IE_BOGUSDOCUMENT) 
		{
			pPicture->Release();
			return err;		
		}
		
        long nWidth  = 0;
        long nHeight = 0;
		long nScaleToWidth= 500;
		long nScaleToHeight= 500;		
		RECT rc, rect;				
		BYTE *imagedata;
		HBITMAP hBit;
		HBITMAP hOld;
		BITMAPINFO bmi; 		
		HDC hWndDC = GetDC(hWnd);
		HDC	hMemDC = CreateCompatibleDC(hWndDC);
		HBRUSH hBrush = (HBRUSH)GetCurrentObject(hMemDC, OBJ_BRUSH);
		
		pPicture->get_Width (&nWidth);
		pPicture->get_Height(&nHeight);

		bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 	
		bmi.bmiHeader.biWidth = nScaleToWidth;
		bmi.bmiHeader.biHeight = nScaleToHeight;
		bmi.bmiHeader.biPlanes = 1; 
		bmi.bmiHeader.biBitCount = 24; // as we want true-color
		bmi.bmiHeader.biCompression = BI_RGB; // no compression
		bmi.bmiHeader.biSizeImage = (((bmi.bmiHeader.biWidth * bmi.bmiHeader.biBitCount + 31) & ~31) >> 3) * bmi.bmiHeader.biHeight; 
		bmi.bmiHeader.biXPelsPerMeter = 0;
		bmi.bmiHeader.biYPelsPerMeter = 0; 
		bmi.bmiHeader.biClrImportant = 0;
		bmi.bmiHeader.biClrUsed = 0; // we are not using palette
			
		hBit = CreateDIBSection(hMemDC,&bmi,DIB_RGB_COLORS,(void**)&imagedata,0,0);						
		hOld = (HBITMAP) SelectObject(hMemDC, hBit);			

		
		rect.left = 0;
        rect.top = nScaleToHeight;
        rect.right = nScaleToWidth;
        rect.bottom = 0;

		FillRect(hMemDC, &rect,  hBrush);
		pPicture->Render(hMemDC, 0,0,  nScaleToWidth,	nScaleToHeight, 0,  nHeight, 
						 nWidth, -nHeight, &rc);	
		
		hBit =  (HBITMAP)SelectObject(hMemDC, hOld);
		
		bi =  CreateBitmapInfoStruct(hBit);						
		CreateBMPFile(hWnd, bBufBMP, &bmi, hBit, hMemDC);
		LocalFree ((HLOCAL)bi);

		
		DeleteDC(hMemDC);
		DeleteDC(hWndDC);
	    DeleteObject(hBrush); 
		DeleteObject(hBit);	  
    
		err = Read_BMP_Header(&bBufBMP);
		if (err) 
		{
			pPicture->Release();
			return err;				
		}
		
	}

	pPicture->Release();


    if ((err = Initialize_PNG()))
	{   
		return err;
	}	
    
    /* Read Palette, if no palette set Header accordingly */
    if(m_iBitsPerPlane < 24) 
	{
		if ((err = Convert_BMP_Palette(&bBufBMP))) 
			return err;
	}
    else
	{
		UT_uint16 bitsPerChannel;
		UT_uint16 colorType;

		if (m_iBitsPerPlane == 24) {
			bitsPerChannel = 8;
			colorType = PNG_COLOR_TYPE_RGB;
		} else if (m_iBitsPerPlane == 32) {
			bitsPerChannel = 8;
			colorType = PNG_COLOR_TYPE_RGB_ALPHA;
		} else if (m_iBitsPerPlane == 48) {
			bitsPerChannel = 16;
			colorType = PNG_COLOR_TYPE_RGB;
		} else if (m_iBitsPerPlane == 64) {
			bitsPerChannel = 16;
			colorType = PNG_COLOR_TYPE_RGB_ALPHA;
		} else {		   
			return UT_ERROR;
		}
	
		png_set_IHDR ( m_pPNG,
					   m_pPNGInfo,
					   m_iWidth,
					   m_iHeight,
					   bitsPerChannel,
					   colorType,
					   PNG_INTERLACE_NONE,
					   PNG_COMPRESSION_TYPE_DEFAULT,
					   PNG_FILTER_TYPE_DEFAULT );
	
	}
    if ((err = Convert_BMP(&bBufBMP))) 
	{
		return err;
	}
    
    /* Clean Up Memory Used */
		
    FREEP(m_pPNGInfo->palette);	
    png_destroy_write_struct(&m_pPNG, &m_pPNGInfo);
    
    return UT_OK;  	  	
}
Exemple #6
0
bool AP_Win32App::_pasteFormatFromClipboard(PD_DocumentRange * pDocRange, const char * szFormat,
											const char * szType, bool bWide)
{
	HANDLE	hData;
	bool 	bSuccess = false;	
  
	if (!(hData = m_pClipboard->getHandleInFormat(szFormat)))
		return bSuccess;		
 		
 	// It's a bitmap
 	if (g_ascii_strcasecmp(szFormat, AP_CLIPBOARD_BMP)==0)
	{			
 		HBITMAP					hBitmap;
 		PBITMAPINFO 			bi;
 		HWND		 			hWnd;
 		HDC 					hdc;
 		IE_ImpGraphic*			pIEG = NULL;
 		FG_Graphic* 			pFG = NULL;	
 		UT_Error 				errorCode;		
 		UT_ByteBuf 				byteBuf;				
 		IEGraphicFileType		iegft = IEGFT_BMP;	
 		XAP_Frame* 				pFrame;						
 		AP_FrameData* 			pFrameData;		
 		FL_DocLayout*			pDocLy;	
 		FV_View* 				pView;						
		UT_ByteBuf*				bBufBMP = new UT_ByteBuf;
 		
 		hBitmap = (HBITMAP)hData;					
 		hWnd =  GetDesktopWindow();
 		hdc = GetDC(hWnd);		
 		
 		// Create a BMP file from a BITMAP
 		bi =  CreateBitmapInfoStruct(hBitmap);						
 		CreateBMP(hWnd, *bBufBMP, bi, hBitmap,hdc);                  										
 		
 		// Since we are providing the file type, there is not need to pass the bytebuff filled up
 		errorCode = IE_ImpGraphic::constructImporter(*bBufBMP, iegft, &pIEG);				 				
		 				
 		if(errorCode != UT_OK)		
			return false;				  	
		 				 			
 		errorCode = pIEG->importGraphic(bBufBMP, &pFG); 		
 		
 		if(errorCode != UT_OK || !pFG)
		{
			DELETEP(bBufBMP);
			DELETEP(pIEG);
 			return false;
		}
		// sunk in importGraphic
		bBufBMP = NULL;
 		 
 		// Insert graphic in the view
 		pFrame = getLastFocussedFrame(); 						
 		pFrameData = (AP_FrameData*) pFrame->getFrameData();		
 		pDocLy =	pFrameData->m_pDocLayout;	
 		pView =  pDocLy->getView();		
 				
 		errorCode = pView->cmdInsertGraphic(pFG);	  		  		
 	
		DELETEP(pIEG);
 		//DELETEP(pFG);		
 		
 		bSuccess = true;
 	}
 	else	
	{
		unsigned char * pData = static_cast<unsigned char *>(GlobalLock(hData));
		UT_DEBUGMSG(("Paste: [fmt %s %s][hdata 0x%08lx][pData 0x%08lx]\n",
					 szFormat, szType,  hData, pData));
		UT_uint32 iSize = GlobalSize(hData);
		UT_uint32 iStrLen = bWide
			? wcslen(reinterpret_cast<const wchar_t *>(pData)) * 2
			: strlen(reinterpret_cast<const char *>(pData));
		UT_uint32 iLen = UT_MIN(iSize,iStrLen);

		
		IE_Imp * pImp = 0;
		IE_Imp::constructImporter(pDocRange->m_pDoc, IE_Imp::fileTypeForSuffix(szType), &pImp, 0);
		if (pImp)
		{
			const char * szEncoding = 0;
			if (bWide)
				szEncoding = XAP_EncodingManager::get_instance()->getUCS2LEName();
			else
				; // TODO Get code page using CF_LOCALE
			pImp->pasteFromBuffer(pDocRange,pData,iLen,szEncoding);
			delete pImp;
		}

		GlobalUnlock(hData);
		bSuccess = true;
	}
	return bSuccess;
}
Exemple #7
0
int main()
{
// Create a normal DC and a memory DC for the entire screen. The
// normal DC provides a "snapshot" of the screen contents. The
// memory DC keeps a copy of this "snapshot" in the associated
// bitmap.
HDC hdcScreen, hdcCompatible;
HBITMAP hbmScreen;
SYSTEMTIME time;
char* filename;
char* filepath;
char* regSubKeyBase = "Software\\PJBSoftware\\ScreenShooter";

bool isRegVersion = false;

filepath = (char*)malloc(100 * sizeof(char));
if (!filepath) return 1;


HKEY progKey;
//Get the screenshot location from the registry, else stuff 'em in "My Documents"
if (RegOpenKeyEx(HKEY_CURRENT_USER,regSubKeyBase,0,KEY_QUERY_VALUE,&progKey) == ERROR_SUCCESS)
{
    DWORD buffsize = 100;
    DWORD type;
    if (RegQueryValueEx(progKey,"ScreenshotPath",NULL,&type,(LPBYTE)filepath,&buffsize) != ERROR_SUCCESS) return 1;
 }
else
{
    char* username = (char*) malloc(25 * sizeof(char));
    if (! username) return 1;
    DWORD buffsize = 25;
    BOOL st = GetUserName(username,&buffsize);
    if (!st) return 1;


    sprintf(filepath,"C:\\Documents and Settings\\%s\\My Documents",username);
    free (username);
}

// Get the time
GetLocalTime((LPSYSTEMTIME)&time);

hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
hdcCompatible = CreateCompatibleDC(hdcScreen);

// Create a compatible bitmap for hdcScreen.

hbmScreen = CreateCompatibleBitmap(hdcScreen,
                     GetDeviceCaps(hdcScreen, HORZRES),
                     GetDeviceCaps(hdcScreen, VERTRES));

if (hbmScreen == 0)
    printf("Could not create bitmap\n");

// Select the bitmaps into the compatible DC.

if (!SelectObject(hdcCompatible, hbmScreen))
    printf("Could not select bitmap\n");



         //Copy color data for the entire display into a
         //bitmap that is selected into a compatible DC.

        if (!BitBlt(hdcCompatible,
               0,0,
               GetDeviceCaps(hdcScreen, HORZRES), GetDeviceCaps(hdcScreen, VERTRES),
               hdcScreen,
               0,0,
               SRCCOPY))

         printf("Screen to Compat Blt Failed\n");

    filename = (char*)malloc(150 * sizeof(char));
    if (!filename) return 1;

    sprintf(filename,"%s\\screenshot-%04u-%02u-%02u-%02u%02u%02u.bmp",filepath,time.wYear,time.wMonth,time.wDay,time.wHour,time.wMinute,time.wSecond);
    free(filepath);


    CreateBMPFile(filename,CreateBitmapInfoStruct(hbmScreen),hbmScreen,hdcCompatible);

    free(filename);

 	return 0;


}
	extern "C" void CreateBMPFile(char *pszFile, HBITMAP hBMP, HDC hDC) 
	//given a handle hBMP of a bitmap that is currently selected in the display context hDC,
	//creates a .bmp file named pszFile containing the bitmap
	//don't remember where I copied this from
	{ 
		int errorLevel;
		HANDLE hf;                  // file handle 
		PBITMAPINFO pbi;			// bitmap info
		BITMAPFILEHEADER hdr;       // bitmap file-header 
		PBITMAPINFOHEADER pbih;     // bitmap info-header 
		LPBYTE lpBits;              // memory pointer 
		DWORD dwTotal;              // total count of bytes 
		DWORD cb;                   // incremental count of bytes 
		BYTE *hp;                   // byte pointer 
		DWORD dwTmp; 
		
		pbi = CreateBitmapInfoStruct(hBMP);
		pbih = (PBITMAPINFOHEADER) pbi; 
		lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);

		assert(lpBits);

		// Retrieve the color table (RGBQUAD array) and the bits 
		// (array of palette indices) from the DIB. 
		errorLevel = GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS);
		assert(errorLevel != 0);

		// Create the .BMP file. 
		hf = CreateFile(pszFile, 
					   GENERIC_READ | GENERIC_WRITE, 
					   (DWORD) 0, 
						NULL, 
					   CREATE_ALWAYS, 
					   FILE_ATTRIBUTE_NORMAL, 
					   (HANDLE) NULL); 
		assert(hf != INVALID_HANDLE_VALUE);
		hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M" 
		// Compute the size of the entire file. 
		hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + 
					 pbih->biSize + pbih->biClrUsed 
					 * sizeof(RGBQUAD) + pbih->biSizeImage); 
		hdr.bfReserved1 = 0; 
		hdr.bfReserved2 = 0; 

		// Compute the offset to the array of color indices. 
		hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + 
						pbih->biSize + pbih->biClrUsed 
						* sizeof (RGBQUAD); 

		// Copy the BITMAPFILEHEADER into the .BMP file. 
		errorLevel = WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp,  NULL);
		assert(errorLevel != 0);

		// Copy the BITMAPINFOHEADER and RGBQUAD array into the file. 
		errorLevel = WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) 
					  + pbih->biClrUsed * sizeof (RGBQUAD), 
					  (LPDWORD) &dwTmp, NULL);
		assert(errorLevel != 0);

		// Copy the array of color indices into the .BMP file. 
		dwTotal = cb = pbih->biSizeImage; 
		hp = lpBits; 
		errorLevel = WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL);
		assert(errorLevel != 0);

		// Close the .BMP file. 
		errorLevel = CloseHandle(hf);
		assert(errorLevel != 0);

		// Free memory. 
		GlobalFree((HGLOBAL)lpBits);
	}
Exemple #9
0
//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  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)
{
	PAINTSTRUCT ps;
	HDC hdc = GetDC(hWnd);
	switch (mode)
	{
	case 0:
		FreeDrawProc(hWnd, message, wParam, lParam);
		break;
	case 1:
		DrawLineProc(hWnd, message, wParam, lParam);
	case 2:
		DrawEllipseProc(hWnd, message, wParam, lParam);
	case 3:
		DrawRoundProc(hWnd, message, wParam, lParam);
	case 4:
		DrawSquareProc(hWnd, message, wParam, lParam);
	case 5:
		DrawRectangleProc(hWnd, message, wParam, lParam);
	}
	
	switch (message)
    {
	case WM_CREATE:
	{
		p1.x = p1.y = 0;
		p2 = p1;
		mode = 1;
		enTrainDessin = FALSE;

		//Tạo bitmap
	
		RECT rect;
		GetClientRect(hWnd, &rect);
		//Tạo ra một bitmap tương thích với DC màn hình
		hBitmap = CreateCompatibleBitmap(hdc, rect.right - rect.left, rect.bottom - rect.top);
		
		//Tô background cho bitmap
		//Tạo memory dc để tương tác với Bitmap
		HDC memDC = CreateCompatibleDC(hdc);
		SelectObject(memDC, hBitmap);
		HBRUSH hBrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
		FillRect(memDC, &rect, hBrush);
		DeleteObject(hBrush);
		DeleteDC(memDC);
		ReleaseDC(hWnd, hdc);
		break;
	}
    case WM_COMMAND:
        {
			
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
			case ID_LINE_STRAIGHTLINE:
				mode = 1;
			break;
			case ID_LINE_FREESTYLE:
				mode = 0;
				break;
			
			case ID_SHAPE_ELLIPSE:
				mode = 2;
				break;
			
			case ID_SHAPE_ROUND:
				mode = 3;
				break;
			
			case ID_SHAPE_SQUARE:
				mode = 4;
				break;
			
			case ID_SHAPE_RECTANGLE:
				mode = 5;
				break;

			case ID_DRAW_WIDTH:
				DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG2), hWnd, AboutBox);
				break;

			case ID_COLOR_RED:
				R = 255;
				G = 0;
				B = 0;
					break;

			case ID_COLOR_GREEN:
				R = 0;
				G = 255;
				B = 0;
					break;


			case ID_COLOR_BLUE:
				R = 0;
				G = 0;
				B = 255;
					break;

			case ID_FORMAT_COLOR:
				doChooseColor(hEdit);

				break;
            
			case ID_FILE_OPEN:

				OPENFILENAME ofn;
				TCHAR szFile[260];
				ZeroMemory(&ofn, sizeof(ofn));
				ofn.lStructSize = sizeof(ofn);
				ofn.hwndOwner = hWnd;
				ofn.lpstrFile = szFile;
				ofn.lpstrFile[0] = '\0';
				ofn.nMaxFile = sizeof(szFile);
				ofn.lpstrFilter = _T("All Files *.*\0*.*\0Text Files *.txt\0*.TXT\0 Doc Files\0*.TXT;*.DOC;*.BAK\0");
				ofn.nFilterIndex = 1;
				ofn.lpstrInitialDir = _T("C:\\");
				ofn.lpstrTitle = _T("My Application - Open file");
				ofn.lpstrDefExt = _T("txt");
				ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

				// Display the Open dialog box. 
				if (GetOpenFileName(&ofn) == TRUE) {
					PAINTSTRUCT ps;
					HDC hdc = GetDC(hWnd);
					//			HDC hdcDestination;
					HBITMAP hbitmap = (HBITMAP)::LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
					//	MessageBox(NULL, ofn.lpstrFile, _T("Selected file"), MB_OK);
					//hdc = getOb
					HDC memDC = CreateCompatibleDC(hdc);
					SelectObject(memDC, hbitmap);
					RECT rect;
					GetClientRect(hWnd, &rect);
					BitBlt(hdc, 0, 0, rect.right - rect.left, rect.bottom - rect.top, memDC, 0, 0, SRCCOPY);
				}

				break;
			case ID_FILE_SAVE:
			{
				OPENFILENAME ofn;
				ZeroMemory(&ofn, sizeof(ofn));
				char szFileName[MAX_LOADSTRING];
				ZeroMemory(szFileName, MAX_LOADSTRING);
				ofn.lStructSize = sizeof(ofn);
				ofn.hwndOwner = NULL;
				ofn.lpstrFilter = _T("All Files(*.*)\0 * .*\0Bmp Files(*.bmp)\0 * .bmp\0Text Files(*.txt)\0 * .txt\0");
				ofn.lpstrFile = (LPWSTR)szFileName;
				ofn.nMaxFile = MAX_LOADSTRING;
				ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
				ofn.lpstrDefExt = (LPCWSTR)L"bmp";
				GetSaveFileName(&ofn);
				PBITMAPINFO PBi = CreateBitmapInfoStruct(hWnd, hBitmap);
				CreateBMPFile(hWnd, ofn.lpstrFile, PBi, hBitmap, hdc);
				/*save = 1;*/
			}
				break;
			case ID_FILE_EXIT:
				DestroyWindow(hWnd);
				break;
			default:
				return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;


    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            
            // TODO: Add any drawing code that uses hdc here...
			HDC hdc = BeginPaint(hWnd, &ps);
			//Vẽ bitmap ra màn hình trước
			HDC memDC = CreateCompatibleDC(hdc);
			SelectObject(memDC, hBitmap);
			RECT rect;
			GetClientRect(hWnd, &rect);
			BitBlt(hdc, 0, 0, rect.right - rect.left, rect.bottom - rect.top, memDC, 0, 0, SRCCOPY);
				switch (mode)
				{
				case 0:
					//Vẽ tự do thì ta vẽ luôn trên màn hình và bitmap
					FreeDraw(memDC);
					FreeDraw(hdc);
					break;
				case 1:
					//Vẽ đường thẳng thì ta chỉ vẽ lên màn hình
					DrawLine(hdc);
					break;
				case 2:
					
					DrawEllipse(hdc);
					break;
				case 3:

					DrawRound(hdc);
					break;
				case 4:

					DrawSquare(hdc);
					break;
				case 5:
					//DrawRectangle(memDC);
					DrawRectangle(hdc);
					break;
				}
				
				DeleteObject(memDC);
			//SelectObject(hdc, oldPen);
			
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
Exemple #10
0
void loadTexture(UINT resourceID, Texture& texture)
{
	std::set<int> validSizes;
	validSizes.insert(64);
	validSizes.insert(128);
	validSizes.insert(256);
	validSizes.insert(512);

	HDC memDC = CreateCompatibleDC(NULL);
	HBITMAP hBitmap = LoadBitmap(g_hInstance, MAKEINTRESOURCE(resourceID));
	
	int nPitch;
	BITMAPINFO* pbmInfo = CreateBitmapInfoStruct(hBitmap, nPitch);
	
	LPBYTE pBuffer = new BYTE[pbmInfo->bmiHeader.biSizeImage];
	GetDIBits(memDC, hBitmap, 0, pbmInfo->bmiHeader.biHeight, pBuffer, pbmInfo, DIB_RGB_COLORS);

	int innerWidth = pbmInfo->bmiHeader.biWidth;
	int innerHeight = pbmInfo->bmiHeader.biHeight;
	if(validSizes.count(innerWidth) == 0)
	{
		std::set<int>::iterator find_it = std::find_if(validSizes.begin(), validSizes.end(), 
			std::bind2nd(std::greater<int>(), innerWidth));
		if(find_it != validSizes.end())
			texture.nWidth = *find_it;
	}else
	{
		texture.nWidth = innerWidth; 		
	}

	if(validSizes.count(innerHeight) == 0)
	{
		std::set<int>::iterator find_it = std::find_if(validSizes.begin(), validSizes.end(), 
			std::bind2nd(std::greater<int>(), innerHeight));
		if(find_it != validSizes.end())
			texture.nHeight = *find_it;
	}else
	{
		texture.nHeight = innerHeight; 		
	}

	texture.nMaxU = (innerWidth * 1.0) / texture.nWidth;
	texture.nMaxV = (innerHeight * 1.0) / texture.nHeight;
	texture.pBuffer = new DWORD[texture.nWidth * texture.nHeight];
		
	BYTE* pIndexPointer = pBuffer;
	DWORD* pDstPointer = texture.pBuffer;
	DWORD color;
	for(int y = 0; y < texture.nHeight; y++)
	{
		DWORD* pCurrentBitString = (DWORD*)pIndexPointer;
		for(int x = 0; x < texture.nWidth; x++)
		{
			if(x < innerWidth && y < innerHeight)
			{
				color = pCurrentBitString[x];
				if(color != 0x000000ff)
				{
					BYTE blue = (BYTE)((color & 0x00ff0000) >> 16);
					BYTE green = (BYTE)((color & 0x0000ff00) >> 8);
					BYTE red = (BYTE)(color & 0x000000ff);
					color = (red << 16) | (green << 8) | blue | 0xff000000;
				}else
				{
					color = 0x00000000;
				}
			}else