[英]Changing Windows Form
我正在研究一種更改Windows窗體框周圍的框架的方法。 我想使其透明或完全擺脫它。 我設法擺脫了圖標,擺脫了最大化按鈕,並限制了用戶調整窗口大小的能力。 該欄與我要實現的視覺主題沖突。
所以問題-在Windows窗體應用程序中自定義窗體邊框和頂部欄有什么好方法嗎?
為FormBorderStyle
選擇正確的值,例如None
。
這可能對您要問的內容有些矯kill過正,但是可以解決...這是我創建的帶有圓形邊框的快速應用程序。 這不是全部代碼,但是也許您會從中得到靈感。
首先,我刪除FormBorderStyle(將其設置為None)並添加鼠標處理程序以允許移動表單。 下面的代碼...
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
然后,我創建了一個粉紅色的網格以顯示為表單背景。 並將TransparencyKey屬性設置為我想要透明的背景顏色的RGB值(在本例中為255,15,255)。
然后,我使用圖形模塊繪制文字控件。
private void Form1_Paint(object sender, PaintEventArgs e)
{
// Set the starting coordinants for our graphics drawing
int y = 0;
int x = 0;
// Set the end coordinants for our graphics drawing
int width = this.Size.Width;
int height = this.Size.Height;
// Set our graphics options
e.Graphics.InterpolationMode = XGraphics.xInterpolation;
e.Graphics.SmoothingMode = XGraphics.xSmoothingMode;
e.Graphics.CompositingMode = XGraphics.xComposingMode;
e.Graphics.CompositingQuality = XGraphics.xComposingQuality;
e.Graphics.PixelOffsetMode = XGraphics.xPixelOffsetMode;
// Set the colors, positions, and gradient directions then draw our background
using (LinearGradientBrush gpxBrush = XGraphics.GradientBrushOrientation(0, x, y, width, height))
{
// Create our color blender object
ColorBlend gpxBlend = null;
gpxBlend = new ColorBlend(4);
gpxBlend.Colors = new Color[] { Color.FromArgb(255, 0, 100, 255), Color.FromArgb(205, 255, 240, 240),
Color.FromArgb(255, 20, 100, 230), Color.FromArgb(105, 105, 100, 255) };
gpxBlend.Positions = new float[] { 0.0F, .45F, .55F, 1.0F };
gpxBrush.InterpolationColors = gpxBlend;
// Draw our background
e.Graphics.FillEllipse(gpxBrush, x, y, width, height);
}
// Set the end coordinants for our graphics drawing
width = this.Size.Width-5;
height = this.Size.Height-5;
using (LinearGradientBrush gpxBrush = XGraphics.GradientBrushOrientation(0, x, y, width, height))
{
// Create our color blender object
ColorBlend gpxBlend = null;
gpxBlend = new ColorBlend(2);
gpxBlend.Colors = new Color[] { Color.White, Color.Gray };
gpxBlend.Positions = new float[] { 0.0F, 1.0F };
gpxBrush.InterpolationColors = gpxBlend;
// Draw our background
e.Graphics.FillEllipse(gpxBrush, x+5, y+5, width-5, height-5);
}
width = this.Size.Width / 2;
height = this.Size.Height / 2;
using (LinearGradientBrush gpxBrush = XGraphics.GradientBrushOrientation(0, x, y, width, height))
{
// Create our color blender object
ColorBlend gpxBlend = null;
gpxBlend = new ColorBlend(2);
gpxBlend.Colors = new Color[] { Color.White, Color.Gray };
gpxBlend.Positions = new float[] { 0.0F, 1.0F };
gpxBrush.InterpolationColors = gpxBlend;
// Draw our background
e.Graphics.FillEllipse(gpxBrush, x + width / 2, y + height / 2, width, height);
}
width = (this.Size.Width / 2) - 5;
height = (this.Size.Height / 2) - 5;
using (LinearGradientBrush gpxBrush = XGraphics.GradientBrushOrientation(0, x, y, width-5, height-5))
{
// Create our color blender object
ColorBlend gpxBlend = null;
gpxBlend = new ColorBlend(4);
gpxBlend.Colors = new Color[] { Color.FromArgb(255, 0, 100, 255), Color.FromArgb(205, 255, 240, 240),
Color.FromArgb(255, 20, 100, 230), Color.FromArgb(105, 105, 100, 255) };
gpxBlend.Positions = new float[] { 0.0F, .45F, .55F, 1.0F };
gpxBrush.InterpolationColors = gpxBlend;
// Draw our background
e.Graphics.FillEllipse(gpxBrush, (x + width / 2) +7, (y + height / 2) + 7, width - 5, height - 5);
}
}
如您所見,現在沒有邊框,並且邊緣是透明的。 只需確保添加一個按鈕以退出應用程序,因為不再有“ X”選項! ;)
希望這對您有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.