How to make a form stay always on top
Sometimes we require to make our application stay always on top of other applications. You simply achive this by setting the TopMost propetry of the Form to True. This post describes the other way using Win32 API functions.
Here is the declarations
private IntPtr HWND_TOPMOST = new IntPtr(-1);
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOMOVE = 0x0002;
private const uint TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd,
IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
And in the Form Load event, you need to call the SetWindowPos() function like this.
SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
You require System.Runtime.InteropServices namespace for using WIN32 API functions in C#.
Run the application at Windows startup
Sometimes its required to start your application while on Windows startup. This small code snippet allows you to register your application on windows startup. This code require Microsoft.Win32 namespace. To register application you need to set the key under
“HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run”
registry key.
private void RegisterInStartup(bool isChecked)
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (isChecked)
{
registryKey.SetValue("ApplicationName", Application.ExecutablePath);
}
else
{
registryKey.DeleteValue("ApplicationName");
}
}
Happy Programming
Unrecognized attribute ‘targetFramework’ error
Yesterday while publishing a WCF Service to IIS I got this error from the VS2010 auto generated web.config file.
<compilation debug="true" targetFramework="4.0">
After searching I found it is because of the Framework version setting in the Application Pool of the web application.
In IIS 7, you can modify the application pool by selecting the Application Pools node from left side bar, and Select the Basic Settings, which will bring the Edit Application Pool dialog, where you can choose the .Net Framework Version. To fix the issue, it should set to version 4.x
Microsoft Visual Studio LightSwitch
Microsoft Visual Studio LightSwitch gives you a simpler and faster way to create professional-quality business applications for the desktop, the web, and the cloud. LightSwitch is a new addition to the Visual Studio family. With LightSwitch, you can build custom applications that rival off-the-shelf solution.
LightSwitch is a stand alone Visual Studio product (it will also be available in future versions of Visual Studio Professional and above). VS Professional gives you a lot of options for building your application, including being able to build top tier enterprise wide apps.
Drag and Drop in Silverlight 3
One of the forums I found a question like how can I implement drag and drop in Silverlight. Today I got a chance to explore Silverlight drag and drop. I haven’t checked whether SL4 supports Drag and Drop, here is a simple implementation, which allow user to move the controls in runtime. I am using a rectangle as the control to drag and I placed the rectangle in a canvas.
XAML Code
<Canvas x:Name="LayoutRoot" Background="Gray">
<Rectangle Height="20" Width="40" Fill="Blue"
MouseLeftButtonDown="Rectangle_MouseLeftButtonDown"
MouseLeftButtonUp="Rectangle_MouseLeftButtonUp"
MouseMove="Rectangle_MouseMove" Canvas.Left="109" Canvas.Top="88">
</Rectangle>
</Canvas>
And here is the C# Code.
//To store the initial location of the control.
private Point _StartPoint = default(Point);
//To check whether user started dragging the control.
private bool _IsMouseCaptured = false;
private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
UIElement uiElement = sender as UIElement;
//Reading the current position
_StartPoint = e.GetPosition(uiElement);
//Capturing the Mouse.
this._IsMouseCaptured = uiElement.CaptureMouse();
}
private void Rectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//Releasing the Mouse and setting the variable to false.
UIElement uiElement = sender as UIElement;
uiElement.ReleaseMouseCapture();
this._IsMouseCaptured = false;
}
private void Rectangle_MouseMove(object sender, MouseEventArgs e)
{
//If the Mouse captured, set the Left and Top values of the control
if (this._IsMouseCaptured)
{
UIElement uiElement = sender as UIElement;
Canvas.SetLeft(uiElement, (e.GetPosition(this).X - this._StartPoint.X));
Canvas.SetTop(uiElement, (e.GetPosition(this).Y - this._StartPoint.Y));
}
}

