如何做窗体透明效果

jiangqiaohua 2006-11-13 05:00:49
象鱼鱼桌面秀软件一下

XP背景淡蓝色的透明效果

很漂亮跟系统结合很完美

c# 可以实现吗
...全文
503 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiangqiaohua 2006-11-15
  • 打赏
  • 举报
回复
不规则窗体的我有一段代码
private Region BmpRgn(Bitmap Picture, Color TransparentColor)
{
int nWidth = Picture.Width;
int nHeight = Picture.Height;
Region rgn = new Region();
rgn.MakeEmpty();
bool isTransRgn;//前一个点是否在透明区
Color curColor;//当前点的颜色
Rectangle curRect = new Rectangle();
curRect.Height = 1;
int x = 0, y = 0;
//逐像素扫描这个图片,找出非透明色部分区域并合并起来。
for (y = 0; y < nHeight; ++y)
{
isTransRgn = true;
for (x = 0; x < nWidth; ++x)
{
curColor = Picture.GetPixel(x, y);
if (curColor == TransparentColor || x == nWidth - 1)//如果遇到透明色或行尾
{
if (isTransRgn == false)//退出有效区
{
curRect.Width = x - curRect.X;
rgn.Union(curRect);
}
}
else//非透明色
{
if (isTransRgn == true)//进入有效区
{
curRect.X = x;
curRect.Y = y;
}
}//if curColor
isTransRgn = curColor == TransparentColor;
}//for x
}//for y
return rgn;
}

我放了张图片没有用

但是
this.Width = 500;
this.Height = 220;
this.BackColor = Color.RoyalBlue;
GraphicsPath m_path =new System.Drawing.Drawing2D.GraphicsPath(FillMode.Winding);
m_path.AddEllipse(1, 1, 400, 160);
Region m_region = new Region(m_path)

这样却是可以的
是一个椭圆
wshuangminlg 2006-11-14
  • 打赏
  • 举报
回复
[DllImport("user32.dll")] 什么意思
wdy9927 2006-11-14
  • 打赏
  • 举报
回复
mark
lookatliu 2006-11-14
  • 打赏
  • 举报
回复
private void button1_Click(object sender, System.EventArgs e)
{
if (textBox1.Text == String.Empty)
{
return;
}
if (int.Parse(textBox1.Text) < 0 || int.Parse(textBox1.Text) > 255)
{
return;
}
string strProcessName = cboProcessList.Text;
if (strProcessName != "")
{
Process procs = (Process)_arrProcessList[cboProcessList.SelectedIndex];
_arrStatusList[cboProcessList.SelectedIndex] = textBox1.Text;
int lng = int.Parse(textBox1.Text);
bool isOk = Win32API.LockWindowUpdate(procs.MainWindowHandle);
long lngStyle = Win32API.GetWindowLong(procs.MainWindowHandle, Win32API.GWL_EXSTYLE);
Win32API.SetWindowLong(procs.MainWindowHandle, Win32API.GWL_EXSTYLE, lngStyle|Win32API.WS_EX_LAYERED);
Win32API.SetLayeredWindowAttributes(procs.MainWindowHandle, 0, Convert.ToByte(lng), Win32API.LWA_ALPHA);
Win32API.DrawMenuBar(procs.MainWindowHandle);
isOk = Win32API.LockWindowUpdate(IntPtr.Zero);
lbStatus.Text = textBox1.Text;
textBox1.Text = String.Empty;
}
}
private void btnRefresh_Click(object sender, System.EventArgs e)
{
Initialize();
}
private void cboProcessList_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (cboProcessList.SelectedIndex == -1)
{
txbProcessTitle.Text = String.Empty;
lbStatus.Text = String.Empty;
}
else
{
txbProcessTitle.Text = (string)_arrFormTitleList[cboProcessList.SelectedIndex];
string strStatus = (string)_arrStatusList[cboProcessList.SelectedIndex];
if (strStatus == String.Empty)
{
strStatus = "未設定";
}
lbStatus.Text = strStatus;
}
}
}
}

以前写的东西2003,windows2000下可以用,有很多bug一直懒得改,项目太忙了,仅供参考
lookatliu 2006-11-14
  • 打赏
  • 举报
