Syntax for Capture and Print

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
RalphMDS
Posts: 21
Joined: Wed Mar 25, 2009 1:24 pm
Location: New Jersey, U.S.A.

Syntax for Capture and Print

Post by RalphMDS »

Hello everyone,

I am a new user to the forum. For the last two weeks I have been playing with HMG and I love it. Thanks Roberto for his wonderful implementation of my favorite programming language!

I have a few questions I have not been about to find answers to in the documentation. Hopefully, someone can give me some guidance...

1) If I have a window called 'Main' and I want to capture it to a file called 'test.bmp' from coordinates 10,10 for a width of 100 and a height of 100 how would I write this statement? I have tried things like "main.Capture [ ( 'temp.bmp' , 10 , 10 , 100 , 100 ) ]" but I must have the syntax wrong...

2) This should basically be the same problem... If I have a window called 'Main' and want to print it with a preview and a dialog starting from coordinates 10,10 for a width of 100 and a height of 100 how would I write this statement? I have tried things like "main.Print [ ( .T., .T., 10 , 10 , 100 , 100 ) ]" but, again, I must have the syntax wrong...

I have some other questions, but I will break those into separate posts. Thanks again for any help you can provide...

Ralph
When your toolbox only contains a hammer, everything looks like a nail...
User avatar
gfilatov
Posts: 1116
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Contact:

Re: Syntax for Capture and Print

Post by gfilatov »

Hello Ralph,

You're welcome!

Kindly take a look for the following functions:

Code: Select all

*------------------------------------------------------------------------------*
FUNCTION SAVEWINDOW ( cWindowName , cFileName , nRow , nCol , nWidth , nHeight )
*------------------------------------------------------------------------------*
Local ntop , nleft , nbottom , nright

	if valtype ( cFileName ) = 'U'
		cFileName := GetStartupFolder() + '\' + cWindowName + '.bmp'
	endif

	if	valtype ( nRow ) = 'U' ;
		.or. ;
		valtype ( nCol ) = 'U' ;
		.or. ;
		valtype ( nWidth ) = 'U' ;
		.or. ;
		valtype ( nHeight ) = 'U' 

		ntop	:= -1
		nleft	:= -1
		nbottom	:= -1
		nright	:= -1

	else

		ntop	:= nRow
		nleft	:= nCol
		nbottom	:= nHeight + nRow
		nright	:= nWidth + nCol

	endif

	SAVEWINDOWBYHANDLE ( GetFormHandle ( cWindowName ) , cFileName , ntop , nleft , nbottom , nright )

RETURN NIL

*------------------------------------------------------------------------------*
FUNCTION PRINTWINDOW ( cWindowName , lPreview , ldialog , nRow , nCol , nWidth , nHeight )
*------------------------------------------------------------------------------*
LOCAL lSuccess
LOCAL TempName 
LOCAL W
LOCAL H
LOCAL HO
LOCAL VO
LOCAL bw , bh , r , tw , th , dc , wdif , hdif , dr
LOCAL ntop , nleft , nbottom , nright

memvar _hmg_printer_name
memvar _hmg_printer_aprinterproperties
memvar _hmg_printer_preview
memvar _hmg_printer_timestamp
memvar _hmg_printer_hdc_bak
memvar _hmg_printer_pagecount
memvar _hmg_printer_copies
memvar _hmg_printer_collate
memvar _hmg_printer_hdc
memvar _hmg_printer_JobName

	if	valtype ( nRow ) = 'U' ;
		.or. ;
		valtype ( nCol ) = 'U' ;
		.or. ;
		valtype ( nWidth ) = 'U' ;
		.or. ;
		valtype ( nHeight ) = 'U' 

		ntop	:= -1
		nleft	:= -1
		nbottom	:= -1
		nright	:= -1

	else

		ntop	:= nRow
		nleft	:= nCol
		nbottom	:= nHeight + nRow
		nright	:= nWidth + nCol

	endif

	if ValType ( lDialog ) = 'U'
		lDialog	:= .F.
	endif

	if ValType ( lPreview ) = 'U'
		lPreview := .F.
	endif

	if lDialog 

		IF lPreview
			SELECT PRINTER DIALOG TO lSuccess PREVIEW
		ELSE
			SELECT PRINTER DIALOG TO lSuccess 
		ENDIF

		IF ! lSuccess
			RETURN NIL
		ENDIF
	
	else

		IF lPreview
			SELECT PRINTER DEFAULT TO lSuccess PREVIEW
		ELSE
			SELECT PRINTER DEFAULT TO lSuccess 
		ENDIF

		IF ! lSuccess
			MSGMINIGUIERROR ( "Can't Init Printer" )
			RETURN NIL
		ENDIF

	endif

	IF ! _IsWIndowDefined ( cWindowName )
		MSGMINIGUIERROR ( 'Window Not Defined' )
		RETURN NIL
	ENDIF

	TempName := GetTempFolder() + '\_hmg_printwindow_' + alltrim(str(int(seconds()*100))) + '.bmp' 

	SAVEWINDOWBYHANDLE ( GetFormHandle ( cWindowName ) , TempName , ntop , nleft , nbottom , nright )

	HO := GETPRINTABLEAREAHORIZONTALOFFSET()
	VO := GETPRINTABLEAREAVERTICALOFFSET()

	W := GETPRINTABLEAREAWIDTH() - 10 - ( HO * 2 ) 
	H := GETPRINTABLEAREAHEIGHT() - 10 - ( VO * 2 ) 

	if ntop = -1

		bw := GetProperty ( cWindowName , 'Width' ) 
		bh := GetProperty ( cWindowName , 'Height' ) - GetTitleHeight ( GetFormHandle (cWindowName) )

	else

		bw := nright - nleft
		bh := nbottom - ntop

	endif


	r := bw / bh

	tw := 0
	th := 0

	do while .t.

		tw ++	
		th := tw / r 

		if tw > w .or. th > h
			exit
		endif

	enddo

	wdif := w - tw 

	if wdif > 0
		dc := wdif / 2
	else
		dc := 0
	endif

	hdif := h - th 

	if hdif > 0
		dr := hdif / 2
	else
		dr := 0
	endif

	START PRINTDOC

		START PRINTPAGE

			@ VO + 10 + ( ( h - th ) / 2 ) , HO + 10 + ( ( w - tw ) / 2 ) PRINT IMAGE TempName WIDTH tW HEIGHT tH 

		END PRINTPAGE

	END PRINTDOC

	DO EVENTS
	Ferase(TempName)

