.NET Developments - A SearchWinDevelopment.com Blog

.NET Developments:

 

A SearchWinDevelopment.com Blog


A blog on all things .NET, with news and tips about Visual Studio, ASP.NET, Visual Basic programming, C# and .NET architecture.

VB6 Programmers - What happened to Printer.Print?

This post goes out to all you VB geeks that are wondering what happened to Printer.Print in VB.  This may be a dated topic but I have a feeling there are a few out there longing for those VB6 days when the printer was always sitting there loyal and waiting.  The VB6 programmer’s best friend.  Well, when you needed to print something anyway.   Once upon a time you could just write a few lines of code and *poof* you created a page of information for your users.  Now you have this PrintDocument thing and PrintDialogs and PrintPreviewDialogs and Graphics objects and the list just goes on and on.

Let me re-introduce you to printing in .NET.  Once you get through the slight grade of the learning curve, you’ll be convinced that .Net printing is better than anything you did with the printer object in VB6.

The task - print a smiley face on a piece of paper.  Lines of code in VB6 - about 6.  Lines of code the .net way - about 22 (but you could consolidate…).

That doesn’t sound like a good trade off.  It seems its easier in VB6.  However - what if you wanted to create a bitmap of the smiley face and then use that bitmap in various places as well as print it here and there?  How many line of code do you need now?

 In VB6 - I have no idea.  You would need to drop down to the API level and call graphics functions against a Device Independent Bitmap device context making sure you clean up after yourself in those places where cleanup is necessary.  Then you would need to save that bitmap to a file and/or have an image control somewhere that you could set using the memory bitmap (again using API calls).  Then perhaps you could print the smiley here and there using some similar printing code.

In .NET - its the same 22 lines of code and you can run those lines of code against any “Device Context” (using API terminology) by simply passing a Graphics object to the code that actually creates the smiley.  You could even create a bitmap object and simply use that bitmap throughout your program without ever getting close to the windows API.
Here are my CreateSmiley functions:

private void DrawSmiley(Graphics g, int Width)
{
  Pen p=new Pen(Color.Black);
  SolidBrush b = new SolidBrush(Color.Black);
  SolidBrush YellowBrush = new SolidBrush(Color.Yellow);
  Point Origin = new Point(0, 0);
  Size HeadSize=new Size(Width,Width);
  Rectangle Container=new Rectangle(Origin, HeadSize);
  Point LeftEye=Origin;
  Point RightEye=Origin;
  Point SmileTopLeft = Origin;
  LeftEye.Offset((int)(HeadSize.Width*.25), (int)(HeadSize.Width*.20));
  RightEye.Offset((int)(HeadSize.Width*.65), (int)(HeadSize.Width*.20));
  SmileTopLeft.Offset((int)(HeadSize.Width *.20), (int)(HeadSize.Width * .40));
  Size SmileSize = new Size((int)(HeadSize.Width*.60), (int)(HeadSize.Width*.40));
  Size EyeSize=new Size((int)(HeadSize.Width * .10),(int)(HeadSize.Width * .10));
  g.FillEllipse(YellowBrush, Container);
  g.DrawEllipse(p, Container);
  g.FillEllipse(b, new Rectangle(LeftEye, EyeSize));
  g.FillEllipse(b, new Rectangle(RightEye, EyeSize));
  g.DrawArc(p, new Rectangle(SmileTopLeft, SmileSize), 180, -180);
  b.Dispose();
  YellowBrush.Dispose();
  p.Dispose();
}

private Bitmap CreateSmiley(int Width)
{
  Bitmap Smiley = new Bitmap(Width, Width);
  Graphics g=Graphics.FromImage(Smiley);
  DrawSmiley(g, Smiley.Width);
  g.Dispose();
  return Smiley;
}

Pretty basic stuff and different than you did in VB6. You have access to all the API stuff without dropping down the the API level. Now as far as printing goes - there are a few objects that need your attention. The PrintDocument, PrintDialog, and PrintPreviewDialog objects. The PrintDocument object is the container for all your drawing methods. It handles paging and rendering of the stuff you are printing. The PrintDialog and PrintPreviewDialog objects manage the actual device you are printing to. The PrintDialog as you may have guessed will print to a printer while the PrintPreviewDialog prints to a preview window.

Here is some code that uses a PrintPreviewDialog and calls the printing methods above:

private void button1_Click(object sender, EventArgs e)
{
  PrintDocument pdoc = new PrintDocument();
  // hook up the event handler for the printpage event
  pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
  PrintPreviewDialog pdialog = new PrintPreviewDialog();
  pdialog.Document = pdoc;
  pdialog.ClientSize = new Size(640, 480);
  pdialog.Show();
}
void pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
  Bitmap smiley=CreateSmiley(300);
  e.Graphics.DrawImage(smiley, new Point(150, 150));
  e.HasMorePages = false;
}

