Get Window Position in HTA - window

Can i get the current window position in HTA (HTML Application)? which is similar with Me.Top and Me.Left in VB6, but i want it get the pixels number.!?

With JS you can get the position of the topmost window with top.screenLeft and top.screenTop.

Here's a cool way to do it. It's sorta animated
VBScript
Add this to global variables
Dim IntervalMovingWindow
then add this OnLoad routine
Sub Window_OnLoad
moveTo screen.availWidth/2-200 , screen.availHeight/2-100
IntervalMovingWindow= Setinterval ("MoveTheWindow",100)
End Sub
Then Add this Routine for moving windows (animated style)
Sub MoveTheWindow
Dim X,Y
X=top.screenleft
Y=top.screentop
If X<=60 Then
X=0
End If
If Y<=60 Then
Y=0
End If
If X<>0 Then
moveBy -20,0
End If
If Y<>0 Then
moveBy 0,-20
End If
If X=0 And Y=0 Then
moveto 0,0
clearInterval IntervalMovingWindow
End If
End Sub
This may not work if you have an excessively large border. in that case you may need to change -20 in the moveBy to a larger negative number like -30 and the If {X or Y}=60 into a larger positive number like If {X or Y}=80
NOTE don't include the Braces '{}' they just mean that i am talking about both X and the Y conditionals.

Related

How to adjust export image size in PowerPoint using vba?

I'm creating a macro in PowerPoint VBA to export an image from the current slide. The export image will be the first image having a width larder than 250 units. The image is stored as a Shape, so I do a For Each ... Next loop to do it. The code works fine.
Function FindAndSavePicture() As String
'
' Find the target picture at the active windows
'
'
Dim myTempPath As String
myTempPath = "C:\Users\" & Environ$("USERNAME") _
& "\AppData\Local\Microsoft\Windows\pic_VBA.jpg"
With ActiveWindow.Selection.SlideRange
For Each s In .Shapes
Debug.Print s.Name
If s.Type = msoPicture And s.Width > 250 Then
' Show scale
Debug.Print "s.Width=" & s.Width ' s.Width=323,3931
Debug.Print "s.Height=" & s.Height ' s.Height=405
' Save pic in file system
s.Export myTempPath, ppShapeFormatJPG
' assign the return value for this function
FindAndSavePicture = myTempPath
Exit For
End If
Next
End With
End Function
Problem
The exported image pic_VBA.jpg is much smaller than it is shown in the PowerPoint. I want the original size of the picture. This exported image by VBA pic_VBA.jpg has 331 x 413 in dimensions. And if I export the image manually using Save As Picture..., the exported image pic_SaveAs.jpg has 692 x 862 in dimensions, which is the original size.
pic_VBA.jpg dimensions : 331 x 413
pic_SaveAs.jpg dimensions : 692 x 862 (original size)
What I've tested
s.Export myTempPath, ppShapeFormatJPG, s.Width, s.Height, ppScaleXY
It doesn't work. The export image's dimensions are 150 x 413
Question
So, how to adjust export image size in PowerPoint using vba ?
Related infomations
MSDN: Shape.Export Method
MSDN: PpExportMode Enumeration
Is the image scaled in PowerPoint? If it's anything but 100%, you'll need to work out the scale % in X/Y dimensions, set it to 100%, export it and then scale it back to the stored settings. This function will assist with that:
' Function to return the scale percentages of a given picture shape
' Written by: Jamie Garroch of YOUpresent.co.uk
Public Type ypPictureScale
ypScaleH As Single
ypScaleW As Single
End Type
' Calculate the scale of a picture by resetting it to 100%,
' comparing with it's former size and then rescaling back to it's original size
Public Function PictureScale(oShp As Shape) As ypPictureScale
Dim ShpW As Single, ShpH As Single
Dim LAR As Boolean
' Save the shape dimensions
ShpH = oShp.height
ShpW = oShp.width
' Unlock the aspect ratio if locked
If oShp.LockAspectRatio Then LAR = True: oShp.LockAspectRatio = msoFalse
' Rescale the image to 100%
oShp.ScaleHeight 1, msoTrue
oShp.ScaleWidth 1, msoTrue
' Calculate the scale
PictureScale.ypScaleH = ShpH / oShp.height
PictureScale.ypScaleW = ShpW / oShp.width
' Rescale the image to it's former size
oShp.ScaleHeight PictureScale.ScaleH, msoTrue
oShp.ScaleWidth PictureScale.ScaleW, msoTrue
' Relock the aspect ratio if originally locked
If LAR Then oShp.LockAspectRatio = msoFalse
End Function
It's not clear from your comments, but you may be missing the fact that PowerPoint uses points (72 points to the inch) as dimensions, not inches or pixels.
Convert the size of the shape from points to inches then multiply by 150 to get the size PPT will export to.
That 150 may vary from one system to another, but I don't believe it does.
Use ActivePresentation.PageSetup.SlideWidth and ActivePresentation.PageSetup.SlideHeight as ScaleWidth and ScaleHeight in the Shape.Export method to receive a picture file with the original dimensions.

