Printing in WPF - c#

I know how to invoke the print dialog and do other things. My question is : what is the best practice to print a WPF page.
To invoke another page with all
margins, formatting and layout worked
out. And then call the print on that
To invoke a user control with all
margins, formatting and layout worked
out. And then call the print on that
To invoke a window with all
margins, formatting and layout worked
out. And then call the print on that
Or any other preferred way of
doing this.
Please let me know how you approach printing ?

In WPF you have many options to print. The question which one is best suited dependes IMO extremely on the content to be printed and the desired quality.
Printing visual
If ouy want to print something that looks exactly like the representation in the UI, then you could try printing directly the visual that builds the root-content of your print-content. If you have positioning problems and have to correct properties of this visual, I would recommend to created a memory-copy of it, transform the copy and then print out this copy. Printing out visuals has IMO many drawbacks. I seldom use it except for short hacks and mini-tools. If I use, I mostly have to create the mentioned invisible copy and print out this. If you go this way, take care that you have to call Measure and Arrange from code.
Printing FlowDocument
Another possiblity is to print out a FlowDocument. If you have something that fits into FlowDocuments, I find this an easy way to create nice content to print out. Mostly if you have some simple reports, list ith some images, this way is a nice way. The print-quality is good and many complex operations will be done for you automatically as long as you don't want to override behaviour, and also this is possible. The quality of such print-out's is IMO very good.
XPS Printing
Hovever if you have complex content and you want it to print out nicely, you have to build XPS-content and print it over the XPS Print Path. This way, you have all possibilities and options, the qualitity is very good but you have the most work to do. I don't like it much, but mostly it's the way I take to print.
I assume, that your question mainly concerns the first block, however I think it could be helpfull to see which options you have, if printing the visual is not enought (for me it's very seldom enough). Hope this answers your question.

Related

How to capture a specific HTML Element as an image?

I asked a question similar to this here:
Is there a way to show part of one form on another form?
This would work great if I could guarantee that every single person who uses this application uses the same monitor as I do.
But I'm a realist so I'll get to the nitty gritty.
If I can (which I can) identify the specific HtmlElement of the WebBrowser control document that houses what I want to create an image from, is there any way that I can create an image from that specific HtmlElement?
I've seen some stuff about IExtractImage but everything I've seen makes my head hurt a little bit. If someone could dumb that down (if that's what I'm looking for) so that I could use that, that would be super good.
To be a bit more clear what I'm hoping for is something like HtmlElement.DrawToBitmap()

Kind of live debugging possible? (Especially UI manipulation)

I would like to know if there is a way to manipulate an App's UI live while running?
I am not a designer and I have many problems sometimes regarding matching colours etc.
The next problem is that anytime I would like to change e.g. the colour of a control I have to quit the App then go to VS2012, apply my changes, build and execute it again to see simple changes.
I know that I see any changes in the designer but I have to see the resulting screen to get an impression of the whole.
Is there a way to achieve this?
Add a secret keypress while Debug flag is set, that raises a form and allows you to select controls and expose a property sheet for them. Be a bit of work to get right, and a good stick of code even using reflection. Might be better off with a storyboard type app to do your designing.
Unlike styles in WPF which can be dynamically adjusted (which made this type of run-time adjustment simple), there isn't as elegant of a solution for Windows Store apps. Ideally, you'd have all of your UI and colors, etc. defined in XAML files and not settable through other means (as it becomes a longer term maintenance issue).
I'd suggest just adding enough test data and configuration so that you can see the look and feel of the pages (with colors, etc.) at design-time. Blend and Visual Studio are now quite good at showing a very reasonable near final rendering of the elements of the application. It's generally not too difficult to do anymore.
One thing I've done in the past was to make a single page/form that contained all of the styles and controls in a large scroll viewer. Then, we set it so it was configurable to the be the first thing to run. The tweak/build cycle was pretty fast, and the results were still very manageable.

c# what wpf control to use for showing text file

Whats the best control to use in wpf applicaion for loading big text files into? And its not just simple as that, i need to be able to hightlight/change background/color of some lines. I was thinking of using RichTextBox.. but though i just come and ask what other people think.
I dont need to edit the text, just highlight lines with errors
EDIT: Ah i forgot to mention, text file content is processed by the program what will change the background color of some lines. Would be nice to give user a change to do that allso when the program failes to find some error lines.
EDIT2: The application is for parsing log files, so the files might be big, over 50 000 lines..
I didn't understand if you only want to display files or allow user to edit them.
Either way, maybe ScintillaNET would be useful to you.
Well known text editor, Notepad++ uses Scintilla :)
You could grab notepad++ sources at http://sourceforge.net/projects/notepad-plus/, but It's written in c++, so the API would be a bit different than in .NET.
EDIT
Notepad++ is capable of displaying files of around 100-200MB. If you need better than that, you would need to implement some kind of paging technology/virtual mode (do not load entire file, only the portion that would be displayed on the screen depending on current scrollbar position)
If Scintilla seems an overkill, then maybe you'd be better off with simply building a DataGrid. If log entries consist of several fields, these could become columns and would be even easier to read. If you want to go that route, first parse your log file into a List of LogEntry, then use databinding to bind to it. You could read your file incrementally as new lines get added for better user experience.
I think RichTextBox will be most appropriate in this case but as CharithJ said you cannot load whole log in to it at one time it will be too much performance hit.
What I would do in scenario like this is .. I will have a Richtextbox along with two small up and down buttons for kind of scroll. I will only load some text of log at one time let's say x number of characters. Once user clicks the up or down scroll button, I would remove some text from start and end depending on which button user have clicked, up or down and based on that I would remove some text from richbox and append some other.
Either you can do something like I suggested above, or one other way could be to extend the RichTextBox control and implement logic that only forwards a limited text to RichTextBox for display at a time. You can handle scroll related events to make changes in RichTextBox's content.

