09stephenb
09stephenb

Reputation: 9816

Centring an HTA window

How do I centre an HTA window so that it is in the centre of the screen regardless of the screen resolution of the size of the HTA. I have this:

</head>
<script>
Sub DoResize 'Do not use Window_Onload
   window.resizeTo 800,600
   strComputer = "."
   Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2")
   Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor")
   For Each objItem in colItems
       intHorizontal = objItem.ScreenWidth
       intVertical = objItem.ScreenHeight
   Next
   intLeft = (intHorizontal - 800) / 2
   intTop = (intVertical - 600) / 2
   window.moveTo intLeft, intTop
End Sub
DoResize() 'Run the subroutine to position the containing window (your HTA dialog) before the body is rendered.
</script>
<body>

But if I change the screen resolution it doesn't work and it resizes the HTA window.

How do I move the HTA to the centre of the screen regardless of the HTA size of screen resolution?

Upvotes: 8

Views: 7505

Answers (3)

Sourabh
Sourabh

Reputation: 1

HTA window

`

<!DOCTYPE html>
<html>
<head>
    <title>File Operations</title>
    <hta:application id="FileOperationsHTA" 
                     applicationname="FileOperationsHTA" 
                     singleinstance="yes" 
                     maximizebutton="no" 
                     minimizebutton="no" 
                     windowstate="normal" 
                     scroll="no" 
                     navigable="yes" 
                     showintaskbar="no" 
                     contextmenu="no" 
                     selection="no"
    />
</head>


<body onload="showFields()">
    <h1>File Operations</h1>
    <label for="operationCombo">Select operation:</label>
    <select id="operationCombo" onchange="showFields()">
        <option value="">Select an operation</option>
        <option value="move">Move</option>
        <option value="copy">Copy</option>
        <option value="delete">Delete</option>
        <option value="newfolder">New Folder</option>
     <option value="rename">Rename</option>
    </select>

    <div id="sourceField" style="display: none;">
        <label for="sourcePath">Source path:</label>
        <input type="text" id="sourcePath" />
    </div>

    <div id="destinationField" style="display: none;">
        <label for="destinationPath">Destination path:</label>
        <input type="text" id="destinationPath" />
    </div>

    <div id="deleteField" style="display: none;">
        <label for="deletePath">File/Folder to delete:</label>
        <input type="text" id="deletePath" />
    </div>

    <div id="newFolderField" style="display: none;">
        <label for="parentPath">Parent path for new folder:</label>
        <input type="text" id="parentPath" />
        <label for="newFolderName">New folder name:</label>
        <input type="text" id="newFolderName" />
    </div>
 <div id="renameField" style="display: none;">
        <label for="sourcePathRename">Source path:</label>
        <input type="text" id="sourcePathRename" />
        <label for="destinationPathRename">Destination path:</label>
        <input type="text" id="destinationPathRename" />
        <label for="newFileName">New filename:</label>
        <input type="text" id="newFileName" />
    </div>


    <button id="executeButton" onclick="executeOperation()">Execute</button>


    <script type="text/javascript">
        function showFields() {

           

            var operation = document.getElementById('operationCombo').value;
            var sourceField = document.getElementById('sourceField');
            var destinationField = document.getElementById('destinationField');
            var deleteField = document.getElementById('deleteField');
            var newFolderField = document.getElementById('newFolderField');
        var renameField = document.getElementById('renameField');
            var executeButton = document.getElementById('executeButton');


            sourceField.style.display = 'none';
            destinationField.style.display = 'none';
            deleteField.style.display = 'none';
            newFolderField.style.display = 'none';
            renameField.style.display = 'none';

           

            switch (operation) {
                case 'move':
                    sourceField.style.display = 'block';
                    destinationField.style.display = 'block';
                    break;
                case 'copy':
                    sourceField.style.display = 'block';
                    destinationField.style.display = 'block';
                    break;
                case 'delete':
                    deleteField.style.display = 'block';
                    break;
                case 'newfolder':
                    newFolderField.style.display = 'block';
                    break;
         case 'rename':
                    renameField.style.display = 'block';
                    document.getElementById('sourcePathRename').disabled = false;
                    document.getElementById('destinationPathRename').disabled = false;
                    document.getElementById('newFileName').disabled = false;
                    break;
            }
        }

       function executeOperation() {
            var operation = document.getElementById('operationCombo').value;
            switch (operation) {
                case 'move':
                    var sourcePath = document.getElementById('sourcePath').value;
                    var destinationPath = document.getElementById('destinationPath').value;
                    moveFilesWithWildcard(sourcePath, destinationPath);
                    break;
                case 'copy':
                    var sourcePath = document.getElementById('sourcePath').value;
                    var destinationPath = document.getElementById('destinationPath').value;
                    copyFilesWithWildcard(sourcePath, destinationPath);
                    break;
                case 'delete':
                    var deletePath = document.getElementById('deletePath').value;
                    deleteFilesWithWildcard(deletePath);
                    break;
                case 'newfolder':
                    var parentPath = document.getElementById('parentPath').value;
                    var newFolderName = document.getElementById('newFolderName').value;
                    createNewFolder(parentPath, newFolderName);
                    break;
         case 'rename':
                    var sourcePathRename = document.getElementById('sourcePathRename').value;
                    var destinationPathRename = document.getElementById('destinationPathRename').value;
                    var newFileName = document.getElementById('newFileName').value;
                    renameFile(sourcePathRename, destinationPathRename, newFileName);
                    break;
            }
        }

function renameFile(sourcePath, destinationPath, newFileName) {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            if (fso.FileExists(sourcePath)) {
                try {
                    fso.MoveFile(sourcePath, fso.BuildPath(destinationPath, newFileName));
                    alert('Rename operation executed.\nSource: ' + sourcePath + '\nDestination: ' + destinationPath);
                } catch (e) {
                    alert('Error renaming file: ' + e.message);
                }
            } else {
                alert('Source file not found.');
            }
        }

        function moveFilesWithWildcard(sourcePath, destinationPath) {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var parentFolder = fso.GetParentFolderName(sourcePath);
            var wildcard = fso.GetFileName(sourcePath);
            var parentFolderObj = fso.GetFolder(parentFolder);

            if (fso.FolderExists(parentFolder)) {
                try {
                    var files = new Enumerator(parentFolderObj.Files);
                    var subFolders = new Enumerator(parentFolderObj.SubFolders);
                    var regex = new RegExp(wildcard.replace(/\./g, "\\.").replace(/\*/g, ".*"), "i");

                    for (; !files.atEnd(); files.moveNext()) {
                        var file = files.item();
                        if (regex.test(file.Name)) {
                            var sourceFile = fso.BuildPath(parentFolder, file.Name);
                            var destinationFile = fso.BuildPath(destinationPath, file.Name);
                            fso.MoveFile(sourceFile, destinationFile);
                        }
                    }

                    for (; !subFolders.atEnd(); subFolders.moveNext()) {
                        var subFolder = subFolders.item();
                        if (regex.test(subFolder.Name)) {
                            var newDestPath = fso.BuildPath(destinationPath, subFolder.Name);
                            fso.CreateFolder(newDestPath);
                            moveFilesRecursive(subFolder, newDestPath);
                        }
                    }
                } catch (e) {
                    alert('Error moving files/folders with wildcard.');
                }
            } else {
                alert('Parent path not found.');
            }
        }

        function moveFilesRecursive(folder, destinationPath) {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var files = new Enumerator(folder.Files);
            var subFolders = new Enumerator(folder.SubFolders);

            for (; !files.atEnd(); files.moveNext()) {
                var file = files.item();
                var destinationFile = fso.BuildPath(destinationPath, file.Name);
                fso.MoveFile(file.Path, destinationFile);
            }

            for (; !subFolders.atEnd(); subFolders.moveNext()) {
                var subFolder = subFolders.item();
                var newDestPath = fso.BuildPath(destinationPath, subFolder.Name);
                fso.CreateFolder(newDestPath);
                moveFilesRecursive(subFolder, newDestPath);
            }
        }

        function copyFiles(sourcePath, destinationPath) {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var folder = fso.GetFolder(sourcePath);

            if (fso.FolderExists(destinationPath)) {
                try {
                    copyFilesRecursive(folder, destinationPath);
                    alert('Copy operation executed.\nSource Path: ' + sourcePath + '\nDestination Path: ' + destinationPath);
                } catch (e) {
                    alert('Error copying files/folders.');
                }
            } else {
                alert('Destination path not found.');
            }
        }

        function copyFilesRecursive(folder, destinationPath) {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var files = new Enumerator(folder.Files);
            var subFolders = new Enumerator(folder.SubFolders);

            for (; !files.atEnd(); files.moveNext()) {
                var file = files.item();
                fso.CopyFile(file.Path, fso.BuildPath(destinationPath, file.Name), true);
            }

            for (; !subFolders.atEnd(); subFolders.moveNext()) {
                var subFolder = subFolders.item();
                var newDestPath = fso.BuildPath(destinationPath, subFolder.Name);
                fso.CreateFolder(newDestPath);
                copyFilesRecursive(subFolder, newDestPath);
            }
        }

 function moveFilesWithWildcard(sourcePath, destinationPath) {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var parentFolder = fso.GetParentFolderName(sourcePath);
            var wildcard = fso.GetFileName(sourcePath);
            var parentFolderObj = fso.GetFolder(parentFolder);

            if (fso.FolderExists(parentFolder)) {
                try {
                    var files = new Enumerator(parentFolderObj.Files);
                    var subFolders = new Enumerator(parentFolderObj.SubFolders);
                    var regex = new RegExp(wildcard.replace(/\./g, "\\.").replace(/\*/g, ".*"), "i");

                    for (; !files.atEnd(); files.moveNext()) {
                        var file = files.item();
                        if (regex.test(file.Name)) {
                            var sourceFile = fso.BuildPath(parentFolder, file.Name);
                            var destinationFile = fso.BuildPath(destinationPath, file.Name);
                            fso.MoveFile(sourceFile, destinationFile);
                        }
                    }

                    for (; !subFolders.atEnd(); subFolders.moveNext()) {
                        var subFolder = subFolders.item();
                        if (regex.test(subFolder.Name)) {
                            var newDestPath = fso.BuildPath(destinationPath, subFolder.Name);
                            fso.CreateFolder(newDestPath);
                            moveFilesRecursive(subFolder, newDestPath);
                        }
                    }
                } catch (e) {
                    alert('Error moving files/folders with wildcard.');
                }
            } else {
                alert('Parent path not found.');
            }
        }

        function moveFilesRecursive(folder, destinationPath) {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var files = new Enumerator(folder.Files);
            var subFolders = new Enumerator(folder.SubFolders);

            for (; !files.atEnd(); files.moveNext()) {
                var file = files.item();
                var destinationFile = fso.BuildPath(destinationPath, file.Name);
                fso.MoveFile(file.Path, destinationFile);
            }

            for (; !subFolders.atEnd(); subFolders.moveNext()) {
                var subFolder = subFolders.item();
                var newDestPath = fso.BuildPath(destinationPath, subFolder.Name);
                fso.CreateFolder(newDestPath);
                moveFilesRecursive(subFolder, newDestPath);
            }
        }




    


    function copyFilesWithWildcard(sourcePath, destinationPath) {
        var fso = new ActiveXObject("Scripting.FileSystemObject");

        if (fso.FileExists(sourcePath)) {
            // Source path is a single file
            var sourceFolder = fso.GetParentFolderName(sourcePath);
            var sourceFileName = fso.GetFileName(sourcePath);

            if (fso.FileExists(destinationPath)) {
                // Destination path includes a filename, create a new folder in the destination path
                var destinationParentFolder = fso.GetParentFolderName(destinationPath);
                var newFolderName = fso.GetBaseName(destinationPath);
                destinationPath = fso.BuildPath(destinationParentFolder, newFolderName);
            } else if (!fso.FolderExists(destinationPath)) {
                // Destination folder does not exist, create it
                fso.CreateFolder(destinationPath);
            }

            // Perform the copy operation
            fso.CopyFile(sourcePath, fso.BuildPath(destinationPath, sourceFileName));
            alert('Copy operation executed.\nSource: ' + sourcePath + '\nDestination: ' + destinationPath);
        } else if (fso.FolderExists(sourcePath)) {
            // Source path is a folder
            var parentFolder = fso.GetParentFolderName(sourcePath);
            var wildcard = fso.GetFileName(sourcePath);
            var parentFolderObj = fso.GetFolder(parentFolder);

            if (fso.FolderExists(parentFolder)) {
                try {
                    var files = new Enumerator(parentFolderObj.Files);
                    var subFolders = new Enumerator(parentFolderObj.SubFolders);
                    var regex = new RegExp(wildcard.replace(/\./g, "\\.").replace(/\*/g, ".*"), "i");

                    // Collect files and folders matching the wildcard
                    var filesToCopy = [];
                    var foldersToCopy = [];

                    for (; !files.atEnd(); files.moveNext()) {
                        var file = files.item();
                        if (regex.test(file.Name)) {
                            filesToCopy.push(file);
                        }
                    }

                    for (; !subFolders.atEnd(); subFolders.moveNext()) {
                        var subFolder = subFolders.item();
                        if (regex.test(subFolder.Name)) {
                            foldersToCopy.push(subFolder);
                        }
                    }

                    // Perform the copy operation
                    if (!fso.FolderExists(destinationPath)) {
                        // Destination folder does not exist, create it
                        fso.CreateFolder(destinationPath);
                    }

                    for (var i = 0; i < filesToCopy.length; i++) {
                        var file = filesToCopy[i];
                        file.Copy(fso.BuildPath(destinationPath, file.Name));
                    }

                    for (var i = 0; i < foldersToCopy.length; i++) {
                        var folder = foldersToCopy[i];
                        copyFolderRecursive(folder, fso.BuildPath(destinationPath, folder.Name));
                    }

                    alert('Copy operation executed.\nSource: ' + sourcePath + '\nDestination: ' + destinationPath);
                } catch (e) {
                    alert('Error copying files/folders with wildcard: ' + e.message);
                }
            } else {
                alert('Source parent path not found.');
            }
        } else {
            alert('Source path not found.');
        }
    }

  







  
    function copyFolderRecursive(sourceFolder, destinationFolder) {
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        fso.CreateFolder(destinationFolder);

        var files = new Enumerator(sourceFolder.Files);
        var subFolders = new Enumerator(sourceFolder.SubFolders);

        for (; !files.atEnd(); files.moveNext()) {
            var file = files.item();
            file.Copy(fso.BuildPath(destinationFolder, file.Name));
        }

        for (; !subFolders.atEnd(); subFolders.moveNext()) {
            var subFolder = subFolders.item();
            copyFolderRecursive(subFolder, fso.BuildPath(destinationFolder, subFolder.Name));
        }
    }

 

function createNewFolder(parentPath, newFolderName) {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var fullPath = fso.BuildPath(parentPath, newFolderName);

            try {
                fso.CreateFolder(fullPath);
                alert('New folder created successfully.\nPath: ' + fullPath);
            } catch (e) {
                alert('Error creating new folder: ' + e.message);
            }
        }






    function deleteFilesWithWildcard(deletePath) {
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var parentFolder = fso.GetParentFolderName(deletePath);
        var wildcard = fso.GetFileName(deletePath);
        var parentFolderObj = fso.GetFolder(parentFolder);

        if (fso.FolderExists(parentFolder)) {
            try {
                var files = new Enumerator(parentFolderObj.Files);
                var subFolders = new Enumerator(parentFolderObj.SubFolders);
                var regex = new RegExp(wildcard.replace(/\./g, "\\.").replace(/\*/g, ".*"), "i");

                // Collect files and folders matching the wildcard
                var filesToDelete = [];
                var foldersToDelete = [];

                for (; !files.atEnd(); files.moveNext()) {
                    var file = files.item();
                    if (regex.test(file.Name)) {
                        filesToDelete.push(file);
                    }
                }

                for (; !subFolders.atEnd(); subFolders.moveNext()) {
                    var subFolder = subFolders.item();
                    if (regex.test(subFolder.Name)) {
                        foldersToDelete.push(subFolder);
                    }
                }

                // Delete files
                for (var i = 0; i < filesToDelete.length; i++) {
                    filesToDelete[i].Delete();
                }

                // Delete folders
                for (var i = 0; i < foldersToDelete.length; i++) {
                    deleteFolderRecursive(foldersToDelete[i]);
                }

                alert('Delete operation executed.\nPath: ' + deletePath);
            } catch (e) {
                alert('Error deleting files/folders with wildcard: ' + e.message);
            }
        } else {
            alert('Parent path not found.');
        }
    }

    function deleteFolderRecursive(folder) {
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var subFolders = new Enumerator(folder.SubFolders);

        for (; !subFolders.atEnd(); subFolders.moveNext()) {
            var subFolder = subFolders.item();
            deleteFolderRecursive(subFolder);
        }

        fso.DeleteFolder(folder.Path);
    }




    </script>
</body>
</html>

`

Upvotes: 0

Pedro404
Pedro404

Reputation: 178

var winWidth = 800, winHeight = 600;
window.resizeTo(winWidth, winHeight);
window.moveTo((screen.width - winWidth) / 2, (screen.height - winHeight) / 2);

Upvotes: 2

Teemu
Teemu

Reputation: 23406

Using WMI for this is a bit overkilling. You can use screen object instead:

window.resizeTo (800, 600);
window.moveTo((screen.width - 800) / 2, (screen.height - 600) / 2);

If you want to resize the window at start, put the lines above to a (JS) script tag in the head section, even before <hta: application> tag, or use the code in an event handler or where ever you need it.

If you want to exclude taskbar's height out of the centering area, you can use screen.availHeight instead of screen.height.

Upvotes: 6

Related Questions