you ever wanted to create a desktop interface with a bit 'unusual? A round window? Well, follow me in this short tutorial: start to create a new application (the screenshots are for Visual Studio 2008):
File / New / Project
select Windows Application Form .
Now we need to use an image as the background of our window in our example we choose a PNG with transparent background:
return to Visual Studio: the main form, set the BackgroundImage loading the chosen image, dimensions of the image size on the form. We leave the property BackgroundImageLayout to None (do not give in to the temptation to set it to Stretch: inhibit the transparency of the application at runtime).
Now set the FormBorderStyle to None, so we remove the title bar and the edges of the form.
Now set the property TrasparencyKey the background color of the form (or, in our case, Control):
L'ultimo passo da compiere รจ la gestione del trascinamento e della chiusura della nostra form: aggiungiamo alle using l'assembly InteropServices:
using System.Runtime.InteropServices;
...e incolliamo all'interno della classe il seguente codice:
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
Infine gestiamo l'evento MouseDown, sia per il trascinamento della form col tasto sinistro del mouse, sia per chiudere l'applicazione con il tasto destro (ricordiamo che manca la barra del titolo, con i pulsanti di chiusura, riduzione e ingrandimento):
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
else
{
Close();
}
}
Abbiamo finito! Compiliamo ed eseguiamo l'applicazione: ecco la nostra finestra dall'aspetto insolito...
0 comments:
Post a Comment