RETURN NIL


#pragma BEGINDUMP

#define HB_OS_WIN_32_USED
#define _WIN32_WINNT  0x0400

#include <shlobj.h>
#include <windows.h>
#include <winuser.h>
#include <wingdi.h>
#include "hbapi.h"
#include "hbvm.h"
#include "hbstack.h"
#include "hbapiitm.h"
#include "commctrl.h"

///////////////////////////////////////////////////////////////////////////////
// SAVE WINDOW (Based On Code Contributed by Ciro Vargas Clemov)
///////////////////////////////////////////////////////////////////////////////

HANDLE ChangeBmpFormat(HBITMAP , HPALETTE );
WORD GetDIBColors(LPSTR lpDIB) ;

HB_FUNC( SAVEWINDOWBYHANDLE ) 
{

	HWND				hWnd	= ( HWND ) hb_parnl( 1 );
	HDC				hDC	= GetDC( hWnd );
	HDC				hMemDC ;
	RECT				rc ;
	HBITMAP			hBitmap ;
	HBITMAP			hOldBmp ;
	HPALETTE			hPal;
	LPSTR				File	= hb_parc(2) ;
	HANDLE			hDIB ;
	int				top	= hb_parni(3) ;
	int				left	= hb_parni(4) ;
	int				bottom	= hb_parni(5) ;
	int				right	= hb_parni(6) ;
	BITMAPFILEHEADER		bmfHdr ;     
	LPBITMAPINFOHEADER	lpBI ;       
	HANDLE			filehandle ;         
	DWORD				dwDIBSize ;
	DWORD				dwWritten ;
	DWORD				dwBmBitsSize ;

	if ( top != -1 && left != -1 && bottom != -1 && right != -1 )
	{
		rc.top = top ;
		rc.left = left ;
		rc.bottom = bottom ;
		rc.right = right ;
	}
	else
	{
		GetClientRect( hWnd, &rc );
	}

	hMemDC  = CreateCompatibleDC( hDC );
	hBitmap = CreateCompatibleBitmap( hDC, rc.right-rc.left, rc.bottom-rc.top );
	hOldBmp = ( HBITMAP ) SelectObject( hMemDC, hBitmap );
	BitBlt( hMemDC, 0, 0 , rc.right-rc.left, rc.bottom-rc.top, hDC, rc.top, rc.left, SRCCOPY );
	SelectObject( hMemDC, hOldBmp );
	hDIB = ChangeBmpFormat(hBitmap ,hPal);

	filehandle = CreateFile( File , GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);

	lpBI = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
	if (!lpBI)
	{
		CloseHandle(filehandle);
		return;
	}

	if (lpBI->biSize != sizeof(BITMAPINFOHEADER))
	{
		GlobalUnlock(hDIB);
		CloseHandle(filehandle);
		return;
	}

	bmfHdr.bfType = ((WORD) ('M' << 8) | 'B'); 

	dwDIBSize = *(LPDWORD)lpBI + ( GetDIBColors( (LPSTR) lpBI ) * sizeof(RGBTRIPLE)) ;

	dwBmBitsSize = ((((lpBI->biWidth)*((DWORD)lpBI->biBitCount))+ 31) / 32 * 4) *  lpBI->biHeight;
	dwDIBSize += dwBmBitsSize;
	lpBI->biSizeImage = dwBmBitsSize;
                   
	bmfHdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
	bmfHdr.bfReserved1 = 0;
	bmfHdr.bfReserved2 = 0;

	bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + lpBI->biSize + ( GetDIBColors( (LPSTR) lpBI ) * sizeof(RGBTRIPLE)) ;

	WriteFile(filehandle, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
   
	WriteFile(filehandle, (LPSTR)lpBI, dwDIBSize, &dwWritten, NULL);

	GlobalUnlock(hDIB);
	CloseHandle(filehandle);

	DeleteDC( hMemDC );
	GlobalFree (hDIB);
	ReleaseDC( hWnd, hDC );

}

HANDLE ChangeBmpFormat(HBITMAP hBitmap, HPALETTE hPal) 
{

	BITMAP              bm;         
	BITMAPINFOHEADER    bi;         
	LPBITMAPINFOHEADER  lpbi;       
	DWORD               dwLen;      
	HGLOBAL             hDIB;
	HGLOBAL             h;
	HDC                 hDC;        
	WORD                biBits;     

	if (!hBitmap)
	{
		return 0 ;
	}

	if (!GetObject(hBitmap, sizeof(bm), (LPSTR)&bm))
	{
		return 0 ;
	}

	if ( hPal == 0 )
	{
		hPal = GetStockObject(DEFAULT_PALETTE);
	}

	biBits = bm.bmPlanes * bm.bmBitsPixel ;

	if (biBits <= 1)
	{
		biBits = 1;
	}
	else if (biBits <= 4)
	{
		biBits = 4;
	}
	else if (biBits <= 8)
	{
		biBits = 8;
	}
	else  
	{
	        biBits = 24;
	}

	bi.biSize = sizeof(BITMAPINFOHEADER);
	bi.biWidth = bm.bmWidth;
	bi.biHeight = bm.bmHeight;
	bi.biPlanes = 1;
	bi.biBitCount = biBits;
	bi.biCompression = BI_RGB;
	bi.biSizeImage = 0;
	bi.biXPelsPerMeter = 0;
	bi.biYPelsPerMeter = 0;
	bi.biClrUsed = 0;
	bi.biClrImportant = 0;

	dwLen = bi.biSize + ( GetDIBColors( (LPSTR)&bi ) * sizeof ( RGBTRIPLE ) ) ;

	hDC = GetDC(NULL);

	hPal = SelectPalette(hDC, hPal, FALSE);
	RealizePalette(hDC);

	hDIB = GlobalAlloc(GHND, dwLen);

	if (!hDIB)
	{
		SelectPalette(hDC, hPal, TRUE);
		RealizePalette(hDC);
		ReleaseDC(NULL, hDC);
		return 0;
	}

	lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

	*lpbi = bi;

	GetDIBits(hDC, hBitmap, 0, (UINT)bi.biHeight, NULL, (LPBITMAPINFO)lpbi,DIB_RGB_COLORS);

	bi = *lpbi;
	GlobalUnlock(hDIB);

	if (bi.biSizeImage == 0)
	{
		bi.biSizeImage = ((((DWORD)bm.bmWidth * biBits)+ 31) / 32 * 4) * bm.bmHeight;
	}

	dwLen = bi.biSize + ( GetDIBColors( (LPSTR)&bi ) * sizeof(RGBTRIPLE)) + bi.biSizeImage;

	h = GlobalReAlloc( hDIB, (SIZE_T) dwLen, 0);
	if ( h )
	{
		hDIB = h;
	}
	else
	{
		GlobalFree(hDIB);
		hDIB = NULL;
		SelectPalette(hDC, hPal, TRUE);
		RealizePalette(hDC);
		ReleaseDC(NULL, hDC);
		return hDIB;
	}

	lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

	if (GetDIBits(hDC, hBitmap, 0, (UINT)bi.biHeight, (LPSTR)lpbi + (WORD)lpbi->biSize + ( GetDIBColors ((LPSTR)lpbi) * sizeof(RGBTRIPLE)) , (LPBITMAPINFO)lpbi, DIB_RGB_COLORS) == 0)
	{
		GlobalFree(hDIB);
		hDIB = NULL;
		SelectPalette(hDC, hPal, TRUE);
		RealizePalette(hDC);
		ReleaseDC(NULL, hDC);
		return hDIB;
	}

	bi = *lpbi;

	GlobalUnlock(hDIB);
	SelectPalette(hDC, hPal, TRUE);
	RealizePalette(hDC);
	ReleaseDC(NULL, hDC);

	return hDIB;

}

WORD GetDIBColors(LPSTR lpDIB)
{
    WORD wBitCount = ((LPBITMAPCOREHEADER)lpDIB)->bcBitCount; ;
    return wBitCount;
}

#pragma ENDDUMP
I hope that give you an idea. :idea:
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein
RalphMDS
Posts: 21
Joined: Wed Mar 25, 2009 1:24 pm
Location: New Jersey, U.S.A.

Re: Syntax for Capture and Print

Post by RalphMDS »

Dear Grigory,

Thank you for the quick reply. I am sure that everything you posted will work to give me the same result as what I was trying to do, but the in the documentation I downloaded (in this case, hmg_help.chm that I found through this site), seems to indicate that the functions are already built in. When I call either Print or Capture without any parameters they work fine for me. My problem is I can't figure out how to pass parameters to them correctly.

I may end up using the solution you provided. I would just like to understand what I am doing wrong.

Thanks,

Ralph
When your toolbox only contains a hammer, everything looks like a nail...
RalphMDS
Posts: 21
Joined: Wed Mar 25, 2009 1:24 pm
Location: New Jersey, U.S.A.

Re: Syntax for Capture and Print

Post by RalphMDS »

Dear Grigory,

I did some testing with the code and now :idea: I understand. Thank you for your help. It was exactly what I needed.

For anyone else that may search for this info in the future, there are two functions built into HMG that allow you to save and print a window respectively. The code that Grigory was nice enough to post is already in there and does not need to be included in your program.

To save a window, call the function

SAVEWINDOW ( cWindowName , cFileName , nRow , nCol , nWidth , nHeight )

where

cWindowName is a character string containing the name of the window to save
cFileName is a character string containing the file name to save in.
nRow is a numeric of the starting window row (without the title bar) to begin saving from
nCol is a numeric of the starting window column to begin saving from
nWidth is a numeric of the width to save
nHeight is a numeric of the height to save

If you just give a window name and filename, it will save a BMP of the entire window.
The function saves as a BMP, so be sure to use the appropriate extension for the file name


To print a window call the function

PRINTWINDOW ( cWindowName , lPreview , ldialog , nRow , nCol , nWidth , nHeight )

where

cWindowName is a character string containing the name of the window to print
lPreview is a logical set to .T. if you want a print preview
ldialog is a logical set to .T. if you want a printer selection dialog
nRow is a numeric of the starting window row (without the title bar) to begin printing from
nCol is a numeric of the starting window column to begin printing from
nWidth is a numeric of the width to print
nHeight is a numeric of the height to print

If you just give a window name, it will print the entire window (less title bar) to the default printer.

Hope this helps someone else in the future...

Ralph
When your toolbox only contains a hammer, everything looks like a nail...
User avatar
Rathinagiri
Posts: 5482
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Syntax for Capture and Print

Post by Rathinagiri »

Thanks a lot Ralph. Really helpful.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Syntax for Capture and Print - I can not compile

Post by Pablo César »

Dear gentlemen, I can not find what I´m doing wrong. I can not compile it in IDE of Hmg.3.0.39:

Code: Select all

/*
  Source code name: Demo3.prg
  Author..........: Mr. Grigory Filatov
  Contributions...: Mr. Ciro Vargas Clemov
  Adaptations.....: Pablo César Arrascaeta
*/
#include "minigui.ch"

Function Main
DEFINE WINDOW Form_1 ;
	AT 0,0 ;
	WIDTH 640 ;
	HEIGHT 480 ;
	TITLE 'Hello World!' ;
	MAIN

	DEFINE BUTTON Button_1
		ROW     10
		COL     30
		WIDTH  280
		HEIGHT 26
		ACTION {||TaskBarPos ()}
		CAPTION "Task Bar Position"
		FONTNAME "Arial"
		FONTSIZE 9
		TOOLTIP ""
		FONTBOLD .F.
		FONTITALIC .F.
		FONTUNDERLINE .F.
		FONTSTRIKEOUT .F.
		ONGOTFOCUS Nil
		ONLOSTFOCUS Nil
		HELPID Nil
		FLAT .F.
		TABSTOP .T.
		VISIBLE .T.
		TRANSPARENT .F.
		MULTILINE .F.
		PICTURE NIL
		PICTALIGNMENT LEFT
    END BUTTON
	
END WINDOW
CENTER WINDOW Form_1
ACTIVATE WINDOW Form_1    
Return Nil

Static Function TaskBarPos ()
Local aPos := GetTaskBarPosition()

MsgInfo(                                             ;
         "TOP="+str(aPos[1],4)+Chr(13)+Chr(10)+      ;
         "LEFT="+str(aPos[2],4)+Chr(13)+Chr(10)+     ;
         "BOTTOM="+str(aPos[3],4)+Chr(13)+Chr(10)+   ;
         "RIGHT="+str(aPos[4],4) )
SAVEWINDOW( "Form_1" , "Teste.bmp" , aPos[1] , aPos[2] , aPos[3] , aPos[4] )
Return Nil

FUNCTION SAVEWINDOW( cWindowName , cFileName , nRow , nCol , nWidth , nHeight )
Local ntop , nleft , nbottom , nright

   if valtype ( cFileName ) = 'U'
      cFileName := GetStartupFolder() + '\' + cWindowName + '.bmp'
   endif

   if   valtype ( nRow ) = 'U' ;
      .or. ;
      valtype ( nCol ) = 'U' ;
      .or. ;
      valtype ( nWidth ) = 'U' ;
      .or. ;
      valtype ( nHeight ) = 'U' 

      ntop   := -1
      nleft   := -1
      nbottom   := -1
      nright   := -1

   else

      ntop   := nRow
      nleft   := nCol
      nbottom   := nHeight + nRow
      nright   := nWidth + nCol

   endif

   SAVEWINDOWBYHANDLE ( GetFormHandle ( cWindowName ) , cFileName , ntop , nleft , nbottom , nright )

RETURN NIL

FUNCTION PRINTWINDOW ( cWindowName , lPreview , ldialog , nRow , nCol , nWidth , nHeight )
LOCAL lSuccess
LOCAL TempName 
LOCAL W
LOCAL H
LOCAL HO
LOCAL VO
LOCAL bw , bh , r , tw , th , dc , wdif , hdif , dr
LOCAL ntop , nleft , nbottom , nright

memvar _hmg_printer_name
memvar _hmg_printer_aprinterproperties
memvar _hmg_printer_preview
memvar _hmg_printer_timestamp
memvar _hmg_printer_hdc_bak
memvar _hmg_printer_pagecount
memvar _hmg_printer_copies
memvar _hmg_printer_collate
memvar _hmg_printer_hdc
memvar _hmg_printer_JobName

   if   valtype ( nRow ) = 'U' ;
      .or. ;
      valtype ( nCol ) = 'U' ;
      .or. ;
      valtype ( nWidth ) = 'U' ;
      .or. ;
      valtype ( nHeight ) = 'U' 

      ntop   := -1
      nleft   := -1
      nbottom   := -1
      nright   := -1

   else

      ntop   := nRow
      nleft   := nCol
      nbottom   := nHeight + nRow
      nright   := nWidth + nCol

   endif

   if ValType ( lDialog ) = 'U'
      lDialog   := .F.
   endif

   if ValType ( lPreview ) = 'U'
      lPreview := .F.
   endif

   if lDialog 

      IF lPreview
         SELECT PRINTER DIALOG TO lSuccess PREVIEW
      ELSE
         SELECT PRINTER DIALOG TO lSuccess 
      ENDIF

      IF ! lSuccess
         RETURN NIL
      ENDIF
   
   else

      IF lPreview
         SELECT PRINTER DEFAULT TO lSuccess PREVIEW
      ELSE
         SELECT PRINTER DEFAULT TO lSuccess 
      ENDIF

      IF ! lSuccess
         MSGMINIGUIERROR ( "Can't Init Printer" )
         RETURN NIL
      ENDIF

   endif

   IF ! _IsWIndowDefined ( cWindowName )
      MSGMINIGUIERROR ( 'Window Not Defined' )
      RETURN NIL
   ENDIF

   TempName := GetTempFolder() + '\_hmg_printwindow_' + alltrim(str(int(seconds()*100))) + '.bmp' 

   SAVEWINDOWBYHANDLE ( GetFormHandle ( cWindowName ) , TempName , ntop , nleft , nbottom , nright )

   HO := GETPRINTABLEAREAHORIZONTALOFFSET()
   VO := GETPRINTABLEAREAVERTICALOFFSET()

   W := GETPRINTABLEAREAWIDTH() - 10 - ( HO * 2 ) 
   H := GETPRINTABLEAREAHEIGHT() - 10 - ( VO * 2 ) 

   if ntop = -1

      bw := GetProperty ( cWindowName , 'Width' ) 
      bh := GetProperty ( cWindowName , 'Height' ) - GetTitleHeight ( GetFormHandle (cWindowName) )

   else

      bw := nright - nleft
      bh := nbottom - ntop

   endif


   r := bw / bh

   tw := 0
   th := 0

   do while .t.

      tw ++   
      th := tw / r 

      if tw > w .or. th > h
         exit
      endif

   enddo

   wdif := w - tw 

   if wdif > 0
      dc := wdif / 2
   else
      dc := 0
   endif

   hdif := h - th 

   if hdif > 0
      dr := hdif / 2
   else
      dr := 0
   endif

   START PRINTDOC

      START PRINTPAGE

         @ VO + 10 + ( ( h - th ) / 2 ) , HO + 10 + ( ( w - tw ) / 2 ) PRINT IMAGE TempName WIDTH tW HEIGHT tH 

      END PRINTPAGE

   END PRINTDOC
   DO EVENTS
   Ferase(TempName)
RETURN NIL


#pragma BEGINDUMP

#define HB_OS_WIN_32_USED
#define _WIN32_WINNT  0x0400

#include <shlobj.h>
#include <windows.h>
#include <winuser.h>
#include <wingdi.h>
#include "hbapi.h"
#include "hbvm.h"
#include "hbstack.h"
#include "hbapiitm.h"
#include "commctrl.h"

///////////////////////////////////////////////////////////////////////////////
// SAVE WINDOW (Based On Code Contributed by Ciro Vargas Clemov)
///////////////////////////////////////////////////////////////////////////////

HANDLE ChangeBmpFormat(HBITMAP , HPALETTE );
WORD GetDIBColors(LPSTR lpDIB) ;

HB_FUNC( SAVEWINDOWBYHANDLE ) 
{

   HWND                 hWnd   = ( HWND ) hb_parnl( 1 );
   HDC                  hDC   = GetDC( hWnd );
   HDC                  hMemDC ;
   RECT                 rc ;
   HBITMAP              hBitmap ;
   HBITMAP              hOldBmp ;
   HPALETTE             hPal;
   LPSTR                File   = hb_parc(2) ;
   HANDLE               hDIB ;
   int                  top   = hb_parni(3) ;
   int                  left   = hb_parni(4) ;
   int                  bottom   = hb_parni(5) ;
   int                  right   = hb_parni(6) ;
   BITMAPFILEHEADER     bmfHdr ;     
   LPBITMAPINFOHEADER   lpBI ;       
   HANDLE               filehandle ;         
   DWORD                dwDIBSize ;
   DWORD                dwWritten ;
   DWORD                dwBmBitsSize ;

   if ( top != -1 && left != -1 && bottom != -1 && right != -1 )
   {
      rc.top = top ;
      rc.left = left ;
      rc.bottom = bottom ;
      rc.right = right ;
   }
   else
   {
      GetClientRect( hWnd, &rc );
   }

   hMemDC  = CreateCompatibleDC( hDC );
   hBitmap = CreateCompatibleBitmap( hDC, rc.right-rc.left, rc.bottom-rc.top );
   hOldBmp = ( HBITMAP ) SelectObject( hMemDC, hBitmap );
   BitBlt( hMemDC, 0, 0 , rc.right-rc.left, rc.bottom-rc.top, hDC, rc.top, rc.left, SRCCOPY );
   SelectObject( hMemDC, hOldBmp );
   hDIB = ChangeBmpFormat(hBitmap ,hPal);

   filehandle = CreateFile( File , GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);

   lpBI = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
   if (!lpBI)
   {
      CloseHandle(filehandle);
      return;
   }

   if (lpBI->biSize != sizeof(BITMAPINFOHEADER))
   {
      GlobalUnlock(hDIB);
      CloseHandle(filehandle);
      return;
   }

   bmfHdr.bfType = ((WORD) ('M' << 8) | 'B'); 

   dwDIBSize = *(LPDWORD)lpBI + ( GetDIBColors( (LPSTR) lpBI ) * sizeof(RGBTRIPLE)) ;

   dwBmBitsSize = ((((lpBI->biWidth)*((DWORD)lpBI->biBitCount))+ 31) / 32 * 4) *  lpBI->biHeight;
   dwDIBSize += dwBmBitsSize;
   lpBI->biSizeImage = dwBmBitsSize;
                   
   bmfHdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
   bmfHdr.bfReserved1 = 0;
   bmfHdr.bfReserved2 = 0;

   bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + lpBI->biSize + ( GetDIBColors( (LPSTR) lpBI ) * sizeof(RGBTRIPLE)) ;

   WriteFile(filehandle, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
   
   WriteFile(filehandle, (LPSTR)lpBI, dwDIBSize, &dwWritten, NULL);

   GlobalUnlock(hDIB);
   CloseHandle(filehandle);

   DeleteDC( hMemDC );
   GlobalFree (hDIB);
   ReleaseDC( hWnd, hDC );

}

HANDLE ChangeBmpFormat(HBITMAP hBitmap, HPALETTE hPal) 
{

   BITMAP              bm;         
   BITMAPINFOHEADER    bi;         
   LPBITMAPINFOHEADER  lpbi;       
   DWORD               dwLen;      
   HGLOBAL             hDIB;
   HGLOBAL             h;
   HDC                 hDC;        
   WORD                biBits;     

   if (!hBitmap)
   {
      return 0 ;
   }

   if (!GetObject(hBitmap, sizeof(bm), (LPSTR)&bm))
   {
      return 0 ;
   }

   if ( hPal == 0 )
   {
      hPal = GetStockObject(DEFAULT_PALETTE);
   }

   biBits = bm.bmPlanes * bm.bmBitsPixel ;

   if (biBits <= 1)
   {
      biBits = 1;
   }
   else if (biBits <= 4)
   {
      biBits = 4;
   }
   else if (biBits <= 8)
   {
      biBits = 8;
   }
   else  
   {
           biBits = 24;
   }

   bi.biSize = sizeof(BITMAPINFOHEADER);
   bi.biWidth = bm.bmWidth;
   bi.biHeight = bm.bmHeight;
   bi.biPlanes = 1;
   bi.biBitCount = biBits;
   bi.biCompression = BI_RGB;
   bi.biSizeImage = 0;
   bi.biXPelsPerMeter = 0;
   bi.biYPelsPerMeter = 0;
   bi.biClrUsed = 0;
   bi.biClrImportant = 0;

   dwLen = bi.biSize + ( GetDIBColors( (LPSTR)&bi ) * sizeof ( RGBTRIPLE ) ) ;

   hDC = GetDC(NULL);

   hPal = SelectPalette(hDC, hPal, FALSE);
   RealizePalette(hDC);

   hDIB = GlobalAlloc(GHND, dwLen);

   if (!hDIB)
   {
      SelectPalette(hDC, hPal, TRUE);
      RealizePalette(hDC);
      ReleaseDC(NULL, hDC);
      return 0;
   }

   lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

   *lpbi = bi;

   GetDIBits(hDC, hBitmap, 0, (UINT)bi.biHeight, NULL, (LPBITMAPINFO)lpbi,DIB_RGB_COLORS);

   bi = *lpbi;
   GlobalUnlock(hDIB);

   if (bi.biSizeImage == 0)
   {
      bi.biSizeImage = ((((DWORD)bm.bmWidth * biBits)+ 31) / 32 * 4) * bm.bmHeight;
   }

   dwLen = bi.biSize + ( GetDIBColors( (LPSTR)&bi ) * sizeof(RGBTRIPLE)) + bi.biSizeImage;

   h = GlobalReAlloc( hDIB, (SIZE_T) dwLen, 0);
   if ( h )
   {
      hDIB = h;
   }
   else
   {
      GlobalFree(hDIB);
      hDIB = NULL;
      SelectPalette(hDC, hPal, TRUE);
      RealizePalette(hDC);
      ReleaseDC(NULL, hDC);
      return hDIB;
   }

   lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

   if (GetDIBits(hDC, hBitmap, 0, (UINT)bi.biHeight, (LPSTR)lpbi + (WORD)lpbi->biSize + ( GetDIBColors ((LPSTR)lpbi) * sizeof(RGBTRIPLE)) , (LPBITMAPINFO)lpbi, DIB_RGB_COLORS) == 0)
   {
      GlobalFree(hDIB);
      hDIB = NULL;
      SelectPalette(hDC, hPal, TRUE);
      RealizePalette(hDC);
      ReleaseDC(NULL, hDC);
      return hDIB;
   }

   bi = *lpbi;

   GlobalUnlock(hDIB);
   SelectPalette(hDC, hPal, TRUE);
   RealizePalette(hDC);
   ReleaseDC(NULL, hDC);

   return hDIB;

}

WORD GetDIBColors(LPSTR lpDIB)
{
    WORD wBitCount = ((LPBITMAPCOREHEADER)lpDIB)->bcBitCount; ;
    return wBitCount;
}

HB_FUNC( GETTASKBARPOSITION )
{
   APPBARDATA pBarData     ;
   pBarData.cbSize = sizeof(pBarData);
   SHAppBarMessage( ABM_GETTASKBARPOS, &pBarData );
   hb_reta( 4 );
   hb_storvnl( pBarData.rc.top, -1, 1 );
   hb_storvnl( pBarData.rc.left, -1, 2 );
   hb_storvnl( pBarData.rc.bottom, -1, 3 );
   hb_storvnl( pBarData.rc.right, -1, 4 );
}

#pragma ENDDUMP
Please note error log:
Harbour 3.1.0dev (Rev. 17036)
Copyright (c) 1999-2011, http://harbour-project.org/
C:\Fontes\HMG\PrintScreen\demo3.prg: In function 'HB_FUN_SAVEWINDOWBYHANDLE':
C:\Fontes\HMG\PrintScreen\demo3.prg:276:34: warning: initialization discards qualifiers from pointer target type
C:\Fontes\HMG\PrintScreen\demo3.prg:306:9: warning: 'hPal' is used uninitialized in this function
C:/hmg.3.0.39/lib/libhmg.a(h_windows.o):h_windows.c:(.text+0x140): multiple definition of `HB_FUN_PRINTWINDOW'
C:/DOCUME~1/PABLOC~1/CONFIG~1/Temp/hbmk_1si9hp.dir/demo3.o:demo3.c:(.text+0xb0): first defined here
C:/hmg.3.0.39/lib/libhmg.a(h_windows.o):h_windows.c:(.text+0x160): multiple definition of `HB_FUN_SAVEWINDOW'
C:/DOCUME~1/PABLOC~1/CONFIG~1/Temp/hbmk_1si9hp.dir/demo3.o:demo3.c:(.text+0xf0): first defined here
C:/hmg.3.0.39/lib/libhmg.a(c_windows.o):c_windows.c:(.text+0x31a0): multiple definition of `ChangeBmpFormat'
C:/DOCUME~1/PABLOC~1/CONFIG~1/Temp/hbmk_1si9hp.dir/demo3.o:demo3.c:(.text+0x170): first defined here
C:/hmg.3.0.39/lib/libhmg.a(c_windows.o):c_windows.c:(.text+0x3540): multiple definition of `HB_FUN_SAVEWINDOWBYHANDLE'
C:/DOCUME~1/PABLOC~1/CONFIG~1/Temp/hbmk_1si9hp.dir/demo3.o:demo3.c:(.text+0x530): first defined here
C:/hmg.3.0.39/lib/libhmg.a(c_windows.o):c_windows.c:(.text+0x3810): multiple definition of `GetDIBColors'
C:/DOCUME~1/PABLOC~1/CONFIG~1/Temp/hbmk_1si9hp.dir/demo3.o:demo3.c:(.text+0x840): first defined here
C:/DOCUME~1/PABLOC~1/CONFIG~1/Temp/hbmk_1si9hp.dir/demo3.o:demo3.c:(.data+0x1c8): undefined reference to `HB_FUN_MSGMINIGUIERROR'
collect2: ld returned 1 exit status
hbmk2: Erro: Executando linkeditor. 1
gcc.exe C:/DOCUME~1/PABLOC~1/CONFIG~1/Temp/hbmk_1si9hp.dir/demo3.o C:/DOCUME~1/PABLOC~1/CONFIG~1/Temp/hbmk_1si9hp.dir/hbmk_nmf5hf.o C:/Fontes/HMG/PrintScreen/_temp.o -mwindows -Wl,--start-group -lhmg -lcrypt -ledit -leditex -lgraph -lhfcl -lini -lreport -lmsvfw32 -lvfw32 -lhbct -lhbwin -lhbmzip -lminizip -lhbmemio -lhbmisc -lhbmysql -lmysql -lhbtip -lsqlite3 -lhbsqlit3 -lsddodbc -lrddsql -lsddmy -lhbodbc -lodbc32 -lhbhpdf -lhbfimage -lhbpgsql -lhbnetio -lhbextern -lhbdebug -lhbvm -lhbrtl -lhblang -lhbcpage -lgtcgi -lgtpca -lgtstd -lgtwin -lgtwvt -lgtgui -lhbrdd -lhbuddall -lhbusrrdd -lrddntx -lrddcdx -lrddnsx -lrddfpt -lhbrdd -lhbhsx -lhbsix -lhbmacro -lhbcplr -lhbpp -lhbcommon -lhbmainwin -lkernel32 -luser32 -lgdi32 -ladvapi32 -lws2_32 -lwinspool -lcomctl32 -lcomdlg32 -lshell32 -luuid -lole32 -loleaut32 -lmpr -lwinmm -lmapi32 -limm32 -lmsimg32 -lwininet -lhbpcre -lhbzlib -Wl,--end-group -odemo3.exe -LC:/hmg.3.0.39/harbour/lib/win/mingw -LC:/hmg.3.0.39/harbour/bin -LC:/hmg.3.0.39/lib
I have wrote his in my message before re-edited, please msg below be disregarded what I wrote:Ahhh another think... I saw something strange in this lines:

Code: Select all

 HB_FUNC( WIN2BM )
 {
   HWND    hWnd   = ISNUM( 1 ) ? (HWND) hb_parnl( 1 ) : ISPOINTER( 1 ) ?
                    (HWND) hb_parptr( 1 ) : GetForegroundWindow();
   UINT    iWhat  = ISLOG( 2 ) ? ( hb_parl( 2 ) ? 1 : 2 ) : 0;
   CHAR  * pszBmF = ISCHAR( 3 ) && hb_parclen( 3 ) ? hb_parc( 3 ) : NULL;
   HDC     hdcWin = NULL;
   HDC     hdcMem = NULL;
   HBITMAP hbmWin = NULL;
   RECT    rc;
When says UINT it means should be UNIT, correct ? And what are those "?" after ISNUM...
I have posted other codes wich I also trying to compile. Sorry my mistake.
Last edited by Pablo César on Fri Oct 28, 2011 11:55 am, edited 3 times in total.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
Rathinagiri
Posts: 5482
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Syntax for Capture and Print

Post by Rathinagiri »

Though I could not help you as such,

UINT is Unsigned INTeger.

? after IsNumber() is short form of IF THEN ELSE ENDIF.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Re: Syntax for Capture and Print

Post by Pablo César »

Thank you Mr. Rathinagiri for your prompt replying and help. I will remake my wrongly corrections and I will check it in this morning (now here is midnight). But I do not believe that corrections could disapears most of errors. I will revert with results. Have you a nice day ! And many thank you again...
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Re: Syntax for Capture and Print

Post by Pablo César »

Dear Mr. Rathinagiri and Mr. Grigory Filatov, sorry I have mismatched with codes in my first message, but I have redited-it, so please be kind to review it. Because I still can not compile with out errors.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
Post Reply