Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto Cropping 3

Status
Not open for further replies.

TariqMehmod

Programmer
Mar 4, 2004
100
PK
ear Experts

I have following excel file

aaa_luwzwu.png


I want to create an image based on usedcells (B3:J22)
Is it possible to crop required range then create an image.

I think GDIPLUS can do this, so I found these links but still confuse how to do this.


The codes in this link crop whole screen but not required area

Please help
 
Sir I have modified this portion

Code:
 \ DEFINE CLASS cmdMyCmdBtn AS CommandButton
\ Caption = '\<Quit'
\ Cancel = .T.
\ Left = 125
\ Top = 520
\ Height = 25
\ visible = .t.

\ PROCEDURE Click
\ this.visible=.f.
\ #INCLUDE gpImage.h

\ IF Not "gpImage" $ SET("Procedure")
\ SET PROCEDURE TO gpImage ADDITIVE
\ ENDIF

\ gdip = CREATEOBJECT("gpInit")
\ img = CREATEOBJECT("gpImage")
\ img.Capture(Thisform.HWnd)
\ LOCAL lnTitleHeight, lnLeftBorder, lnTopBorder
\ lnTitleHeight = SYSMETRIC(9)
\ lnLeftBorder = SYSMETRIC(3)
\ lnTopBorder = SYSMETRIC(4)
\ Img.Crop(lnLeftBorder, lnTitleHeight + lnTopBorder, ;
\ Img.ImageWidth - (lnLeftBorder * 2), ;
\ Img.ImageHeight - (lnTitleHeight + (lnTopBorder * 2)))
\ Img.SaveasBMP("Captured_Clean")
\ Img = NULL
\ Gdip = NULL
\ erase (m.lcTempClass)
\ clear events
\ release thisform

\ ENDDEFINE]

added this line

Code:
 \ erase (m.lcTempClass)

As a result no there are only *.fxp files

ddd_wdvnt8.png


these lines create only *.prg files

Code:
 lcTempClass = Sys(2015)+'.prg'
Strtofile(myVar, lcTempClass)
Compile (lcTempClass)]


so this line

Code:
 erase (m.lcTempClass)

erase only *.prg files

I am amazing what procedure creates *.fxp files.

How to stop creating fxp files every time?

Please
 
You can't, an fxp file is a compiled version of the prg.

This line creates them:
Code:
Compile (lcTempClass)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
If erase(m.lcTempClass) deletes the .prg file, you only need to do this:

Code:
erase(m.lcTempClass)
erase(strtran(m.lcTempClass,".prg",".fxp"))

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Looking at your code in more detail, you seem to be generating the text of a class definition, storing it in a variable (MyVar), writing that variable to a PRG file, compiling the PRG (which, by the way, is where the FXP comes from), referencing the class definition to create your form object, and finally erasing the PRG (and the FXP?).

Am I missing something here? Is there any reason to use such a convoluted approach? Why not simply put the class definitions in the original program?

But if you must generate the PRG and FXP like that, it would be better to place them in the user's Temp folder rather than the default folder, which is what you are doing. That way, the files will eventually get deleted during the user's normal cleanup routine.

In other words, instead of:

Code:
lcTempClass = Sys(2015)+'.prg'

you could do this:

Code:
lcTempClass = FULLPATH(Sys(2015)+'.prg', SYS(2023))

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thank SIR Mike Lewis and GriffMG.

About 99% portion has been completed due to your kind help.

The forms with Excel file appears, when I press commandbuton then an image is created successfully.

But now I want to make it auto, the enduser must not click commandbutton.

For this I add a timer onto form. When forms load then after 10 seconds, the timer is fired to click commnadbutton.

Form loads, timer firs but but this error occurs
ddd_temvwy.png


It means timer searches form but could not locate the commandbutton.

I think this line needs modification
Code:
\ m.oForm.cmdMyCmdBtn.click

Whole codes are below

Code:
 * getting desktop path
loShell = CreateObject ('WScript.Shell')
lcDesktop = loShell.SpecialFolders('desktop')


* select file
lcXLS = Getfile('XLSx')
*lcXLS =[C:\xls\Daily.xlsx]
Set Textmerge To Memvar myVar Noshow
Set Textmerge On

\ DEFINE CLASS oleExcelObject as OLEControl
\ OleClass ="Excel.Sheet"  && Server name
\ OleTypeAllowed = 0      && Linked
\ DocumentFile = "<<m.lcXLS>>" && This file should exist
\ ENDDEFINE