回复
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

namespace SetLayeredWindows
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button btnRefresh;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label lbCaption;
private System.Windows.Forms.ComboBox cboProcessList;
private System.Windows.Forms.TextBox txbProcessTitle;
private System.Windows.Forms.Label lbStatusCaption;
private System.Windows.Forms.Label lbStatus;
private ArrayList _arrProcessList;
private ArrayList _arrFormTitleList;
private ArrayList _arrStatusList;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.cboProcessList = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.btnRefresh = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.lbCaption = new System.Windows.Forms.Label();
this.txbProcessTitle = new System.Windows.Forms.TextBox();
this.lbStatusCaption = new System.Windows.Forms.Label();
this.lbStatus = new System.Windows.Forms.Label();
this.SuspendLayout();
this.cboProcessList.Location = new System.Drawing.Point(58, 24);
this.cboProcessList.Name = "cboProcessList";
this.cboProcessList.Size = new System.Drawing.Size(166, 20);
this.cboProcessList.TabIndex = 0;
this.cboProcessList.SelectedIndexChanged += new System.EventHandler(this.cboProcessList_SelectedIndexChanged);
this.button1.Location = new System.Drawing.Point(58, 108);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "透明";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.textBox1.Location = new System.Drawing.Point(58, 74);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(58, 19);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "";
this.btnRefresh.Location = new System.Drawing.Point(232, 22);
this.btnRefresh.Name = "btnRefresh";
this.btnRefresh.TabIndex = 3;
this.btnRefresh.Text = "刷新";
this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
this.label1.Location = new System.Drawing.Point(6, 74);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(52, 19);
this.label1.TabIndex = 4;
this.label1.Text = "透明度:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.label2.Location = new System.Drawing.Point(6, 26);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(52, 16);
this.label2.TabIndex = 5;
this.label2.Text = "工程:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.lbCaption.Location = new System.Drawing.Point(116, 74);
this.lbCaption.Name = "lbCaption";
this.lbCaption.Size = new System.Drawing.Size(46, 19);
this.lbCaption.TabIndex = 6;
this.lbCaption.Text = "(0-255)";
this.lbCaption.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.txbProcessTitle.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txbProcessTitle.Location = new System.Drawing.Point(58, 48);
this.txbProcessTitle.Name = "txbProcessTitle";
this.txbProcessTitle.ReadOnly = true;
this.txbProcessTitle.Size = new System.Drawing.Size(266, 19);
this.txbProcessTitle.TabIndex = 8;
this.txbProcessTitle.Text = "";
this.lbStatusCaption.Location = new System.Drawing.Point(162, 74);
this.lbStatusCaption.Name = "lbStatusCaption";
this.lbStatusCaption.Size = new System.Drawing.Size(62, 19);
this.lbStatusCaption.TabIndex = 4;
this.lbStatusCaption.Text = "今の状態:";
this.lbStatusCaption.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.lbStatus.Location = new System.Drawing.Point(224, 74);
this.lbStatus.Name = "lbStatus";
this.lbStatus.Size = new System.Drawing.Size(62, 19);
this.lbStatus.TabIndex = 4;
this.lbStatus.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(382, 137);
this.Controls.Add(this.txbProcessTitle);
this.Controls.Add(this.lbCaption);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnRefresh);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.cboProcessList);
this.Controls.Add(this.lbStatusCaption);
this.Controls.Add(this.lbStatus);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Form1";
this.Text = "透明なフォーム";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
_arrProcessList = new ArrayList();
_arrFormTitleList = new ArrayList();
_arrStatusList = new ArrayList();

Initialize();
}
private void Initialize()
{
cboProcessList.Items.Clear();
_arrProcessList.Clear();
_arrFormTitleList.Clear();
_arrStatusList.Clear();
cboProcessList.SelectedIndex = -1;
cboProcessList.Text = String.Empty;
txbProcessTitle.Text = String.Empty;
textBox1.Text = String.Empty;
lbStatus.Text = String.Empty;
System.Diagnostics.Process[] pros = Process.GetProcesses();
for (int i=0; i<pros.Length; i++)
{
if (pros[i].MainWindowHandle != IntPtr.Zero)
{
cboProcessList.Items.Insert(cboProcessList.Items.Count,pros[i].ProcessName);

_arrProcessList.Add(pros[i]);

_arrFormTitleList.Add(pros[i].MainWindowTitle);

_arrStatusList.Add(String.Empty);
}
}
}
lookatliu 2006-11-14
  • 打赏
  • 举报
