I have a WPF WebBrowser Control in a WPF app, on a web page (visited in the WPF browser) I have a VBScript Print() sub to print a receipt without promoting the user.
<script language='VBScript'>
Sub Print()
OLECMDID_PRINT = 6
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_PROMPTUSER = 1
call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>
Since adding this VBScript Print() I now am prompted with the following in my WPF Browser:
internet explorer blocked an activex control so this page might not
display correctly
How can I either prevent the message and just run the script, or at very worst show a prompt?
The solution for us in the end was to use Silverlight 5 to print the page without prompting the user as Silverlight 5 provides a way to print without prompting the user using ElevatedPermissions.
Related
I have embedded a WebBrowser control in my application and display content that I receive from a server. Specifically: The control is bound to a string which contains the error message from a rest call, that sometimes is HTML.
I wonder if there is a security risk if active content, e.g. JavaScript would be sent as part of the error message. Is there a way to instruct the WebBrowser control to disable all active content?
There are several ways to do:
First way is remove javascript from your string before pass it to browser, from Elian Ebbing's answer:
The quick 'n' dirty method would be a regex like this:
var regex = new Regex(
"(\\<script(.+?)\\</script\\>)|(\\<style(.+?)\\</style\\>)",
RegexOptions.Singleline | RegexOptions.IgnoreCase
);
string ouput = regex.Replace(input, "");
The better* (but possibly slower) option would be to use
HtmlAgilityPack:
HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(htmlInput);
var nodes = doc.DocumentNode.SelectNodes("//script|//style");
foreach (var node in nodes) node.ParentNode.RemoveChild(node);
string htmlOutput = doc.DocumentNode.OuterHtml;
*) For a discussion about why it's better, see this thread.
That way seem better and easier.
Second way is use WinForms webbrowser control, which allow you control lower level of browser, but this involve some invoking to WinAPI.
You can see this link for more info.
I saw this code ealier about drag and dropping attachment files (http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C) from Outlook into Windows Form and it works fine in windows Forms, but I can't seem to make it work with WPF.
I tried to simply change System.windows.form.IDataObject to System.Windows.IDataObject but it doesn't work (as I should have guessed).
I also simply tried to get the content of the e.Data FileContents but always get errors (which seems to be the case to everyone when I check on the web).
Did anyone ever did drag and dropping attachment from Outlook to WPF ? I am at a complete loss.
Edit : I am not trying to get file from a Windows Explorer windows ( I do but I know how to). It's really the whole getting attachment from Outlook directly that doesn't work . I am fully aware too that I could simple take the file from outlook into a temp folder and then drop it into my program, but I would like to avoid this unncessary step if possible.
so in the end I was able to find out a link where someone did exactly that :
https://gist.github.com/MattyBoy4444/521547
For those who wonders. Here is what I did exactly.
Create a new project in C# (my code is in VB) and add the code to it
Reference the new project in my main project to be able to use it
In my drop Event, check whether or not I had the "FileGroupDescriptorW" object in the drop data and called the method if I do to retrieve the files.
Here is the complete code
If obj.GetDataPresent("FileGroupDescriptorW") Then 'Outlook
Dim oOutLookObj As New Helpers.OutlookDataObject(e.Data)
Dim StrFiles() As String = oOutLookObj.GetData("FileGroupDescriptorW")
Dim contentStream() As System.IO.MemoryStream = oOutLookObj.GetData("FileContents")
' Do intended work...
End if
The names of the files are in StrFiles and the content are found in the streams. Both have the same array size and are order correctly.
I am trying to make WatiN attach to an IE popup window (IE 10).
This popup contains a frameset --> a single frame --> a pdf document.
My goal is to save this pdf to my disk.
Dim winExists = IE.Exists(Of IE)(Find.ByUrl(Function(url) url.Contains("__ADFvDlg")))
If winExists Then 'this evaluates to true
Dim win = IE.AttachTo(Of IE)(Find.ByUrl(Function(url) url.Contains("__ADFvDlg"))) ' Timeout while waiting for frame document becoming available
End If
1) I have tried using the above code inline or in a STA thread
2) When coded inline, its parent thread is also STA
3) I have tried to increase the default timeout to 8 minutes, same result after 8 minutes have passed
There is no other option for me than to parse this particular popup, since it is a site built with Oracle ADF and, apart from the fact that it is A MESS, it is very strange at times...this popup has a URL that somehow works only once. If I try to use it in another window, no pdf is returned. The same happens when I refresh the popup.
I cannot fetch the PDF in the Temporary Internet Files since it is not there (I suppose this is because the website works under SSL).
Any guidelines or solutions even outside WatiN's scope is more than welcome since I've hit a brick wall.
Technologies: VS2012, WPF
Thanks a lot in advance.
I found it easiest when I tried the same thing by making the pop-up show up as a new tab. That way I could attach to it's URL. From there I would use
File.WriteAllText(fileName, responseDownLoad.Content.ReadAsStringAsync().Result);
Where responseDownload will be a HttpResponseMessage
Issues of robustness, stability and you shouldn't do this aside, has anyone ever filled in a windows credential prompt via code (so that's one that looks like this:)
Is it possible to interact with these dialog boxes through Win32 APIs, or using SendKeys/send mouse / UI Automation? Any ideas / tips anyone has would be greatly appreciated!
I ended up using the UI Automation framework, which allowed me to grab a reference to the credential prompt and then fill it out and complete it that way.
Code snippet:
AutomationElement desktop = AutomationElement.RootElement;
//get all windows on the desktop
AutomationElementCollection windows = desktop.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
foreach (AutomationElement window in windows)
{
if (window.Current.ClassName.Equals("#32770")) //security dialog
{
// access the appropriate AutomationElements to enter credentials here
}
}
To interact with an element, you grab the appropriate Pattern object and call its methods (eg Textboxes have a ValuePattern which has a .SetValue() method.
I also used UISpy to find all the values for things like ClassNames, AutomationIds, etc to help find the correct item through .FindAll() and PropertyConditions objects.
Use something like AHK (Auto HotKey) it is a simple language that can be compiled to an EXE and is designed for automating the keyboard and mouse.
Or you could do it from WPF:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6fc7f1f6-f3e2-4b32-9d2b-9c7a2680e04a/
Or users could simply tick "Remember my credentials"
I'm having difficulties with a set of activeX controls used for automation of lab instruments. I've got them all to work in VB, in forms applications - as they won't work in console applications. My company is working mainly with python and there would thus be a great advantage in writting our drivers in python, mainly for lims integration.
As such, I have tried to embed my activeX controls within ironpython windows forms. I've tried two different ways of introducing my controls in the forms:
1) with clr referencing the Ax dll after using AxImp on the original ocx. Then I add my instance of the control as a form control and try to run it from there. It throws System.Windows.Forms.AxHost+InvalidActiveXStateException.
import clr
clr.AddReferenceToFileAndPath(r"C:\dlls\AxVCode3Lib.dll")
clr.AddReferenceByPartialName("System.Windows.Forms")
import System.Windows.Forms as Forms
import AxVCode3Lib
class activeXform(Forms.Form):
def __init__(self):
a = AxVCode3Lib.AxVCode3()
self.Controls.Add(a)
for i in self.Controls:
r = i.Initialize("serial")
print r
form = activeXform()
raw_input(">exit")
2) Using SystemReflection and the ProgID, which throws a generic "Exception has been thrown by the target of an invocation". The following code was introduced in a basic ironpython windows form, however I was not able to add those objects as form controls.
import System.Type
import System.Reflection
import System.Array
oType = System.Type.GetTypeFromProgID("VCODE3.VCode3Ctrl.1")
o = System.Activator.CreateInstance(oType)
args = System.Array[str](['serial'])
try:
r = oType.InvokeMember("Initialize", System.Reflection.BindingFlags.InvokeMethod, None, o, args)
print r
except Exception as e:
print e
Now I suspect both those examples fail due to some properties missing in my forms. However I can't figure out what it is, in particular in case 1 when the instance is actually added as a control, it seems I am very close.
You cannot use the methods of the ActiveX control until its native handle is created. In other words, not until after you call the form's Show() method.
Make sure you get the basic outline of a Winforms application correct, peek in the Program.cs source code file of a sample C# Winforms app. The Application.Run() call is essential. Use the form's Load event (or override OnLoad) as the first event where you can actually start using the control.