WinForms Invoke/BeginInvoke

I have a C# win form where I read a file and show lines in a datagridview. Everything works fine.. and I use delegate and Invoke for displaying the lines as they are being read. It also shows a progressbar and does some other stuff like calculating line length and parse the lines to extract certain fields from each line.
Just curious, if anybody has tried this kind of implementation. Currently my app reads a 250MB file (having ~ 12000 lines) in little over 3 minutes (Win 7 32bit/celeron 2.66Ghz/4GB). I was wondering if it is possible to reduce the time - more of like by changing the way I implemented it.
Well, you're quite right to have second thoughts about this. What's probably the toughest read possible, War and Peace by Tolstoy, has roughly half a million words. What you're dumping to the screen is a hundred times more. It doesn't really matter how long it takes to put this much info on the screen, it will take your user a wholeheckofalot longer to even scroll through it.
I personally hesitate to ever put anything more than, oh, 50 items in a list. 100 tops. Beyond that, it becomes sheer torture for a human being.
To get there, allow your user to filter content, gradually drilling down in the huge result set to a relevant item. Exactly what that ought to look like is not clear from your question. Think about it for a bit, I'm sure you'll come up with something.
Ok...I love these questions. Why are you showing a user 250mbs of data in a data grid? There is NO WAY that an end user can process understand 250mbs of data in a grid. If the end user is pressuring you for this feature you need to address it with them from a "this is a bad idea" standpoint. Performance problems can usually be solved in most cases by educating the user. :-D
First, you need not load the entire file, nor display it. You can load portions of it for display. i.e. in the grid, you cannot show the enire file anyway, the screen is only so big. Consider loading only what you display (plus a few extra lines for smooth scrolling) and load the rest on demand as the user is scrolling through the content. The user will be unaware that the file is only partially loaded.
Even nicer would be if you have some logical grouping, in a book you could display the chapters, in a financial type application, either by account or time period.
Maybe something like VirtualMode can helps you:
Virtual mode is designed for use with very large stores of data. When the VirtualMode property is true, you create a DataGridView with a set number of rows and columns and then handle the CellValueNeeded event to populate the cells. Virtual mode requires implementation of an underlying data cache to handle the population, editing, and deletion of DataGridView cells based on actions of the user. For more information about implementing virtual mode, see How to: Implement Virtual Mode in the Windows Forms DataGridView Control.
P.S. About performance: no one except profiling tool can't helps you to solve any performance-related problems.

Replacement for yes/no/cancel MessageBox (C#)

I am looking for a decent replacement for the standard windows YES/NO or YES/NO/CANCEL MessageBox.
I have often seen these standard dialogs misused in ways such as: "To save in plain text answer YES, or to save in html answer NO". Obviously, the text should read "Save As: and the "buttons should be labeled "Text" and "HTML". It is not a yes/no question that is being asked, and although it could be phrased that way, it would not be easy to read and understand.
Microsoft gives no way to change the text on the buttons. There is no fast/simple way to build a replacement from scratch... as evidenced by the number of applications using the awkward style mentioned above.
Is there any free C# replacement dialog or MessageBox out there that lets you at least:
- specify the number of buttons
- specify the text to appear on each button
- specify the default button
I have looked and have been unable to find one.
(I would build one myself, but I am not familiar enough with all the behaviors that a fully functional control should have, since I only need/use/know a small subset. Two examples I don't use: themes and internationalization. I need something that my coworkers will also want to use.)
Check out Dissecting the MessageBox on CodeProject. The project is a bit dated, but it's pretty much exactly what you're looking for and it shouldn't take much to update it.
Depending on your target platform, a task dialog may be a good way of doing this. There is a .NET wrapper for task dialogs in the Windows API Code Pack. However these are provided only in Windows Vista and above, not in XP or 2003.
Frankly, it is not that difficult to create such a Messagebox yourself, we have such a thing working in the current app we are developing.
What you need is a FlowLayout for the buttons that will auto-align any buttons you create. Our API then has something like (params Tuple<string,DialogResult>[] buttons)
Tuple is a helper class that contains two values. The string is the Text of the button, the Dialogresult is the one our messagebox returns when the button with said text is clicked.
I agree with Frank. It wouldn't be too difficult to create your own generic form that handles this for you. Without getting into code, the form should do the following
1) Have a property to set the message you want to show to the user.
2) Have a method for adding buttons, with 2 arguments, one for the button text, and one for the dialog result
3) When the form is displayed, it should be in modal dialog mode so that the rest of the application is inactive while until one of the options is clicked.
So, to create a Save As/Don't Save/Cancel, you would add 3 buttons in step 2, all with the appropriate button text and dialog result.
Using Flow layout, you should be able to get it to display properly regardless of the size of the message, or the number of buttons.

Categories