Print Web browser Control in WPF - wpf

In Application window there is scroll view ,on taking the print out of the window whole scroll view didn't come ,only the window size print is coming ,I can take the print of scroll view only but is there any way to take the printout of whole scrollview with the window?
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
// printDialog.PrintVisual(canvas, "Scrollview Description");
printDialog.PrintVisual(this, this.Title);
}

I didnot get any answer for this question even after editing it, well I look into google several time and after multiple failed attempt finally I got a solution , problem in my code was that ,i used webbrowser control inside scroll view , after reading one solution i remove the scrollview and put code for taking print out of browser control and it works, this is the code i use
mshtml.IHTMLDocument2 doc = webbrwsr.Document as mshtml.IHTMLDocument2;
doc.execCommand("Print", true, null);
for this i have to include one .NET reference Microsoft.mshtml
hope this solution will work for any one else facing the same problem.

Related

Cannot change window size and position between hide and show

In my scenario I have a window which I want to use to display on two different ways. One way when the window has WindowState = WindowState.Normal; and the other way where the window has Normal or Maximised state.
I want to save the Size and Position of these two window modes so for the user it would seem like two separate windows.
I have two different issues around this.
1) When I initialize the window after Show() has been called, there is a small flicker which on slower machines isn't such a short flicker. In order to fix this I wanted to set up the window while it is hidden like so:
((SecondWindowViewModel)this.DataContext).LoadWindowPosition(mode);
this.Show();
if I do this an even worst problem comes up
2)In this case window 1 which is in normal mode has some random height set on drag after window 2 has been closed in Maximized mode.
I've created a sample application what you can find here, where you can see the exact problem, I've also written down the steps to reproduce as well.
EDIT
Implemented Maxims Changes and removed unnecessary references
Haven't had yet the time to completely solve your issue, but one issue you have in your code is that you do not set the window state accordingly to your mode. In SecondWindow.xaml.cs, your code should look like this:
if (mode == WindowViewMode.Normal)
{
WindowState = WindowState.Normal;
}
else
{
WindowState = WindowState.Maximized;
}
if (this.IsVisible)
{
this.Hide();
}
else
{
((SecondWindowViewModel)this.DataContext).LoadWindowPosition(mode);
this.Show();
}
Still need to take a look at the problems your LoadWindowPosition is causing.

wpf Richtextbox selected text distorted

I am experimenting with the WPF RichTextBox and it shows some text in my application. I notice that When I select some text in that RichTextBox, the selected text gets distorted and the text below to that also gets distorted as shown below.
After I deselect that and scroll it comes back to normal.Is there anyway to avoid this distortion?
Now I solved this problem by myself. I just added the following single line of code in my application and make it works. We need to disable hardware acceleration in screen settings using this.
RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly;
This helped me a lot to fix my issue and it works for me.

How to Move content to next page in WPF Form

I am new in WPF if there is something wrong please co-operate.Here i require some idea from experts.
I am working on one application in which i have to show some content on WPF form after filling the fields present on the form.On the same form i also have a print option.
Check this image.This is my form here part in the red block is generated at runtime.When i click on the print button it only show the visible part on the paper and skip the remaining part.
Problem :
How i can move the remaining part of the form which is under scroll to next page when i click on print.
For example in the given image we can see only 2 bulls eye completely and next 2 partially.How i can shift this remaining part to next page only when i click on print.
The answer is quite easy : don't rely on your window to do the printing, but build the visual you want then print it.
For instance, you must have a function that creates dynamically the circles and so on, then adds them to a Panel. What you might do is to print the Panel.
Or if you prefer, you might build Dynamically a new window, where you put all the Data you want printed as you want, then print the window. The advantage of this method is that it is more flexible for the content (if you want a header/footer) and also you can watch the content easily for debug. Note that even if the Window content is dynamic, you can have a base window for printing that avoids you to do too much xaml with code (expl : you might have TextBox bound to a PrintTitle property that you setup in the constructor of the Print Window...).
Notice that visual that were not rendered on screen will not print. Be sure, to avoid the common issues, to have a look at this article from this great site, switch on the code, here :
http://www.switchonthecode.com/tutorials/printing-in-wpf
Edit (reply to the question in comment):
1) If you have fixed number of bulls eyes, just make one Window for that number and Print it, this is waaaay easier.
2) To put Visuals in pages instead of rows, you'll have to rely on page Width/Height. What matters is the size of your control vs size of page. In the example, they build (in OnRender) Controls having LineHeight, LineWidth as size. Do the same : Try to put as many line of control as you can such as
(Control Height + margin )*NumberOfControlPerPage < Page Height.
Then you have to change OnRender to render controls instead of Rows made with rectangle+text. Pack your controls two by two in Horizontal StackPanels Then pack those StackPanel into a vertical StackPanel, then render. You have to keep track for each page which control was rendered last, then resume rendering at the following control.
Please follow this link.This is the basic which i got after searching in web world.Using this basic detail you can do any thing with print in WPF

Take a screenshot with silverlight 5 / xna

I'm trying to take a screenshot of a subclassed XNA DrawingSurface element from within a silverlight 5 application. This sl app will run inside an aspx page.
Here's what I've tried so far without success:
WriteableBitmap bmp = new WriteableBitmap(LayoutRoot, null);
testImage.Source = bmp;
with LayoutRoot being the parent control on the silverlight page and testImage is just an Image control on the page to see if the screenshot is correct.
This will render all the silverlight controls on the page just fine, but the drawingsurface part remains empty. In other words the XNA content is not rendered into the image.
I've also tried to render the XNA content to a RenderTarget2D, but the silverlight version of the render target does not seem to have any methods to save the data. I saw some WP7 examples that used a method called SaveAsJpeg(), but that doesn't seem to be available in the SL5 version of the class.
I would appreciate any help with this.
Greets,
Floris
One possibility would be to draw your screen into a RenderTarget2D and then use the method SaveAsPng like:
using (Stream stream = File.OpenWrite("filename.png"))
{
renderTarget2D.SaveAsPng(stream, renderTarget2D.Width, renderTarget2D.Height);
}

Windows Forms User Control not painting well in WPF application

So, this is the issue:
I have a Windows Forms User Control that I placed in main Window of my WPF application. I override paint method of User Control. It paints ok in "ideal" case. But, after showing the control in main window, I added MessageBox. This is the code snippet:
board = new BoggleBoard(Boggle.CurrentGame.Size);
boardHost.Child = board;
MessageBox.Show("You have " + time + " seconds to find as many words as you can. Click OK when you are ready to play);
If I don't show MessageBox, everything is ok. But with the code above, after MessageBox is shown, my control is painted, but just like boardHost (Windows Form Host) has lower opacity, so I get dark area around the control. I say "like" cause I tried with
boardHost.Opacity = 1;
but it doesn't help, I still get the same thing.
What might cause this problem?
Here is a screenshot. As obvious, the dark area around the board should not be there. And it is not visible if I don't show MessageBox after it is drawn.
http://i.stack.imgur.com/RZs2W.png

Resources