Bringing ToolWindow to Front of Application - vb.net

Basically, in my code I transform my form into a toolwindow in the load event:
Dim lStyleEx As Long = win32.WS._EX_TOOLWINDOW
Call SetWindowLong(Handle, win32.GWL.__EXSTYLE, CInt(lStyleEx))
Const swpFlags As Long = _
win32.SWP._FRAMECHANGED Or _
win32.SWP._NOMOVE Or _
win32.SWP._NOSIZE Or _
win32.SWP._NOZORDER
Call SetWindowPos(Handle, CType(0, IntPtr), 0, 0, 0, 0, CType(swpFlags, win32.SetWindowPosFlags))
(I have a class win32 containing enums for all the constants)
Now, I want to bring this toolwindow to the front with the IVsWindowFrame.Show(), but I'm having trouble getting the actual IVsWindowFrame for my form. After trawling the web I found a snippet on the MSDN forums, but after converting to VBA various issues come up, e.g. I don't know how to properly create a new ServiceProvider:
Dim shell As IVsUIShell = DirectCast(ServiceProvider.GetService(GetType(SVsUIShell)), IVsUIShell)
Dim frame As IVsWindowFrame
Dim guid As New Guid(ToolWindow.GuidList.guidToolWindowPersistanceString)
shell.FindToolWindow(CUInt(__VSFINDTOOLWIN.FTW_fForceCreate), guid, frame)
frame.Show()
Sourced from https://social.msdn.microsoft.com/Forums/vstudio/en-US/d6c2b85c-3a48-48f7-a9f6-a68ea4ef0441/how-to-get-access-to-my-toolwindowpane-object-with-an-ivswindowframe-pointer?forum=vsx
Any help with finding the IVsUIShell of a form or an explanation on this code would be greatly appreciated!
I've also tried something as simple as setting the actual form as an IVsWindowFrame:
Dim windowFrame As IVsWindowFrame = CType(Me, IVsWindowFrame)
windowFrame.Show()
but it throws an exception:
type 'RifleSystem.scrSettings' to type 'Microsoft.VisualStudio.Shell.Interop.IVsWindowFrame'.
btw, I'm running 64-bit VS2013 if that helps.

Okay I feel stupid now. There's a property Form.Owner which does exactly what I need in this scenario, and it works perfectly! No need for all this convuluted stuff :D
Here's the link in case anyone is interested:
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner%28v=vs.110%29.aspx

Related

How to convert 'Integer' to 'Timestamps'?

