Here's a sample application that I wrote that combines three technologies:
- Windows Presentation Framework
- Regular Expressions to parse HTML
- Reading HTML with HttpWebRequest and HttpWebResponse
To run the sample, just press the Go button. The text box fills with anchor tags from the web site given in the url box.
To build the sample, start with a new .NET Framework 3.0 Windows Application (WPF) project. You'll have to rename the main window to GlideWindow and do a few other obvious tweaks to get the code to compile.
Put this in the GlideWindow XAML file:
<Window x:Class="Glide.GlideWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Glide" Height="300" Width="700"
>
<Grid x:Name="_Grid">
<Grid.RowDefinitions>
<RowDefinition Height="36" />
<RowDefinition />
</Grid.RowDefinitions>
<Canvas Grid.Row="0" Name="_ControlCanvas">
<TextBlock Canvas.Left="15" Canvas.Top="8" Width="25" Height="20"
HorizontalAlignment="Left" Text="url:" />
<TextBox Canvas.Left="40" Canvas.Top="8" Width="600" Height="20"
x:Name="_UrlTextBox" >http://msn.com</TextBox>
<Button Canvas.Left="650" Canvas.Top="8" Width="28" Height="20"
HorizontalAlignment="Right" x:Name="_GoButton" Content="Go!">
</Button>
</Canvas>
<TextBox Grid.Row="1" Margin="10,4,0,0" Name="textBox1"
VerticalAlignment="Top" HorizontalAlignment="Left" VerticalScrollBarVisibility="Auto">
</TextBox>
</Grid>
</Window>
And here's the GlideWindow code-behind file.
// First step in creating a tree-driven personal search engine.
// by Martin J. Tracy
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Glide
{
// Main window.
public partial class GlideWindow : System.Windows.Window
{
#region Constructor
public GlideWindow()
{
InitializeComponent();
this._GoButton.Click += new RoutedEventHandler(_GoButton_Click);
}
#endregion
#region Go button event handler
// Read and parse the HTML and display anchor tags in the text box.
void _GoButton_Click(object sender, RoutedEventArgs e)
{
string url = _UrlTextBox.Text;
string line = GetHtmlAsLine(url);
List<string> anchors = GetAnchorsFromLine(line);
// Format anchor tags and display them in a text box.
StringBuilder sbuild = new StringBuilder();
foreach (string anchor in anchors)
{
sbuild.Append(anchor + "\r\n");
}
textBox1.Text = sbuild.ToString();
}
#endregion
#region Read HTML from web page as one long line
// Read web page at url and return contents as one long line.
string GetHtmlAsLine(string url)
{
string line = "";
HttpWebRequest httpWReq = null;
HttpWebResponse httpWResp = null;
StreamReader webstream = null;
try
{
httpWReq = (HttpWebRequest)WebRequest.Create(url);
httpWResp = (HttpWebResponse)httpWReq.GetResponse();
webstream = new StreamReader(httpWResp.GetResponseStream(), Encoding.ASCII);
line = webstream.ReadToEnd();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
finally
{
if (webstream != null) webstream.Close();
if (httpWResp != null) httpWResp.Close();
}
return line;
}
#endregion
#region Parse anchors from HTML using regular expressions
// Return a list of anchor tags.
List<string> GetAnchorsFromLine(string line)
{
List<string> anchors = new List<string>();
string anchorPattern =
@"(?<anchor><a(\s+\w+\s*[=]\s*([""].*?[""]|['].*?[']))*\s*>)(?<inner>.*?)</a>";
MatchCollection matches =
Regex.Matches(line, anchorPattern,
RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
foreach (Match m in matches)
{
string anchor = m.Groups["anchor"].ToString();
anchors.Add(anchor);
}
return anchors;
}
#endregion
}
}
Windows Presentation Foundation (WPF) is Microsoft’s preferred technology for writing windows applications for Vista. You can write WPF applications with Visual Studio to run either under Windows XP SP2 or Windows Vista. You can read about WPF in Charles Petzold's new book
Applications = Code + Markup (2006) (ISBN-10: 0735619573) For some reason, this book isn't listed in either the Barnes & Noble or the Borders Books database, but it's probably on their shelves.
Before you write your first WPF program using Visual Studio, you’ll want to download and install these three software packages:
- .NET Framework 3.0 (codename WinFX).
- Windows SDK for Vista.
- VS 2005 extensions for .NET Framework 3.0 (codename Fidalgo)
.NET Framework 3.0
.NET Framework 3.0 is the runtime for both WPF and Windows Connection Framework (WCF, codename Indigo). .NET Framework 3.0 ships with Windows Vista.
With .NET Framework 3.0 alone, you can write WPF programs using Visual Studio (or even NotePad). In Visual Studio, you won’t see project templates and Intellisense. But you can create an empty project and add references to PresentationCore.dll and other WPF assemblies. By the way, you won’t find them under the .NET tab – You’ll have to browse to the ReferencedAssemblies folder.
Once you’ve written and launched a WPF program, the PresentationHost will display the WPF application for you.
Windows SDK for Vista
Windows SDK for Vista adds documentation (large) and Intellisense for WPF . The documentation isn’t integrated into Visual Studio, and there are no project templates. However, there's an excellent set of samples for you to install.
The download page has two options hidden in the Instructions section. Use the Web Install option if you have a high-bandwidth. If you intend to write WPF applications using Visual C# and don’t care about programming against the older Win32 API then set the installation options this way:
When installation completes, you'll find the zipped sample files at
C:\Program Files\Microsoft SDKs\Windows\v6.0\Samples
VS 2005 extensions for .NET Framework 3.0
VS 2005 extensions for .NET Framework 3.0 merges the documentation you obtained from the Windows SDK into Visual Studio and adds project templates. WPF assemblies show up in the .NET tab of the Add Reference dialog. This download also adds a XAML designer (codename Cider).
Installation Scenarios
Here’s what you need to write WPF applications in Visual Studio in these scenarios:
- VS 2005 under Windows XP SP2
- VS codename Orcas under Windows XP SP2
- VS 2005 under Vista
- VS codename Orcas under Vista
VS 2005 under Windows XP SP2
You must install these in order.
1. .Net Framework 3.0 (Codename WinFX)
http://www.microsoft.com/downloads/details.aspx?FamilyId=10CC340B-F857-4A14-83F5-25634C3BF043&displaylang=en
2. Windows SDK for Vista
http://www.microsoft.com/downloads/details.aspx?FamilyId=7614FE22-8A64-4DFB-AA0C-DB53035F40A0&displaylang=en
3. VS 2005 extensions for .NET Framework 3.0 (WCF & WPF), November 2006 CTP
(codename Fidalgo)
http://www.microsoft.com/downloads/details.aspx?FamilyId=F54F5537-CC86-4BF5-AE44-F5A1E805680D&displaylang=en
VS codename Orcas under Windows XP SP2
You must install Windows SDK for Vista
http://www.microsoft.com/downloads/details.aspx?FamilyId=7614FE22-8A64-4DFB-AA0C-DB53035F40A0&displaylang=en
VS 2005 under Windows Vista
You must install these in order.
1. VS 2005 Team Suite SP1
http://www.microsoft.com/downloads/details.aspx?familyid=BB4A75AB-E2D4-4C96-B39D-37BAF6B5B1DC&displaylang=en
2. VS 2005 SP1 Update for Windows Vista Beta
http://www.microsoft.com/downloads/details.aspx?FamilyID=fb6bb56a-10b7-4c05-b81c-5863284503cf&DisplayLang=en
3. Windows SDK for Vista
http://www.microsoft.com/downloads/details.aspx?FamilyId=7614FE22-8A64-4DFB-AA0C-DB53035F40A0&displaylang=en
4. VS 2005 extensions for .NET Framework 3.0 (WCF & WPF), November 2006 CTP
(codename Fidalgo)
http://www.microsoft.com/downloads/details.aspx?FamilyId=F54F5537-CC86-4BF5-AE44-F5A1E805680D&displaylang=en
VS codename Orcas under Windows Vista
You must install these in order.
1. VS 2005 Team Suite SP1
http://www.microsoft.com/downloads/details.aspx?familyid=BB4A75AB-E2D4-4C96-B39D-37BAF6B5B1DC&displaylang=en
2. VS 2005 SP1 Update for Windows Vista Beta
http://www.microsoft.com/downloads/details.aspx?FamilyID=fb6bb56a-10b7-4c05-b81c-5863284503cf&DisplayLang=en
3. Windows SDK for Vista
http://www.microsoft.com/downloads/details.aspx?FamilyId=7614FE22-8A64-4DFB-AA0C-DB53035F40A0&displaylang=en
This posting is provided "AS IS" with no warranties, and confers no rights.
Visual Studio 2005 SDK documentation is now available on the MSDN Library Web site!
To access the documentation, browse to http://msdn2.microsoft.com/en-us/library/default.aspx. You'll find the documentation in the Table of Contents under Development Tools and Languages/Visual Studio/Visual Studio SDK.
------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights
Should I implement IDisposable on my managed Package.ProjectPackage project node to release expensive resources?
No. Instead, put your resource cleanup code in the IVsHierarchy.Close method.
Implementing IDisposable on your project node may cause Visual Studio to call your ServiceContainer.Dispose method. This in turn calls the Dispose method of all service instances that implement IDisposable. Calling Dispose on a service instance that hasn't been removed first with ServiceContainer.RemoveService can cause exceptions.
Take a look at this and other notes like it in the new Frequently Asked Questions (FAQs) topics in our docs.
------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights
You won't find the IronPython templates in the File/New Web Site dialog even after you've built or rebuilt the IronPython sample solution. You must also install the templates by typing one of these commands from the VS command prompt:
>devenv /setup
>devenv /installvstemplates
The latter is much quicker.
The problem is that, for most samples, the build system only calls devenv /setup automatically if a .ctc file has been altered. The order of project compilation is important, too. If the templates project is built after the project containing the altered .ctc file, then devenv /setup will be called before the templates are available.
When in doubt, install the templates.
------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights
To demo the IronPython (IP) Web site support you must first install two IP binaries into the GAC. You can find them at
<Install path>/VisualStudioIntegration/Common/Assemblies/IronMath.dll
<Install path>/VisualStudioIntegration/Common/Assemblies/IronPython.dll
Open the Visual Studio command prompt window and navigate to this directory. Then type in the following --
>gacutil -i ironmath.dll
>gacutil -i ironpython.dll
------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
What if you want to find the Command GUID:ID pair associated with a context menu item? Paul Harrington uses this technique --
That one's a little tricky. The ID you want is {D309F791-903F-11D0-9EFC-00A0C911004F}:00000431
It's a little tricky to get to this, but here's how.
1. Load your WebLibrary project and select the "foo" web reference (You have to do this because the items on that menu are context sensitive)
2. Tools/Customize...
3. On the Toolbars tab, check "Context Menus". You should see a big toolbar appear with several "xxx Context Menus". Not many people know about this. It's a special toolbar that's only visible in customize mode. It collects all context menus into one tree. Some have been categorized, but the vast majority fall under "Other Context Menus". Fortunately, the one you want has been categorized under "Project and Solution".
4. On that new toolbar click on the "Project and Solution Context Menus"
5. Click on the "Folder" submenu.
6. Make an obvious customization to one of the commands (e.g. right click on "Update Web Reference" and rename it to "FindMe"). Note that you can't customize "Folder" itself, but you can customize the items inside (by design).
7. Close the customize dialog
7b) Verify that your customization worked (right click on 'foo' in the solution explorer)
8. Export your customizations (Tools/Import and Export Settings...)
------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
Attachment(s): UpdateWebReference.JPG
Currently we have no way of displaying the GUID:ID pairs of Visual Studio command bars. Here's a technique presented by Paul Harrington --
With a little patience, you can get this [information] today via Export Settings. Start by resetting your settings to the "General Settings" profile. Then make a small, but identifiable customization to the command bar you're interested in. For example:
-
* If it's a toolbar, show it if it's hidden, hide it if it's shown.
-
* If it's a menu, go into customize mode (Tools/Customize...) and rename it to something identifiable
Export your settings (you only need to include "Menu and Command Bar Customizations" under the General Settings category) and open up the exported file (it's an XML format). You should be able to find your customization under the "UserCustomizations" node.
For example, here's my file after showing the "Dialog Editor" toolbar:
<UserCustomizations>
<modify_toolbar Menu="{E148F049-C570-4F55-84A6-6DA870AF229E}:00001388" Visibility="show" FullScreen="hide" Dock="top" Row="2" DockRectangle="0,49,375,75"/>
</UserCustomizations>
The GUID:ID of the Dialog Editor toolbar is right there in the modify_toolbar record: {E148F049-C570-4F55-84A6-6DA870AF229E}:00001388
Here's another where I renamed the "Community" menu:
<modify Cmd="{D309F791-903F-11D0-9EFC-00A0C911004F}:00000090" CmdPri="fe000000" Group="{D309F791-903F-11D0-9EFC-00A0C911004F}:00000105" GroupPri="fd000000" Menu="{D309F791-903F-11D0-9EFC-00A0C911004F}:00000000" Name="UniqueStringForExport"/>
Here you see the GUID:ID for the Community Menu, the GUID:ID for the group it lives in and the GUID:ID for its parent Menu (in which that group resides), which in this case is the main MenuBar.
------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
[Compiled by Chetan Parmer.]
Defined in Microsoft.VisualStudio.VsConstants:
guidCOMPlusOnlyEng {449EC4CC-30D2-4032-9256-EE18EB41B62B} //ComPlusOnly
These GUIDs are not yet defined in VsConstants:
guidNativeOnlyEng {3B476D35-A401-11D2-AAD4-00C04F990171} //NativeOnly
guidScriptEng {F200A7E7-DEA5-11D0-B854-00A0244A1DE2} //Script
guidCOMPlusNativeEng
{92EF0900-2251-11D2-B72E-0000F87572EF} //CompPLusNative
guidCOMPlusSQLLocalEng {E04BDE58-45EC-48DB-9807-513F78865212} //SqlClr
CLSID_SqlDebugEngine3 {3B476D3A-A401-11D2-AAD4-00C04F990171} //Yukon
CLSID_SqlDebugEngine2 {3B476D30-A401-11D2-AAD4-00C04F990171} //LegacyTSQL
------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
[From the prolific pen of Douglas Hodges. Continued from Common Context Menu Constants. ]
Visual Web Developer Project Specific Information:
The context menu for the Web Project node has the following groups:
- guidVenusCmdId:IDG_CTX_BUILD
guidSHLMainMenu:IDG_VS_CTXT_PROJECT_ADD_ITEMS
guidVenusCmdId:IDG_CTX_REFERENCE
guidVenusCmdId:IDG_CTX_PUBLISH
guidSHLMainMenu:IDG_VS_CTXT_ITEM_VIEWBROWSER
guidSHLMainMenu:IDG_VS_CTXT_PROJECT_SCC
guidSHLMainMenu:IDG_VS_CTXT_FOLDER_TRANSFER
guidSHLMainMenu:IDG_VS_VIEW_PROPPAGES
where
// {C7547851-4E3A-4e5b-9173-FA6E9C8BD82C}
DEFINE_GUID(guidVenusCmdId,
0xc7547851, 0x4e3a, 0x4e5b, 0x91, 0x73, 0xfa, 0x6e, 0x9c, 0x8b, 0xd8, 0x2c);
#define IDG_CTX_REFERENCE 0x0102
#define IDG_CTX_PUBLISH 0x0103
#define IDG_CTX_BUILD 0x0104
Here are some of the commands the web project adds to these special groups:
guidVSStd97:cmdidBuildSel, guidVenusCmdId:IDG_CTX_BUILD, 0x0100;
guidVSStd2K:ECMD_PUBLISHSELECTION, guidVenusCmdId:IDG_CTX_BUILD, 0x0300;
guidVSStd2K:ECMD_ADDREFERENCECTX, guidVenusCmdId:IDG_CTX_REFERENCE, 0x0100;
guidVSStd2K:ECMD_ADDWEBREFERENCECTX, guidVenusCmdId:IDG_CTX_REFERENCE, 0x0000;
guidVSStd2K:ECMD_UPDATEWEBREFERENCE, guidVenusCmdId:IDG_CTX_REFERENCE, 0x0000;
guidVSStd2K:cmdidViewInClassDiagram, guidVenusCmdId:IDG_CTX_REFERENCE, 0x0500;
guidVSStd2K:ECMD_PUBLISHCTX, guidVenusCmdId:IDG_CTX_PUBLISH, 0x0100;
guidVSStd2K:ECMD_STARTOPTIONSCTX, guidVenusCmdId:IDG_CTX_PUBLISH, 0x0200;
guidVSStd97:cmdidSetStartupProject, guidVenusCmdId:IDG_CTX_PUBLISH, 0x0300;
Jamie Laflen adds this one --
The project kind for a venus project is VsWebSite.PrjKind.prjKindVenusProject
You'll find it in VsWebsite.Interop.dll
------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
[From the prolific pen of Douglas Hodges. Continued at Common Web Context Menu Constants]
Common menu constants used by VSIP’s in extending project systems:
"Project" top-level menu: IDM_VS_MENU_PROJECT
Solution Explorer toolbar: IDM_VS_TOOL_PROJWIN
Common project context menus that apply when a single node is selected in the Solution Explorer, or when there is multiple homogenous selection in the Solution Explorer (i.e., all selected nodes are of the same type):
IDM_VS_CTXT_PROJNODE – applies when the project node is selected
IDM_VS_CTXT_ITEMNODE – applies when a file is selected
IDM_VS_CTXT_FOLDERNODE – applies when a folder is selected
IDM_VS_CTXT_WEBREFFOLDER – applies when the Web Reference folder is selected
IDM_VS_CTXT_REFERENCEROOT – applies when the references root node ("References") is selected
IDM_VS_CTXT_REFERENCE – applies when a reference node is selected (assembly, COM, and project references only – not web references)
These context menus apply when the selection in the Solution Explorer spans multiple hierarchies:
IDM_VS_CTXT_XPROJ_SLNPROJ – applies when the current selection contains the solution node and root project node(s).
IDM_VS_CTXT_XPROJ_SLNITEM – applies when the current selection contains the solution node and project item(s).
IDM_VS_CTXT_XPROJ_MULTIPROJ – applies when the current selection consists of multiple root project nodes only.
IDM_VS_CTXT_XPROJ_PROJITEM – applies when the current selection contains a mix of root project node(s) and project item(s). In addition, the selection may or may not contain the solution node.
IDM_VS_CTXT_XPROJ_MULTIITEM – applies when the current selection contains project items from multiple projects within the solution, or when items of different types are selected within the same project.
Common groups within IDM_VS_CTXT_PROJNODE context menu:
IDG_VS_CTXT_PROJECT_BUILD – build, rebuild, deploy the project
IDG_VS_CTXT_COMPILELINK – compilation/linking
IDG_VS_CTXT_PROJECT_CONFIG – configurations and build order
IDG_VS_CTXT_PROJECT_ADD – adding items to the project
IDG_VS_CTXT_PROJECT_START – setting the startup project for F5 action
IDG_VS_CTXT_PROJECT_SAVE – saving items
IDG_VS_CTXT_PROJECT_DEBUG – debugging commands
IDG_VS_CTXT_PROJECT_SCC – source control operations
IDG_VS_CTXT_PROJECT_TRANSFER – cut/copy/paste operations
IDG_VS_CTXT_PROJECT_PROPERTIES – access to project properties dialog------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
The VS SDK April CTP is finally out the door! It contains many new samples to help you get up to speed. Unfortunately, the OnlineHelpSearchProvider sample has a few known issues that weren't caught before the readme file was frozen.
First of all, the sample was written to use Microsoft.VisualStudio.Enterprise.ASPNetHelper.dll. This .dll file doesn't ship with Visual Studio Pro, so if that's what you're using, you're dead in the water.
But let's say you have Visual Studio Enterprise, but you installed it to the D: drive instead of the default C: drive. There's a <codebase> tag in the web.config file that's hardwired to the C: drive. You have to repoint it, like this.
<
codeBase version="8.0.0.0" href="file:///D:/Program%20Files/Microsoft%20Visual%20Studio%208/Common7/IDE/PrivateAssemblies/Microsoft.VisualStudio.Enterprise.ASPNetHelper.DLL"/>
Once that's done, you're good to go.
I've written a help topic on how to install and run the sample. You can find it in the VS SDK help collection at VisualStudioIntegration/Samples/Custom Search Provider.
------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
Hi! My name is Martin Tracy, and I'm a programmer writer for Microsoft Visual Studio. What is a programmer writer, you ask? It means I write both documentation and sample code for the Visual Studio Software Development Kit (SDK).
During my last five years at Microsoft, I've written for the WMI team, the Tablet PC team, and the Visual Studio Debugger SDK, as well as providing the initial walkthroughs for Visual J#.
My goals for this blog are to:
- Point you to interesting Help topics covering the Visual Studio SDK.
- Get your input as I prepare to document the new features of the next release of the Visual Studio SDK.
- Share any tricks and tips I learn while exploring Visual Studio, developing web sites, and writing Windows applications.
I look forward to our dialog and receiving your comments!
-- Martin
------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.