Keeping an HTA from resizing to extremely small sizes

I have a HTA that I want to block resizing to extremely small sizes (less than 170x70). I found a way in VBScript to do this by detecting if the window is to small and if it is resizing it automatically like this:
<script type="text/vbscript">
Sub Window_onResize
Dim w
Dim h
Dim p
w = Document.Body.OffsetWidth
h = Document.Body.OffsetHeight
p = false
If Document.Body.OffsetWidth < 170 Then
w = 170
p = true
End If
If Document.Body.OffsetHeight < 70 Then
h = 70
p = true
End If
If p Then
window.ResizeTo w,h
End If
End Sub
</script>
My problem is that apparently ResizeTo counts the scroll bar and the title bar whereas OffsetWidth and OffsetHeight don't. I thought of solving this problem by detecting the title bar's height and the scroll bar's width. I've searched the web for how to do this and I haven't found anything. You can suggest a way of detecting the title bar's height and the scroll bar's width or another way of keeping the HTA from getting resized to extremesly small sizes.

Print image to pdf without margin using Matlab

I'm trying to use the answers I found in these questions:
How to save a plot into a PDF file without a large margin around
Get rid of the white space around matlab figure's pdf output
External source
to print a matlab plot to pdf without having the white margins included.
However using this code:
function saveTightFigure( h, outfilename, orientation )
% SAVETIGHTFIGURE(H,OUTFILENAME) Saves figure H in file OUTFILENAME without
% the white space around it.
%
% by ``a grad student"
% http://tipstrickshowtos.blogspot.com/2010/08/how-to-get-rid-of-white-margin-in.html
% get the current axes
ax = get(h, 'CurrentAxes');
% make it tight
ti = get(ax,'TightInset');
set(ax,'Position',[ti(1) ti(2) 1-ti(3)-ti(1) 1-ti(4)-ti(2)]);
% adjust the papersize
set(ax,'units','centimeters');
pos = get(ax,'Position');
ti = get(ax,'TightInset');
set(h, 'PaperUnits','centimeters');
set(h, 'PaperSize', [pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
set(h, 'PaperPositionMode', 'manual');
set(h, 'PaperPosition',[0 0 pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
% save it
%saveas(h,outfilename);
if( orientation == 1)
orient portrait
else
orient landscape
end
print( '-dpdf', outfilename );
end
Results in this output:
As you can see the 'PaperSize' seems to be set not properly. Any idea of possible fixes?
NOTE
If I change the orientation between landscape and portrait the result is the same, simply the image is chopped in a different way.
However if I save the image with the saveas(h,outfilename); instruction the correct output is produced.
Why is this? And what is the difference between the two saving instructions?
Alltogether the answers you mentioned offer a lot of approaches, but most of them didn't worked for me neither. Most of them screw up your papersize when you want to get the tight inset, the only which worked for me was:
set(axes_handle,'LooseInset',get(axes_handle,'TightInset'));
I finally wrote a function, where I specify the exact height and width of the output figure on paper, and the margin I want (or just set it to zero). Be aware that you also need to pass the axis handle. Maybe this functions works for you also.
function saveFigure( fig_handle, axes_handle, name , height , width , margin)
set(axes_handle,'LooseInset',get(axes_handle,'TightInset'));
set(fig_handle, 'Units','centimeters','PaperUnits','centimeters')
% the last two parameters of 'Position' define the figure size
set(fig_handle,'Position',[-margin -margin width height],...
'PaperPosition',[0 0 width+margin height+margin],...
'PaperSize',[width+margin height+margin],...
'PaperPositionMode','auto',...
'InvertHardcopy', 'on',...
'Renderer','painters'... %recommended if there are no alphamaps
);
saveas(fig_handle,name,'pdf')
end
Edit: if you use painters as renderer saveas and print should produce similar results. For jpegs print is preferable as you can specify the resolution.

VBA Chart Manipulation Slow

I wrote some Excel VBA code that generates a scatterplot and changes a few properties of the chart. (Code is below for reference.) The code moves slowly through tasks like deleting the chart legend, removing horizontal/vertical gridlines, and changing the X and Y series. Excel's timer gives me the following duration for each task:
insert scatterplot: 0.01171875
delete series: 0
plot x vs y: 0.55859375
delete legend: 0.5703125
delete chart title: 0.66015625
remove grid: 1.3046875
format axes: 0
overall: 3.11328125
Removing the grid, changing the title, plotting the X and Y series, and deleting the legend seem to take a long time. I've googled for alternative ways to write the code, but haven't been able to find anything useful. The code works entirely as expected, except for the slow speed. Any ideas as to what's causing the bad performance, and how I can speed this up? Thanks in advance.
EDIT: I've already turned off screen updating while working with the chart. The chart is generated/formatted while a userform is open, if that makes any difference.
Here is the relevant snippet of code:
With ActiveChart
'Delete all series currently in plot
Do While .FullSeriesCollection.Count > 0
.FullSeriesCollection(1).Delete
Loop
'Plot Actual (Y) vs. Inverse Distribution (X)
.SeriesCollection.NewSeries
.FullSeriesCollection(1).XValues = "=" & tempSheetName & "!$C:$C"
.FullSeriesCollection(1).Values = "=" & tempSheetName & "!$A:$A"
'Delete legend
.Legend.Delete
'Delete chart title
.SetElement (msoElementChartTitleNone)
'Remove gridlines
.SetElement (msoElementPrimaryValueGridLinesNone)
.SetElement (msoElementPrimaryCategoryGridLinesNone)
'Format axes
Dim xAxis As Axis, yAxis As Axis
Set xAxis = .Axes(xlCategory)
Set yAxis = .Axes(xlValue)
With yAxis
'Title y axis "actual"
.HasTitle = True
.AxisTitle.Caption = "Actual"
'Add tick marks
.MajorTickMark = xlOutside
End With
With xAxis
'Title x axis by dist type
.HasTitle = True
.AxisTitle.Caption = dist.getDistType
'Add tick marks
.MajorTickMark = xlOutside
End With
End With
Without the data and machine specifics it can be hard to say why this is slow, although here are some alternatives to some of the code you have.
The first and foremost thing I'd change is not to Activate the chart. If you are creating the chart through code, do so but set it to a variable, eg Set wcChart = ThisWorkbook.Charts.Add. Then change With ActiveChart to With wcChart.
Also, delete the FullSeriesCollection and then delete the chart title, remove the gridlines and change the axes before filling up the new data. The chart manipulation should be quicker with less data in the chart. Be careful here though because changing aspects of the chart in different orders can produce different outputs (as an example the layout of a legend).
You fill the new FullSeriesCollection with the entire columns of A and C, specify the exact range of the data rather than the whole column.
Other changes to try, I'm not saying these will work but they are worth a shot if you haven't tried. Instead of checking for a FullSeriesCollection each time:
Do While .FullSeriesCollection.Count > 0
.FullSeriesCollection(1).Delete
Loop
The following may be quicker:
For ii = .FullSeriesCollection.Count To 1 Step -1
.FullSeriesCollection(ii).Delete
Next ii
Also, instead of .SetElement for the Chart title and Gridlines I use the following:
'You have to set the title to 'True' before it'll work with 'False'. Go figure.
.HasTitle = True
.HasTitle = False
.HasMajorGridlines = False
.HasMinorGridlines = False

Viewing Part of a figure

I made a simulation of 10000 times and want to view part of simulation between 5000-5200. I am able to view it with the code below, but the x-axis says 0-250. I want the x-axis to display the exact figure of 5000-5200. Also there seems to be a small gap at the end of the figure as the axis runs up to 250 for some reason. I just want to view the figure in for this set time with the x-axis showing the exact labels and without the gap at the end.
Thanks
N=10000;%Number of simulation
P=0.02;
Q = zeros(N,1); %current value of queue
X=zeros(N,1);%simulation data
Ci=0;
L=0.9;
Bu=zeros(N,1);
Bs=30;
Bd1=50;
Bd2=270;
Ti=0;
for Ti=2:N
U=rand(1);
a=log10(U);
b=log10(1-P);
c=(a/b);
d=1+c;
X(Ti)=round(d);
Ci=Ci+1;
if X(Ti)< (L)*(Bs)
Bu(Ti)=Bs;
else if X(Ti) < (L)*(Bs+Bd1)
Bu(Ti)=Bs+Bd1;
else
Bu(Ti)=Bs+Bd1+Bd2;
end
end
Ti=Ti+1;
end
plot(X(5000:5200,1),'r');
set (gca,'ylim',[0 400]);
hold on;
plot(Bu(5000:5200,1),'b');
set (gca,'ylim',[0 400]);
hold off
Plot expects two inputs, the first depicting the horizontal axis and the second depicting the vertical axis. When you do not supply two inputs, then it computes the length of the single input (in this case that length is 5200-5000 = 200), and it just uses 1 through that length (1:200 in this case) as if it is the values for the horizontal axis variable.
I think you want to issue the command:
plot(5000:5200, X(5000:5200,1), 'r')
Often Matlab will adjust plot axes for better default views, so it's probably showing the axis out to the index 250 just by virtue of some default plotting convention. You can similarly use set(gca, 'xlim', [5000 5200]) if you wish.

Resources