\ DEFINE CLASS cmdMyCmdBtn AS CommandButton
\ Caption = '\<Close'
\ Cancel = .T.
\ Left = 125
\ Top = 520
\ Height = 25
\ visible = .t.

\ PROCEDURE Click
\ this.visible=.f.
\ #INCLUDE gpImage.h

\ IF Not "gpImage" $ SET("Procedure")
\ SET PROCEDURE TO gpImage ADDITIVE
\ ENDIF

\ gdip = CREATEOBJECT("gpInit")
\ img = CREATEOBJECT("gpImage")
\ img.Capture(Thisform.HWnd)
\ LOCAL lnTitleHeight, lnLeftBorder, lnTopBorder
\ lnTitleHeight = SYSMETRIC(9)
\ lnLeftBorder = SYSMETRIC(3)
\ lnTopBorder = SYSMETRIC(4)
\ Img.Crop(lnLeftBorder, lnTitleHeight + lnTopBorder, ;
\ Img.ImageWidth - (lnLeftBorder * 2), ;
\ Img.ImageHeight - (lnTitleHeight + (lnTopBorder * 2)))
\ *Img.SaveasBMP("Dash")
\ Img.SaveasBMP(lcDesktop+'\'+"Dash")
\ Img = NULL
\ Gdip = NULL
\ release like _5
\ erase (m.lcTempClass)
\ clear events
\ release thisform

\ ENDDEFINE


\ DEFINE CLASS timer1 AS timer							&& timer
\ interval=5000
\ PROCEDURE timer
\ m.oForm.cmdMyCmdBtn.click
\ ENDDEFINE




Set Textmerge To
Set Textmerge Off


lcTempClass = FULLPATH(Sys(2015)+'.prg', SYS(2023)) 	&& temp folder
Strtofile(myVar, lcTempClass)
Compile (lcTempClass)

Public oForm
oForm = Createobject("Form")

With oForm

	.Height = 550
	.Width = 360
	.autocenter=.t.
	.caption=[Dashboard]
	.minbutton=.f.
	.maxbutton=.f.
	.alwaysontop=.t.

	.Newobject("ExcelObject","oleExcelObject",lcTempClass)  && Add OLE object
	With .ExcelObject
		.Left = 5
		.Top = 5
		.Width = .Parent.Width - 10
		.Height = .Parent.Height - 10
		.Visible = .T.
	Endwith

	.Newobject("commandbutton","cmdMyCmdBtn",lcTempClass)  	&& Add command button
	.Newobject("timer","timer1",lcTempClass)  				&& Timer class

Endwith

oForm.Show
Read Events

Clear Class 'oleExcelObject'
*delete file (m.lcTempClass)


*Erase (Juststem(m.lcTempClass)+'.*')

*RELEASE memvar like *.prg
*Erase (Juststem(lcTempClass)+'*.*') 
*Erase ('_5WM*.*')

Please
 
At the time the class is declared, m.oForm.cmdMyCmdBtn.click does not exist.

It only exists OUTSIDE the class definition.

I am not sure at all how you would go about firing the click() from your CommandButton class from within a new timer class.

Perhaps using a reference to the timer class's parent?

Code:
this.parent.commandbutton.click()

or maybe

Code:
this.parent.cmdMyCmdBtn.click()



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Thank SIR Mike Lewis and GriffMG.

These codes did the perfect job, no issue

Code:
 \ DEFINE CLASS timer1 AS timer
\ interval=5000
\ PROCEDURE timer
\ this.parent.commandbutton.click()  
\ ENDDEFINE

Now form closes automatically without user involvement.
Image file is also created properly.

BUT

An other strange behavior appeared

I put all codes in a prg, when I run this prg outside EXE then no issue
But when I call this prg within EXE like this

Code:
 DO excel_on_form

Then the form that prg creates does not appears, mouse glass appear for some seconds and as a result a blank image file is saved like this one

[URL unfurl="true"]https://res.cloudinary.com/engineering-com/image/upload/v1604997872/tips/Dash_cl4rvc.bmp[/url]

I am amazing the codes work fine in development mode but not in exe.

I am call the prg from a FORM that has following properties
ShowWindow= 2- as top level form
WindowState=0- Normal
WindowType=0-Modeles

Please help me at this very last stage

With Regards
 
I'm amazed that worked, it was a blind guess.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Tariq, I wish you would read my above post date-stamped 9 Nov 20 16:25. I think the root of all your problems is that you are trying to generate and compile a program file at run time, rather than simply writing a program in the normal way.

And now you are adding a command button for no reason other than to execute its Click method from a timer. This doesn't make sense. Instead of this:

Code:
PROCEDURE Click
   * code here to capture the image
ENDEFINE
*****
PROCEDURE timer
  this.parent.commandbutton.click()  
ENDDEFINE

it makes more sense to drop the command button completely and just do this:

Code:
PROCEDURE timer
  * code here to capture the image  
ENDDEFINE

In short, the whole thing is unnecessarily complicated. If I was in your shoes, I would tear it up and start again, keeping in mind the points I have just made.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Sir Mike Lewis, all problem related to make my form automatic working has been solved. No issue but form does not appear in EXE.

If you think my way is not legal then no matter I forgot everything and restart work with FORM because it will be more easy and better.

In my project I have add a FORM and now trying to drop OLECONTROL. Please guide me what control or class should I use to view EXCEL file onto it?

bb_fn2evu.png


With Regards
 
Sir Tariq, I am not saying that your way is not legal, only that it is too complicated. In programming, that which is simplest is usually the best.

Also, I didn't say that you should start again, but rather that I would start again if these circumstances. For you to start again, you will have a whole lot of new stuff to learn - as demonstrated in your previous post.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Tariq,

This is what I have in mind. Basically I have taken all your code, and removed all the stuff about generating and compiling the PRG at run time. I haven't been able to test it, because I don't have the imaging control. But although I don't guarantee that it will work first time, I think it will be much easier for you to get working that your original approach.

Code:
* getting desktop path
loShell = CREATEOBJECT ('WScript.Shell')
lcDesktop = loShell.SpecialFolders('desktop')

* select file
lcXLS = GETFILE('XLSx')
IF EMPTY(lcXLS)
    * user has cancelled
    RETURN
ENDIF

    LOCAL oForm
    oForm = CREATEOBJECT("Form")

    WITH oForm

        .HEIGHT = 550
        .WIDTH = 360
        .AUTOCENTER=.T.
        .CAPTION=[Dashboard]
        .MINBUTTON=.F.
        .MAXBUTTON=.F.
        .ALWAYSONTOP=.T.

        .NEWOBJECT("ExcelObject","oleExcelObject")  && Add OLE object
        WITH .ExcelObject
            .LEFT = 5
            .TOP = 5
            .WIDTH = .PARENT.WIDTH - 10
            .HEIGHT = .PARENT.HEIGHT - 10
            .VISIBLE = .T.
        ENDWITH

        .NEWOBJECT("Timer1","oTimer")  		


    oForm.SHOW(1)  && form is modal, so no need for READ EVENTS
ENDWITH

DEFINE CLASS oleExcelObject AS OLECONTROL
    OLECLASS ="Excel.Sheet"  && Server name
    OLETYPEALLOWED = 0      && Linked
    DOCUMENTFILE = lcXLS && This file should exist
ENDDEFINE

DEFINE CLASS oTimer AS TIMER							&& timer
    INTERVAL=5000

    PROCEDURE TIMER
    THIS.ENABLED = .F.
    DO ScreenShot
    THIS.ENABLED = .T.
ENDDEFINE

PROCEDURE ScreenShot

#INCLUDE gpImage.h

IF NOT "gpImage" $ SET("Procedure")
    SET PROCEDURE TO gpImage ADDITIVE
ENDIF

GDIP = CREATEOBJECT("gpInit")
img = CREATEOBJECT("gpImage")
img.Capture(THISFORM.HWND)
LOCAL lnTitleHeight, lnLeftBorder, lnTopBorder
lnTitleHeight = SYSMETRIC(9)
lnLeftBorder = SYSMETRIC(3)
lnTopBorder = SYSMETRIC(4)
img.Crop(lnLeftBorder, lnTitleHeight + lnTopBorder, ;
img.ImageWidth - (lnLeftBorder * 2), ;
img.ImageHeight - (lnTitleHeight + (lnTopBorder * 2)))
img.SaveasBMP(lcDesktop+'\'+"Dash")
img = NULL
GDIP = NULL
RELEASE THISFORM

ENDFUNC

As you will see, there is no command button and no temporary files. The timer should fire five seconds after the form loads, after which the form should close. Give it a try and let us know how you get on.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike, that's a body of work.

Even if the OP doesn't 'get it' I'm going to thank you for making it so clear, clean and obvious

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
I wasn't only referring to the immediate work, but the sheer length of this thread!

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Must be close now.

At least the OP on that one was 'listening'

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Thanks sir's for best and simple coding.

I run your codes but following error appears

ax_bg7qwa.png


With Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top