CommonDialog help |
Author: |
Message: |
Otacon
New Member
Posts: 11
Joined: Oct 2008
|
O.P. CommonDialog help
Ok, so I'm using this code:
jscript code: function BrowseForPm(InitialDir)
{
BrowseFilter = "Text Files (*.txt)|*.txt|All Files|*.*";
var BrowseDialog = new ActiveXObject("UserAccounts.CommonDialog")
BrowseDialog.Filter = BrowseFilter;
BrowseDialog.InitialDir = InitialDir;
BrowseDialog.Flags = "&H4";
BrowseDialog.ShowOpen()
return BrowseDialog.FilePath;
pm = BrowseDialog.FilePath;
}
The dialog shows up fine, but it seems like the file path isn't being saved to the variable. Does anyone know what the problem is? I've been trying to make a dialog that works for days, searched this entire forum for anything related to CommonDialog and found nothing helpful.
Also, this type of dialog has been replaced with a new kind of dialog in Windows Vista, something called ItemDialog, so it won't even show up. So does anyone know the code that would be equivalent to this for the new Vista ItemDialog, because this won't work(I actually tested it just in case).
Thanks in advance for all your help.
This post was edited on 11-24-2008 at 06:47 PM by Otacon.
|
|
11-24-2008 06:46 PM |
|
|
MeEtc
Patchou's look-alike
In the Shadow Gallery once again
Posts: 2200 Reputation: 60
38 / /
Joined: Nov 2004
Status: Away
|
RE: CommonDialog help
I think I know what you're doing wrong. When you say 'the file path isn't being saved to the variable', I'm going to assume you mean the variable named 'pm'. It looks just fine to me, 'pm' IS being assigned the file path, but the variable is being destroyed on the very next line when the function ends.
What you need to do instead, is read the value of the function when its being called. Thats what the 'return' function does.
So somwhere else in your script i suppose you are calling the BrowseForPM() function, and then checking the value of pm on the next line. Instead, check the value of BrowseForPm(), and you shall get your file path.
I cannot hear you. There is a banana in my ear.
|
|
11-24-2008 06:57 PM |
|
|
Matti
Elite Member
Script Developer and Helper
Posts: 1646 Reputation: 39
32 / /
Joined: Apr 2004
|
RE: CommonDialog help
That's very simple.
jscript code: return BrowseDialog.FilePath;
pm = BrowseDialog.FilePath;
You are returning before setting the variable, that means you end the function right there. Everything else that comes afterwards won't be executed. So, this is very simple to fix: first set the pm variable, then return its value.
jscript code: pm = BrowseDialog.FilePath;
return pm;
As for your second question: yes, have a look at matty's reply to Browse For File featuring an implementation of GetOpenFileName which works both on XP and Vista. You'll probably want to change some things, like getting it out of the OnEvent_Initialize function and such.
If you're having trouble with this, we'll help you with that, but it's better to experiment with it a bit first and try things yourself - it really is the best way to learn programming.
This post was edited on 11-24-2008 at 06:59 PM by Matti.
|
|
11-24-2008 06:59 PM |
|
|
Otacon
New Member
Posts: 11
Joined: Oct 2008
|
O.P. RE: CommonDialog help
Tried both solutions, and sadly neither of them work. What I forgot to say earlier is that pm is a global variable.
And I did see matty's thread earlier, but I have no idea whatsoever what to do with that code. Seems too complicated to me, I'm just a humble newbie .
|
|
11-24-2008 07:12 PM |
|
|
Matti
Elite Member
Script Developer and Helper
Posts: 1646 Reputation: 39
32 / /
Joined: Apr 2004
|
RE: CommonDialog help
Heh, I could have seen that one coming. Well, have a look at my implementation of matty's code, used in my own script.
Some usage examples:
jscript code: //Open File Dialog
//Opens C:\ first, file filters are text files (*.txt) and all files (*.*)
pm = BrowseForFile("Browse for PM", "C:\\", "Text Files (*.txt)|*.txt|All files (*.*)|*.*||");
//Save File Dialog
//Opens C:\ first, file filters are PNG files (*.png) and all files (*.*)
var file = BrowseForFile("Save picture as", "C:\\", "PNG Files (*.png)|*.png|All files (*.*)|*.*||", true);
if(!file) {
//User didn't specify a file to save to or pressed Cancel.
} else {
//User specified a valid save location. Do something useful here.
}
And here's the implementation:
jscript code: /* FUNCION: BrowseForFile
Original code by Choli from the Messenger Plus! Translator script
Desc: Opens a "Browse for file" dialog with the given settings
Parameters:
title Dialog title
dir Start directory
filter Filter message. Example: "JavaScript file|*.js|Text file|*.txt||"
save True is "Save as", false is "Open"
ext Default extension to append to the filename when no extension is specified
file File to display as default
hwnd_owner Handle of the owner window, used to copy window icon etc.
Return value: File path to open or save to
*/
function BrowseForFile(title, dir, filter, save, ext, file, hwnd_owner) {
save = (save == true);
hwnd_owner = 1*hwnd_owner;
var OFN_ALLOWMULTISELECT = 0x200;
var OFN_ENABLESIZING = 0x800000;
var OFN_EXPLORER = 0x80000;
var OFN_FILEMUSTEXIST = 0x1000;
var OFN_HIDEREADONLY = 0x4;
var OFN_LONGNAMES = 0x200000;
var OFN_PATHMUSTEXIST = 0x800;
var OFN_OVERWRITEPROMPT = 0x2;
var OpenFileName = Interop.Allocate(88);
with (OpenFileName) {
WriteDWORD(0, Size); // lStructSize
WriteDWORD(4, hwnd_owner); // hwndOwner
WriteDWORD(8, 0); // hInstance
if(typeof filter !== "string") filter = "All files (*.*)|*.*||";
var s_filter = Interop.Allocate(2 * (filter.length + 1));
WriteMultiStringW(s_filter, filter);
WriteDWORD(12, s_filter.DataPtr); // lpstrFilter
WriteDWORD(16, 0); // lpstrCustomFilter
WriteDWORD(20, 0); // nMaxCustomFilter
WriteDWORD(24, 1); // nFilterIndex
if(typeof file !== "string") file = "";
var s_file = Interop.Allocate(2 * (1024 + 1));
s_file.WriteString(0, file);
WriteDWORD(28, s_file.DataPtr); // lpstrFile
WriteDWORD(32, (s_file.Size / 2 - 1)); // nMaxFile
WriteDWORD(36, 0); // lpstrFileTitle
WriteDWORD(40, 0); // nMaxFileTitle
if(typeof dir === "string") {
var s_dir = Interop.Allocate(2 * (dir.length + 1));
s_dir.WriteString(0, dir);
WriteDWORD(44, s_dir.DataPtr); // lpstrInitialDir
}
if(typeof title === "string") {
var s_title = Interop.Allocate(2 * (title.length + 1));
s_title.WriteString(0, title);
WriteDWORD(48, s_title.DataPtr); // lpstrTitle
}
WriteDWORD(52, OFN_ENABLESIZING | OFN_EXPLORER | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_PATHMUSTEXIST | (save ? OFN_OVERWRITEPROMPT : OFN_FILEMUSTEXIST)); // flags
WriteWORD(56, 0); // nFileOffset
WriteWORD(58, 0); // nFileExtension
if(typeof ext === "string") {
var s_ext = Interop.Allocate(2 * (ext.length + 1));
s_ext.WriteString(0, ext);
WriteDWORD(60, s_ext.DataPtr);
} else WriteDWORD(60, 0); // lpstrDefExt
WriteDWORD(64, 0); // lCustData
WriteDWORD(68, 0); // lpfnHook
WriteDWORD(72, 0); // lpTemplateName
WriteDWORD(76, 0); // pvReserved
WriteDWORD(80, 0); // dwReserved
WriteDWORD(84, 0); // FlagsEx
}
var result = Interop.Call("comdlg32.dll", (save ? "GetSaveFileNameW" : "GetOpenFileNameW"), OpenFileName);
var s_return = false;
if (result) {
var p_path = s_file.ReadString(0);
var p_files = new Array();
var p_file = "";
var pos = 2 * (p_path.length + 1);
if(!save) {
while(true) {
p_file = s_file.ReadString(pos);
if(p_file.length > 0) {
p_files.push(p_file);
pos += 2 * (p_file.length + 1);
} else break;
}
}
s_return = (p_files.length > 0) ? new Array(p_path).concat(p_files) : p_path;
}
OpenFileName.Size = 0;
s_filter.Size = 0;
s_file.Size = 0;
s_dir.Size = 0;
return s_return;
function WriteMultiStringW (datablock, string) {
var pos = 0;
datablock.WriteString(0, string);
pos = string.indexOf("|", pos);
while (pos !== -1) {
datablock.WriteWORD(2 * pos, 0);
pos = string.indexOf("|", pos + 1);
}
}
function Space (number) {
var i;
var s = "";
for (i = 0; i < number; i++) {
s += " ";
}
return s;
}
}
This post was edited on 11-24-2008 at 07:42 PM by Matti.
|
|
11-24-2008 07:38 PM |
|
|
Otacon
New Member
Posts: 11
Joined: Oct 2008
|
O.P. RE: CommonDialog help
Just as I thought everything was going to work this time, I got:
code: Error: Overflow (code: -2146828282)
File: file_browse.js. Line: 135.
after I pressed my button which calls the function.
Line 135:
jscript code: WriteDWORD(4, hwnd_owner); // hwndOwner
Damn, I'm never going to get this working. Thanks for all your help so far, guys.
|
|
11-24-2008 07:57 PM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
RE: CommonDialog help
Try passwing 0 as the hwndOwner see if it works. Passing an actual value (numerical) causes the window to be modal to the calling window. When pressing the button you can use: jscript code: pPlusWnd.Handle
as the hwndOwner now pPlusWnd is dependent on what the variable name is that is a parameter to code: function OnWindowIdEvent_CtrlClicked(pPlusWnd, sControlId) {
...
}
What value were you passing to the function?
This post was edited on 11-24-2008 at 08:05 PM by matty.
|
|
11-24-2008 08:03 PM |
|
|
Otacon
New Member
Posts: 11
Joined: Oct 2008
|
O.P. RE: CommonDialog help
Oh man, this is so confusing... Do you mind if I send you the .plsc file so you can check it out? Because I'm honestly very confused at this point. But don't try to do anything for me, just check it out and tell me where I went wrong so I can correct it? I actually want to learn something from this.
EDIT: Uploaded the file just in case you want to check it out.
Attachment: AMIP v0.9.1.plsc (6.7 KB)
This file has been downloaded 197 time(s).
This post was edited on 11-24-2008 at 08:33 PM by Otacon.
|
|
11-24-2008 08:16 PM |
|
|
Spunky
Former Super Mod
Posts: 3658 Reputation: 61
36 / /
Joined: Aug 2006
|
RE: CommonDialog help
jscript code: BrowseDialog.Flags = "&H4";
I thought we had to use hex equivalents? Shouldn't it be:
jscript code: BrowseDialog.Flags = 0x04;
Just a bit confused myself
<Eljay> "Problems encountered: shit blew up"
|
|
11-24-2008 08:48 PM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
RE: CommonDialog help
Why do you have two of the exact same functions just with different names?
Anyways one of your problems is that you are not passing anything to hwnd_owner. This causes a problem because in the code it says hwnd_owner = 1*hwnd_owner;
However when you don't specify anything it is null. So you have two options. Either pass a value to the parameter or add this to the top of the function
jscript code: if (typeof hwnd_owner === 'undefined') hwnd_owner = 0;
This post was edited on 11-24-2008 at 09:42 PM by matty.
|
|
11-24-2008 09:20 PM |
|
|
Pages: (2):
« First
[ 1 ]
2
»
Last »
|
|
|