回复
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Drawing;

namespace SetLayeredWindows
{
public class Win32API
{
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct LV_COLUMN
{
public UInt32 mask;

public Int32 fmt;

public Int32 cx;

public String pszText;

public Int32 cchTextMax;

public Int32 iSubItem;

public Int32 iImage;

public Int32 iOrder;
}
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, ref
LV_COLUMN lParam);
[DllImport("user32.dll")]
public static extern int SendMessage (IntPtr hwnd, int wMsg, int wParam,int lParam);
[DllImport("user32.dll")]
public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
public extern static bool IsWindowVisible(IntPtr hWnd);

[DllImport("user32.dll")]
public extern static bool SetForegroundWindow(IntPtr hWnd);

public const int LWA_COLORKEY = 0x0001;

public const int LWA_ALPHA = 0x0002;

public const Int32 WS_EX_LAYERED = 0x80000;

public const int GWL_EXSTYLE = -20;

[DllImport("user32.dll")]
public extern static bool SetLayeredWindowAttributes (IntPtr hWnd, Int32 crKey, byte bAlpha, int dwFlags);

[DllImport("user32.dll")]
public extern static long SetWindowLong(IntPtr hWnd, Int32 nIndex, long dwNewLong);

[DllImport("user32.dll")]
public extern static long GetWindowLong(IntPtr hWnd, Int32 nIndex);

[DllImport("user32.dll")]
public extern static long DrawMenuBar(IntPtr hWnd);

[DllImport("user32.dll")]
public extern static bool LockWindowUpdate(IntPtr hWnd);

public const int WM_COPYDATA = 0x004a;

public static int RGB(Color c)
{
return (int)(c.R | c.G << 8 | c.B << 16);
}

public static int RGB(int r, int g, int b)
{
return (int)(r | g << 8 | b << 16);
}
[StructLayout(LayoutKind.Sequential, Pack=2)]
public struct COPYDATASTRUCT
{
public uint dwData;
public uint cbData;
public IntPtr lpData;
};

[StructLayout(LayoutKind.Sequential, Pack=2)]
public struct data
{
public int nMode;
public IntPtr s1;
public uint ID;
public IntPtr address;
public IntPtr Name;
}
}
}
兔子-顾问 2006-11-14
  • 打赏
  • 举报
回复
窗体的透明色不行。你搜索一下异型窗体。再自己改一下,应该可以达到你的效果。看看窗体的Region属性。
startsmart 2006-11-14
  • 打赏
  • 举报
回复
那个什么秀没用过,不过窗体的opacity属性最好别用,它会把子控件的颜色同时淡化,如果想让窗体透明,可以把窗体的TransparencyKey属性置为你窗体的颜色,这样显示出来的就只有你的控件了
mjm_d 2006-11-13
  • 打赏
  • 举报
回复
等到 vista
dyw31415926 2006-11-13
  • 打赏
  • 举报
回复
不错,mark,简单而好看
xingxing2378 2006-11-13
  • 打赏
  • 举报
回复
可以调整透明度阿
还有就是在颜色上最好从web中选择
是vs2005?
j_lei407 2006-11-13
  • 打赏
  • 举报
回复
如果是winform,直接通过form的Opacity属性就可以了
j_lei407 2006-11-13
  • 打赏
  • 举报
回复
如果是web程序那么通过css就可以控制透明度FILTER: Alpha(Opacity='90');
YapEro 2006-11-13
  • 打赏
  • 举报
回复
表单背景颜色 + Opacity属性,自己多加调试就能实现。不过要实现不规则窗体就有点麻烦。
Little_Ghost 2006-11-13
  • 打赏
  • 举报
回复
设置form的Opacity属性为50%
就可以^^

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