How to create a window on Internet Explorer - c#

I try to create a window on Internet Explorer,just like this:
I wrote this with C++
But I do not know how to design a dialog with MFC,I want to develop this program with c#.
I already know that using BHO can do this work.Here is the core code:
public int SetSite(object site)
{
if (site != null)
{
webBrowser = (SHDocVw.WebBrowser)site;
webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
}
else
{
webBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
webBrowser = null;
}
return 0;
}
onDocumentComplete :
SHDocVw.WebBrowser webBrowser;
public void OnDocumentComplete(object pDisp, ref object URL)
{
Form form1 = new Form();
IntPtr p = new IntPtr(webBrowser.HWND);
Control con = Control.FromHandle(p);
con.Controls.Add(form1);
}
But this do not work.Please tell me waht's wrong with my code and how can I do this work.Thank you.

Related

Close SaveFileDialog/OpenFileDialog programmatically without using pinvoke

Due to some requirements, I have to close SaveFileDialog programmatically without using PINVOKE.
Is there any way to close SaveFileDialog other than using the PINVOKE way?
I had tried to close the owner form of the SaveFileDialog, but the SaveFileDialog still there.
What I had tried:
Close the form which execute the ShowDialog() of SaveFileDialog.
SaveFileDialog.Dispose()
Closing the owner window passed to the ShowDialog(owner); method should work. For example:
private static Form CreateDummyForm(Form owner) {
Form dummy = new Form();
IntPtr hwnd = dummy.Handle; // force handle creation
if (owner != null) {
dummy.Owner = owner;
dummy.Location = owner.Location;
owner.LocationChanged += delegate {
dummy.Location = owner.Location;
};
}
return dummy;
}
[STAThread]
static void Main() {
Form form = new Form();
form.Size = new Size(400,400);
Button btn = new Button { Text = "btn" };
btn.Click += delegate {
SaveFileDialog fsd = new SaveFileDialog();
int timeoutMillis = 5000;
Form dummy = CreateDummyForm(form); // Close disposes the dummy form
Task.Delay(TimeSpan.FromMilliseconds(timeoutMillis)).ContinueWith((t) => { dummy.Close(); dummy.Dispose(); }, TaskScheduler.FromCurrentSynchronizationContext());
fsd.ShowDialog(dummy);
fsd.Dispose();
};
form.Controls.Add(btn);
Application.Run(form);
}
If you use visual studio designer to add a SaveFileDialog, your Form will have a field with this dialog during the life time of your form.
It is way more efficient and way more easier create the SaveFileDialog only when needed. If you do this in a using statement, you won't have to take care of Disposing it, and certainly won't need PInvoke
private void MenuItem_FileSaveAs_Clicked(object sender, ...)
{
using (var dlg = new SaveFileDialog())
{
dlg.FileName = this.FileName;
dlg.InitialDirectory = ...
dlg.DefaultExt = ...
...
// Show the SaveFileDialog, and if Ok save the file
var dlgResult = dlg.ShowDialog(this);
if (dlgResult == DialogResult.OK)
{
// operator selected a file and pressed OK
this.FileName = dlg.FileName;
this.SaveFile(this.FileName);
}
}
}

Using external forms in WPF