**I did a effort but got error while testing
Error BC30311 Value of type 'Integer' cannot be converted to 'Timestamps'.**
I have tried this :
Public Sub test()
client = New DiscordRpcClient("test")
client.Logger = New ConsoleLogger
client.Initialize()
client.SetPresence(New RichPresence With {
.Details = "test",
.Assets = New Assets() With {
.LargeImageKey = "test",
.LargeImageText = "test",
.Timestamps = 0
})
Dim timer = New System.Timers.Timer(150)
AddHandler timer.Elapsed, Sub(sender, args)
client.Invoke()
End Sub
timer.Start()
client.Invoke()
End Sub
here the problem is "Timestamps = 0", So how can i solve.
im trying to use discord rich Presence elapsed timer.
This is actually more of an issue with this Discord-RPC-Csharp library than it is with C#. That being said, I looked into it anyway.
The example code given in the repository for this project shows this as an example
Timestamps = Timestamps.FromTimeSpan(10)
See the error you got is an error specific to C# for when trying to assign one value type to a complete different type. An Integer is not a Timestamp, and a Timestamp is not an Integer. So we need to figure out what Timestamps actually is. So the best way to do this is to right-click on Timestamps and go to "Go To Definition" or hit F12 on it.
Now in RichPresence.cs you can see the class definition for Timestamps. You will see four options
Timestamps.Now
Timestamps.FromTimeSpan(double seconds)
Timestamps.FromTimeSpan(Timespan timespan)
in addition to a constructor
new Timestamps(DateTime start, DateTime end)
Since you haven't told us what this timestamp is supposed to represent in your code, I'll leave it to you to figure out which one of these you want/need to use.

How to access nsIHTMLEditor interface in GeckoFX?

I am trying to make a WYSIWYG HTML-editor by embedding GeckoFX in a Windows Forms application in VB.NET.
The code goes like this:
Dim Gbrowser As New GeckoWebBrowser
Gbrowser.Navigate("about:blank")
...
Gbrowser.Navigate("javascript:void(document.body.contentEditable='true')")
How can I activate and access the nsIHTMLEditor interface from within my application?
Thank you.
UPDATE
This code does not work:
Dim hEditor As nsIHTMLEditor
hEditor = Xpcom.GetService(Of nsIHTMLEditor)("#mozilla.org/editor/htmleditor;1")
hEditor = Xpcom.QueryInterface(Of nsIHTMLEditor)(hEditor)
hEditor.DecreaseFontSize()
Error in the last line: HRESULT E_FAIL has been returned from a call to a COM component.
nsIHTMLEditor is likely a per browser instance rather than a global instance (like things returned by Xpcom.GetService)
One can get a nsIEditor like this by (by supplying a Window instance)
var editingSession = Xpcom.CreateInstance<nsIEditingSession>("#mozilla.org/editor/editingsession;1");
nsIEditor editor = editingSession.GetEditorForWindow((nsIDOMWindow)Window.DomWindow);
Marshal.ReleaseComObject(editingSession);
(or you can just call the nsIEditor GeckoWebBrowser.Editor property.)
You may be able to cast this nsIEditor to a nsIHtmlEditor (although I have yet to try it)
GeckoWebBrowser browser = .....;
// Untested code
nsIHTMLEditor htmlEditor = (nsIHTMLEditor)browser.Editor;
Update:
The VB code from #GreenBear
Dim gEditor As nsIHTMLEditor:
gEditor = Gbrowser.Editor:
gEditor.DecreaseFontSize()

Disable WebBrowser caching

I am new here and really want your help.
I've been trying to disable my webbrowser's cache but I get overload resolution failed because no accessible "Navigate" without a narrowing conversion. I'm stuck and I don't know what to do anymore, I did search all the possible solutions but found no answer.
Here's my code:
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Const navNoReadFromCache As Long = 4
Const navNoHistory As Long = 2
Const navNoWriteToCache As Long = 8
Dim navflags As Long
navflags = navNoHistory + navNoWriteToCache
WebBrowser1.Navigate("url", 4)
End Sub
End Class
Original Error message is:
Error 2 Overload resolution failed because no accessible 'Navigate' can be called without a narrowing conversion:
'Public Sub Navigate(urlString As String, newWindow As Boolean)': Argument matching parameter 'newWindow' narrows from 'Integer' to 'Boolean'.
'Public Sub Navigate(urlString As String, targetFrameName As String)': Argument matching parameter 'targetFrameName' narrows from 'Integer' to 'String'.
.NET WebBrowserControl doesn't have overload what accept int or long argument.
So, you can't set BrowserNavConstants (this for IWebBrowser2 not .NET WebBrowserControl) value to .NET WebBrowserControl.
I found following page:
http://msdn.microsoft.com/en-us/library/40x214wa%28v=vs.110%29.aspx
The WebBrowser control stores Web pages from recently visited sites in a cache on the local hard disk. Each page can specify an expiration date indicating how long it will remain in the cache. When the control navigates to a page, it saves time by displaying a cached version, if one is available, rather than downloading the page again.
Use the Refresh method to force the WebBrowser control to reload the current page by downloading it, ensuring that the control displays the latest version.
Updated.
I try the code following, that looks like work fine :
private void button1_Click(object sender, EventArgs e) {
webBrowser1.Navigate("http://www.google.co.jp");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
webBrowser1.Refresh(WebBrowserRefreshOption.Completely);
}
This loads page two times.
So after some time of searching and testing different methods I came with a good result.
Shell("RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 8", vbHide)
This is the code I used to delete the cache that held my web browser on a blackscreen.
What it does is search for the Internet Explorer's Temporary files and deletes them, the vbHide has to be kept next to the comma to work, what is does is hide the window that pops us telling you it deletes temp files.

How do you remove this text in vb net?

Whenever I use streamwriter to put a textbox value into a .txt file it goes like this:
System.Windows.Forms.TextBox, Text: dadadadaad what is wrong here?
Dim Na_1 As New IO.StreamWriter(folder & "\name.txt")
Na_1.WriteLine(TextName)
Na_1.Close()
You have flush the StreamWriter before you close it:
Na_1.Flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.
Source: MSDN
So this should be your code:
Na_1 As New IO.StreamWriter(folder & "\name.txt")
Na_1.WriteLine(TextName)
Na_1.Flush()
Na_1.Close()
Alternatively you can use the Using-statement which automatically flushes and closes the stream (this is the better practice):
Using Na_1 As New IO.StreamWriter(folder & "\name.txt")
Na_1.WriteLine(TextName)
End Using
Following the comments of Ric and ForkAndBeard I think I misunderstood the problem:
Your problem is that you are calling the TextNameobject of the TextBox-Class directly via Na_1.WriteLine(TextName).
Now since you can't write an object to a file, the runtime simply call the ToString()-method of the Class TextBox which inherits from Object.
The result will be following:
"System.Windows.Forms.TextBox, Text: "
or generally:
"YourNamespace.YourClass"
Source:MSDN
If you want to have the text of the TextBox, you have to call the property TextBox.Text of the object:
Na_1.WriteLine(TextName.Text);

Converting TIFF to PDF results in Insufficient Data For Image

I have a utility that converts batches of TIFF images to PDFs using the PDFSharp library. The following code performs the actual conversion. When I open the resulting PDF files in Acrobat Reader, I receive an error message for certain ones stating "Insufficient Data for Image." Others are fine.
What could be causing this? Is there anything I'm missing in the code that could prevent this?
Public Shared Function ConvertImageToPDF(ByVal img As Image) As Byte()
Using ms As New MemoryStream()
Using pdf As New PdfDocument()
Dim pageCount = GetPageCount(img)
For index = 0 To (pageCount - 1)
Dim page = New PdfPage()
Using sourceImage = GetPage(img, index)
Using pageImage = XImage.FromGdiPlusImage(sourceImage)
page.Width = pageImage.PointWidth
page.Height = pageImage.PointHeight
pdf.Pages.Add(page)
Using xgr = XGraphics.FromPdfPage(pdf.Pages(index))
xgr.DrawImage(pageImage, 0, 0)
End Using
End Using
End Using
Next
pdf.Save(ms, False)
pdf.Close()
End Using
Return ms.ToArray()
End Using
End Function
Public Shared Function GetPageCount(ByVal img As Image) As Integer
If (img Is Nothing) Then
Return -1
End If
Return img.GetFrameCount(FrameDimension.Page)
End Function
Public Shared Function GetPage(ByVal img As Image, ByVal pageNumber As Integer) As Image
img.SelectActiveFrame(FrameDimension.Page, pageNumber)
Dim ms = New MemoryStream()
img.Save(ms, ImageFormat.Tiff)
Return Image.FromStream(ms)
End Function
UPDATE:
If I run the same code over the same TIFF files, then the PDF files that were corrupt before are now OK, and ones that were OK before are now corrupt.
UPDATE 2:
After reviewing this connect issue (https://connect.microsoft.com/VisualStudio/feedback/details/584681/system-drawing-image-flags-has-different-value-in-vista-and-windows-7) and the community comment on this MSDN page (http://msdn.microsoft.com/en-us/library/system.drawing.image.save.aspx), it appears the issue is related to an operating system level bug in Windows 7. Can anyone confirm this or offer a workaround?
As stated in my update, after reviewing this connect issue (https://connect.microsoft.com/VisualStudio/feedback/details/584681/system-drawing-image-flags-has-different-value-in-vista-and-windows-7) and the community comment on this MSDN page (http://msdn.microsoft.com/en-us/library/system.drawing.image.save.aspx), it appears the issue is related to an operating system level bug in Windows 7.
This is supported by the comment from PDFsharpTeam.
In addition, when the images are read in Windows XP, the flags property on the image object is set to 77888. On Win7, it is set to 77840. After reviewing the MSDN documentation for the flags property (http://msdn.microsoft.com/en-us/library/system.drawing.image.flags.aspx), the difference is that WinXP flagged the image as a grayscale image (which mine are), but Win7 flagged it as an RGB image. This appears to be a symptom of the problem, but I don't know enough about image formats and color spaces to speak with authority on this.
UPDATE (2014-06-13):
After continuing to experience this problem, I researched a bit further and found a post on the PDFSharp forums mentioning this issue and linking to another post with a fix.
http://forum.pdfsharp.net/viewtopic.php?f=2&t=2729
http://forum.pdfsharp.net/viewtopic.php?p=5967#p5967
Basically, there are two methods in the PdfImage.FaxEncode.cs file that need to be updated.
In both the CountOneBits() and CountZeroBits() methods, replace the following code:
return found + hits;
with
found += hits;
if (found >= bitsLeft)
return bitsLeft;
return found;