The VB.Net code is virtually the same. Just change the declaration variables around, change the curly braces to Sub/End Sub, remove the semi-colons and your 80% done.
This method of printing is easy to hook up and offers a great deal of flexibility but if you want real reporting power - there is no substitute for a good reporting engine such as SQL Server Reporting Services or Business Objects’ Crystal Reports. There are others.  I’m a convert.  I was a Crystal Reports bigot but if you’re using a SQL Server database - you get reporting services for free and I must admit after running SQL RS through its paces - I like it better than Crystal Reports.  That, of course, is my opinion.

mysmiley

Microsoft releases .NET Micro Framework Version 2.5 updates

Microsoft updated the .NET Micro Framework, releasing Version 2.5 with improvements in Web Services and TCP/IP support.

.NET Micro Framework Version 2.5 includes a Web Services on Devices implementation compatible with Windows Vista and supported by the other Microsoft Windows Embedded platforms.

With Web Services on Devices, Micro Framework devices can discover and communicate with other devices on a network without need for user configuration. While some observers initially scoffed at Web services as a ‘real-time’ embedded solution, a growing consensus hold that it is ‘real-time’ enough for a slew of solutions.
 
Also, with Version 2.5, the framework gains native TCP/IP stack support, and, let’s face it, TCP/IP is nothing short of the greatest protocol of all time.

Microsoft also said it has inked a deal with oft-time rival IBM to collaborate on a pre-installed Windows embedded-based point-of-service solution for the retail and hospitality industries.

Every time a bell rings…

If you have upgraded from Visual Studio 2003 to Visual Studio 2005, you have probably noticed one of the major annoyances: they took away the sounds.

With VS 2003, you could set things up so that your computer made a happy little sound at you when a build succeeds. This was great because I could turn away from the monitor for a moment, if only to rest my eyes, until the sound brought me back. Or I could check email, get another glass of cold caffeine, etc.

I’d even gone a step further by making my app play the sound of a bell ringing, when it had finished booting itself after a successful build in the dev environment. This extended the time I could gaze out my window at the tangle of weeds we call a lawn.

But in VS 2005, all you can do is hear a sound when you hit a breakpoint. Big deal. I want the cheerful news that everything is copasetic. So why did they take out the sounds? To save a few hundred milliseconds. That appears to be the official word.

And the official word, back in April 2006, was that they were working on fixing this “bug” for the next release. Well, the next release has come and gone. Still no sounds. In the meantime, they suggest, you can write an add-in.

In Service Pack 1, there is a new control called SoundPlayer, which appears to do the same thing a simple two-line function can do. Yes, without it you have to declare an API function, which means unmanaged code and wild-eyed people grabbing you by the collar to tell you what a mistake this is, but I’ve been using API calls very carefully since .NET appeared and have yet to suffer (I am knocking on my desk top with both fists). I guess it comes down to this: I’d rather have my sounds back than a relatively useless control.

I saved much more time when I had that happy sound than the hundreds of milliseconds they were worried about. At least my app still rings a bell when it’s up and running.

But they could have told me.

Community Server steward Telligent rolls out Graffiti CMS engine

SearchWinDevelopment.com recently caught up with Rob Howard, who graced these pages in the Pre Silverlight Days. Howard rode the first blog wave at Microsoft, and has continued on the social networking trail as head of Telligent, provider of Community Server, a blogging application running on the ASP.NET platform.

Recently, Telligent has launched Graffiti, which takes a big conceptual step up the stream from blogging. Graffiti is a Content Management System [CMS], which is what people find they need when they bash about in the blogpond long enough.

Trouble with CMS is: There is a wide spectrum with not too many nodes along the way. You have the big vendors [Vignette, Interwoven] with rich products and pricey price tags on one spectra end .. and on the other end there is WordPress and Blogger with low cost and low features. Telligent sees opportunity.

Graffiti is a light weight CMS platform, Howard told us. It is a good fit in a more disparate environment, he indicated. He said many companies are beginning to field more than a single CMS type. They are supporting what he calls ‘niche focused publishing platforms.’

As with past Telligent efforts, Graffiti runs on ASP.NET. But this time, it also runs on Linux and Mac OS, and Mono. It supports various DBs, including MySQL.

Telligent has more in store. Besides upcoming new revs of Community Server, there is the Harvest Reporting suite due from the company. Said Howard, it rolls up 130 pre-built reports that let blogmeisters slice and dice their community data. Blogging may be a commodity item, yes. But the software the grows up around the blogging phenomenon  may be industries onto themselves. With its stabs at CMS and reporting, Telligent may point the way toward a trend.

Power Tools for VSTO now available

Microsoft has released a set of Power Tools for Visual Studio 2008 Tools for Office. It’s called v1.0.0.0 and it’s available at the Microsoft Download Center.

The download includes a set of reusable class libraries and a Ribbon IDs ToolWindow tool.

The following operating systems are supported:

  • Windows Server 2003
  • Windows Server 2008
  • Windows Vista
  • Windows XP

It is compatible with Microsoft Office 2007.