Sunday, June 20, 2010
Open Data Protocol
Check this out:
http://www.odata.org/developers/protocols/overview
Tuesday, June 8, 2010
Macros for Visual Studio
- Select Tools \ Macros \ Macros IDE.
- In the Project Explorer (within the Macros IDE), double click MyMacros \ Module1.
- Paste the following codes into the editor.
After that, close the Macros IDE. Now, you may customize the shortcut key to execute the Macro.
- Select Tools \ Customize from VS menu.
- Click Keyboard button (next to the Close button).
- In the "show command containing" field, type "PrintDateTimeSingleLine". This will shortlist the commands that is accessible through VS.
- In the "Press shortcut keys" field, press CTRL+0 and click Assign button.
- Click OK button to save the customized keyboard settings.
- In the coding editor, press CTRL+0 and VS will execute the Macro. So, you don't have to repeatly typing the current date/time for the comments anymore.
Sub PrintDateTime()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert(DateTime.Now.ToString("d.MMM.yyyy, ddd @ hh:mm"))
End If
End Sub
Sub PrintDateTimeSingleLine()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//" & DateTime.Now.ToString("d.MMM.yyyy") & "-lhw-")
End If
End Sub
Sub PrintDateTimeProcHeader()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert(DateTime.Now.ToString("d.MMM.yyyy") & "-lhw")
selection.NewLine()
selection.Insert("-")
End If
End Sub
Sub PrintSeparator()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//-----------------------------------------------------------------------")
End If
End Sub
Sub PrintHeavySeparator()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//=======================================================================")
End If
End Sub
Sub PrintShortSeparator()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//-------------------------------------")
End If
End Sub
Sub PrintNotice()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//<======")
End If
End Sub
Saturday, May 1, 2010
Fullscreen Internet Explorer
iexplore.exe -kBelow is the C++ code in calling Internet Explorer to show 2 tab pages in fullscreen mode:
Reference:
CComQIPtrsp = pActiveSite;
(pActiveSite is IOleClientSite *)
CComPtrm_pBrowser;
HRESULT hr = sp->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2,
(void**)&m_pBrowser);
VARIANT_BOOL v = VARIANT_FALSE;
VARIANT_BOOL v1 = VARIANT_TRUE;
m_pBrowser->put_TheaterMode(v1);
m_pBrowser->put_TheaterMode(v);
http://www.vistaheads.com/forums/microsoft-public-internetexplorer-general/289892-ie7-fullscreen-toggle-effect-programmatically-similar-like-f11.html
Saturday, April 17, 2010
AsyncOperationManager for WinForm
1. BackgroundWorker class - allows you to post the progress in integer type only.
2. AsyncOperationManager class - allows you to post any object into the method that is running in the UI context.
If you need to pass the information to be shown in the UI, AsyncOperationManager is much more flexible.
Below is the sample code on using the AsyncOperationManager class.
private void button3_Click(object sender, EventArgs e)
{
AsyncOperation ao
= AsyncOperationManager.CreateOperation(null);
Thread t = new Thread(new
ParameterizedThreadStart(DoRun2));
t.Start(ao);
}
public void DoRun2(object state)
{
AsyncOperation ao = (AsyncOperation)state;
int j;
for (int i = 0; i < 100; i++)
{
j = tl.id;
ao.Post(this.DoProgress2, i);
System.Threading.Thread.Sleep(90);
}
ao.OperationCompleted();
}
void DoProgress2(object state)
{
// you don't have to call label2.Invoke() method because
// ao.Post() will put the context back to the UI
// context automatically.
this.label2.Text = state.ToString();
}
Tuesday, March 2, 2010
Reflection performance
http://msdn.microsoft.com/en-us/magazine/cc163759.aspx
http://www.drdobbs.com/windows/184416570
Basically, to speed up the reflection, you should do this:
1. Cache the object instance that you have created and reuse it later.
2. Using Interface or delegate to invoke the method instead of MethodInfo.Invoke.
3. Create the assembly dynamically using Reflection.Emit.
Monday, January 18, 2010
global.asax - rewriting the URL
http://localhost/myWebsite/beers
Instead, there are a few ways to achieve it. The easiest way is to use the "urlMappings" in the web.config file. But, this is good for the website that does not update frequently.
  <system.web>
    <urlMappings>
      <add url="~/beers" mappedUrl="~/article_list.aspx?id=1111"/>
    </urlMappings>
  </system.web>
If you have a website that requires to publish the contents that stores in the database, then, you might wonder how to achieve the same result. So, the URL mapping feature can't meet the requirement.
Two ways to achieve this:
(1) you may create a class that inherits from IHttpModule OR
(2) add global.asax to the website and then override BeginRequest method.
Sample code
protected void Application_BeginRequest(object sender, EventArgs e)
{
try
{
// get the requested URL.
string s = this.Request.RawUrl.ToString();
// get the current website url including the virtual directory.
string current_website = (String)Application["current_website"];
if (String.IsNullOrEmpty(current_website))
{
current_website = VirtualPathUtility.ToAbsolute("~/");
Application["current_website"] = current_website;
}
// remove the virtual directory. So, only the requested file name will be in the string.
string s2 = s.Replace(current_website, "");
// if the string contains "." (ie, file extension dot)
// or "/" (ie, the folder separator), exit the checking.
if ((s2.Contains("/"))
|| (s2.Contains("."))
)
{
return;
}
else
{
// Now, we need to search for the string in the database that matches the requested url.
// If found the record, we will rewrite the url. The new URL will not reflect in the client browser.
if (s2.CompareTo("beers") == 0)
{
string url = "~/article.aspx?id=1111";
((HttpApplication)sender).Context.RewritePath(url);
}
}
}
catch (Exception ex)
{
// exception handler goes here.
}
}
Wednesday, November 4, 2009
How to find out the current path/virtual directory of a website?
- VirtualPathUtility.IsAbsolute() will returns false. Meaning, once the URL contains the "~" symbol, it becomes relative. So, VirtualPathUtility.IsAppRelative() will return true.
- For ASP.NET to translate this URL into a runtime URL, we have to call VirtualPathUtility.ToAbsolute(). The result will be "/images/ok.jpg" and "/news/images/ok.jpg" for the above mentioned situation respectively.
- To compute the physical path, you have to call AppDomain.CurrentDomain.BaseDirectory + relative path (you have to remove the "~" symbol and replace "/" with "\").