Just a quick question are there any issues using a Form to create a Web browser in a WPF application and what are the reasons behind it. Or is it a badly designed application? It is the only one I have seen though I have never seen such a implementation. What could be the WPF equivalent to this?
here is some example code of a WPF Application using a Form.
using System.Windows.Forms;
private static long RunWebBrowserFormAndGetCode()
{
Form webBrowserForm = new Form();
WebBrowser webBrowser = new WebBrowser();
webBrowser.Dock = DockStyle.Fill;
long userID = 0;
var uri = new Uri(#"https://somesite.com/get");
webBrowser.Url = uri;
webBrowserForm.WindowState = FormWindowState.Maximized;
webBrowserForm.Controls.Add(webBrowser);
webBrowserForm.Icon = new System.Drawing.Icon(#"bla.icon");
WebBrowserDocumentCompletedEventHandler documentCompletedHandler = (s, e1) =>
{
try
{
string[] clientID = webBrowser.Url.Segments[2].Split('/');
if (clientID[0].All(char.IsDigit))
{
userID = Int64.Parse(clientID[0]);
webBrowserForm.Close();
}
}
catch (Exception)
{
return;
}
};

How to implement a userdefined cursor in C#?

I want to define my own Cursor as the Current Cursor in my WPF Application, but wenn I try to create a new Cursor Object from my .cur File, I get an Error.
My Code is
private void NewFile()
{ ...
iEvent_dragdrop = (HTMLDocumentEvents2_Event)doc;
iEvent_dragdrop.ondragstart += new HTMLDocumentEvents2_ondragstartEventHandler(IEvent_ondragstart);
}
private bool IEvent_ondragstart(IHTMLEventObj pEvtObj)
{
x_start = pEvtObj.x; // Read position of Mouse
y_start = pEvtObj.y;
....
if (File.Exists("MyCursor.cur"))
{
System.Windows.Forms.Cursor myCursor = new System.Windows.Forms.Cursor(GetType(), "MyCursor.cur");
System.Windows.Forms.Cursor.Current = myCursor;
//System.Windows.Forms.MessageBox.Show("File exist");
}
else System.Windows.Forms.MessageBox.Show("File does not exist");
return false;
}
When I try to drag the HTML Object, I get the error System.NullReferenceException wasn´t handled in the source-code. But I tested if the File exists ....
Can anyone tell me, what´s my mistake?
Thanks!
I recommend searching the web before asking. First search result for the title explains everything you should need to know.
Try this out:
class Program {
[System.STAThread]
static void Main(string[] args) {
byte[] cursorBytes = new System.Net.WebClient().DownloadData(#"https://github.com/tlorach/nvGraphy/raw/master/cursor1.cur");
System.IO.Stream cursorStream = new System.IO.MemoryStream(cursorBytes, false);
System.Windows.Forms.Cursor cursor = new System.Windows.Forms.Cursor(cursorStream);
System.Windows.Forms.Form mainForm = new System.Windows.Forms.Form();
mainForm.Cursor = cursor;
System.Windows.Forms.Application.Run(mainForm);
}
}
Or this:
class Program {
[System.STAThread]
static void Main(string[] args) {
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Filter = "Cursor (*.cur)|*.cur";
switch(openDialog.ShowDialog()) {
case System.Windows.Forms.DialogResult.OK:
System.Windows.Forms.Cursor cursor = new System.Windows.Forms.Cursor(openDialog.FileName);
System.Windows.Forms.Form mainForm = new System.Windows.Forms.Form();
mainForm.Cursor = cursor;
System.Windows.Forms.Application.Run(mainForm);
break;
}
}
}

Showing Form from a thread not rendering perfectly

I am developing a chat application using jabber-net opensource library..
my aim is to display a form (chat window ) when a message is coming.
But when I use this code, Form appears in the task bar,,, not perfectly rendered...
seems like this... More over I can see the form only when I mousehover the Icon on taskbar (Hail Windows 7)... Any form are like this...
Click here for Output Image
my code is this...
public jabber.client.JabberClient jabberClient1;
jabberClient1.User = UserName;
jabberClient1.Password = Password;
jabberClient1.Resource = resource;
jabberClient1.AutoRoster = true;
jabberClient1.OnMessage += new MessageHandler(jabberClient1_OnMessage);
private void jabberClient1_OnMessage(object sender, jabber.protocol.client.Message msg)
{
try
{
chatWindow chw = new chatWindow();
chw.Left = 0;
chw.Top = 0;
chw.TopMost = true;
//chw.LoadChat(msg.From.User, msg.From.Bare, "0");
//chw.SetMessage(msg);
chw.Show();
}
}
you have to use chw.ShowDialog()
or use if invokerequired
private delegate void dlgInvokeRequired();
public void InvokeMethode()
{
if (this.InvokeRequired == true)
{
dlgInvokeRequired d = new dlgInvokeRequired(InvokeMethode);
this.Invoke(d);
} else
{
chatWindow chw = new chatWindow();
chw.Left = 0;
chw.Top = 0;
chw.TopMost = true;
//chw.LoadChat(msg.From.User, msg.From.Bare, "0");
//chw.SetMessage(msg);
chw.Show();
}
}
I have solved it myself...
I have to use
JabberClient1.InvokeControl = FormInstance;
and, the FormInstance should be shown Before the chat window appears....
ie, It can be the contact window (Roster)....

Open Windows Form with opcv From wpf windows

note I am new in Wpf >
I have project that decode qr code by using opencv library throw web cam >
and it running successfully
now I wanna to using this project in new Wpf project >
after adding new wpf project and make reference to WinForms application >
and this my simple code to open WinForm >
public void runnow(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CameraCapture.cameraCapture()); }
by ruining give me this exception >
The type initializer for 'Emgu.CV.CvInvoke' threw an exception.>
what can I do for solve this
C# code
public partial class CameraCapture : Form
{
Capture capture;
bool Capturing;
Bitmap bimap;
private Reader reader;
private Hashtable hint;
libAES libEncryption = new libAES();
string Mykey = "";
public static String dataDecrypted="";
public CameraCapture()
{
InitializeComponent();
}
private void Mains(object sender, EventArgs arg) // Start function main to encode Qr code
{
Image<Bgr, Byte> image = capture.QueryFrame();
if (image != null)
{
bimap = image.ToBitmap();
pictureBox1.Image = bimap;
reader = new QRCodeReader();
hint = new Hashtable(); // Add some elements to the hash table. There are no duplicate keys, but some of the values are duplicates.
hint.Add(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
RGBLuminanceSource source = new RGBLuminanceSource(bimap, bimap.Width, bimap.Height); //This class is used to help decode images from files which arrive as RGB data from* Android bitmaps. It does not support cropping or rotation.
BinaryBitmap img = new BinaryBitmap(new GlobalHistogramBinarizer(source));
Result result = null;
try
{
result = reader.decode(img, hint);
dataDecrypted = libEncryption.Decrypt(result.Text, Mykey);
}
catch
{
dataDecrypted = "";
}
if (result == null)
{
label1.Text = " no decode";
}
else
{
label4.Text = result.Text;
label1.Text = dataDecrypted;
capture.Dispose();
}
}
} // end function Main
private void btnStart_Click(object sender, EventArgs e)
{
if (capture == null)
{
try
{
capture = new Capture(); // **the exption thown here**
}
catch (NullReferenceException exception)
{
MessageBox.Show(exception.Message);
}
}
if (capture != null)
{
if (Capturing)
{
btnStart.Text = "Start Capture";
Application.Idle -= Mains;
}
else
{
btnStart.Text = "Stop Capture";
Application.Idle += Mains;
}
Capturing = !Capturing;
}
}
private void Release()
{
if (capture != null)
capture.Dispose();
}}
If you want to host WinForm in WPF, you need to use host control System.Windows.Forms.Integration.WindowsFormsHost
WPF provides many controls with a rich feature set. However, you may
sometimes want to use Windows Forms controls on your WPF pages. For
example, you may have a substantial investment in existing Windows
Forms controls, or you may have a Windows Forms control that provides
unique functionality.
Example code
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Create the interop host control.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
// Create the MaskedTextBox control.
MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");
// Assign the MaskedTextBox control as the host control's child.
host.Child = mtbDate;
// Add the interop host control to the Grid
// control's collection of child controls.
this.grid1.Children.Add(host);
}
Check =>
http://msdn.microsoft.com/en-us/library/ms751761.aspx

Categories

Resources