如何重绘winform的窗体边框?

afafaf_2008 2008-12-28 11:47:37
我并不想把窗体的边框设为none再去加图片按钮什么的,只是想重绘蓝色的标题栏和那几个最大化最小化关闭按钮,哪位知道的告诉我怎么做啊?还有如果还想重绘窗体左右和下面的边框又怎么做?
...全文
1100 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
datiankuo 2010-07-05
  • 打赏
  • 举报
回复
dssssssssssssssssssssssssssssssssssssss
xu_2007 2008-12-29
  • 打赏
  • 举报
回复
5楼朋友说的方法会闪烁吗?
haha0369 2008-12-29
  • 打赏
  • 举报
回复
研究一下
特别 2008-12-28
  • 打赏
  • 举报
回复
mark
wuyq11 2008-12-28
  • 打赏
  • 举报
回复
wangping_li 2008-12-28
  • 打赏
  • 举报
回复
重载WndProc,然后处理WM_NCCALCSIZE和WM_NCPAINT来重画非用户区(标题栏

参考:
http://9iyou.com/Program_Data/cshap-130392.html
killer_liqiao 2008-12-28
  • 打赏
  • 举报
回复
...
hxp930 2008-12-28
  • 打赏
  • 举报
回复
下面的代码实现了你想要的一部分功能

[DllImport("User32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("User32.dll")]
private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("Kernel32.dll")]
private static extern int GetLastError();

//标题栏按钮的矩形区域。
Rectangle m_rect = new Rectangle(205, 6, 20, 20);

protected override void WndProc(ref Message m)
{

base.WndProc(ref m);

switch (m.Msg)
{
case 0x86://WM_NCACTIVATE
goto case 0x85;

case 0x85://WM_NCPAINT
{
IntPtr hDC = GetWindowDC(m.HWnd);
//把DC转换为.NET的Graphics就可以很方便地使用Framework提供的绘图功能了
Graphics gs = Graphics.FromHdc(hDC);
gs.FillRectangle(new LinearGradientBrush(m_rect, Color.Pink, Color.Purple, LinearGradientMode.BackwardDiagonal), m_rect);
StringFormat strFmt = new StringFormat();
strFmt.Alignment = StringAlignment.Center;
strFmt.LineAlignment = StringAlignment.Center;
gs.DrawString("√", this.Font, Brushes.BlanchedAlmond, m_rect, strFmt);
gs.Dispose();

//释放GDI资源
ReleaseDC(m.HWnd, hDC);
break;
}

case 0xA1://WM_NCLBUTTONDOWN
{
Point mousePoint = new Point((int)m.LParam);
mousePoint.Offset(-this.Left, -this.Top);
if (m_rect.Contains(mousePoint))
{
MessageBox.Show("hello");
}
break;
}
}
}

//在窗口大小改变时及时更新按钮的区域。
private void Form1_SizeChanged(object sender, System.EventArgs e)
{
m_rect.X = this.Bounds.Width - 95;
m_rect.Y = 6;
m_rect.Width = m_rect.Height = 20;
}
lalac 2008-12-28
  • 打赏
  • 举报
回复
override WndProc,重写WM_NCPAINT消息
a12321321321312321 2008-12-28
  • 打赏
  • 举报
回复
http://www.cnblogs.com/volnet/articles/cpp_ondraw.html
可以研究一下。
public void SetBits() { //绘制绘图层背景 Bitmap bitmap = new Bitmap(Main.Width + 10, Main.Height + 10); Rectangle _BacklightLTRB = new Rectangle(20, 20, 20, 20);//窗体光泽重绘边界 Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.HighQuality; //高质量 g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量 ImageDrawRect.DrawRect(g, Properties.Resources.main_light_bkg_top123, ClientRectangle, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), 1, 1); if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = Win32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = Win32.CreateCompatibleDC(screenDC); try { Win32.Point topLoc = new Win32.Point(Left, Top); Win32.Size bitMapSize = new Win32.Size(Width, Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = Byte.Parse("255"); blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } }
自定义窗体的最大化、最小化和关闭按钮, C#移动无标题栏窗体的三种代码: C#移动无标题栏窗体的三种代码:第一种采用,需注意窗体上的控件是否把窗体覆盖了。。。MouseDown、MouseMove、MouseUp事件应该是鼠标所处位置最顶层的控件的事件 在窗体的类中声明两个变量 private Point mouseOffset; //记录鼠标指针的坐标 private bool isMouseDown = false; //记录鼠标按键是否按下 创建该窗体 MouseDown、MouseMove、MouseUp事件的相应处理程序 private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { int xOffset; int yOffset; if (e.Button == MouseButtons.Left) { xOffset = -e.X ; yOffset = -e.Y ; mouseOffset = new Point(xOffset, yOffset); isMouseDown = true; } } private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (isMouseDown) { Point mousePos = Control.MousePosition; mousePos.Offset(mouseOffset.X, mouseOffset.Y); Location = mousePos; } } private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { // 修改鼠标状态isMouseDown的值 // 确保只有鼠标左键按下并移动时,才移动窗体 if (e.Button == MouseButtons.Left) { isMouseDown = false; } } 第二种调用API 未验证 using System.Runtime.InteropServices; [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; private void Form1_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } 第三种未验证 private bool isMouseDown = false; private Point FormLocation; //form的location private Point mouseOffset; //鼠标的按下位置 [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); private const int WM_SYSCOMMAND = 0x0112;//点击窗口左上角那个图标时的系统信息 private const int SC_MOVE = 0xF010;//移动信息 private const int HTCAPTION = 0x0002;//表示鼠标在窗口标题栏时的系统信息 private const int WM_NCHITTEST = 0x84;//鼠标在窗体客户区(除了标题栏和边框以外的部分)时发送的消息 private const int HTCLIENT = 0x1;//表示鼠标在窗口客户区的系统消息 private const int SC_MAXIMIZE = 0xF030;//最大化信息 private const int SC_MINIMIZE = 0xF020;//最小化信息 protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_SYSCOMMAND: if (m.WParam == (IntPtr)SC_MAXIMIZE) { m.WParam = (IntPtr)SC_MINIMIZE; } break; case WM_NCHITTEST: //如果鼠标移动或单击 base.WndProc(ref m);//调用基类的窗口过程——WndProc方法处理这个消息 if (m.Result == (IntPtr)HTCLIENT)//如果返回的是HTCLIENT { m.Result = (IntPtr)HTCAPTION;//把它改为HTCAPTION return;//直接返回退出方法 } break; } base.WndProc(ref m);//如果不是鼠标移动或单击消息就调用基类的窗口过程进行处理 } private void Form1_Load(object sender, EventArgs e) { } ------------------------------- 如何在窗体标题栏左边的控制菜单加入自己的菜单啊? 我们一般在窗口标题栏点右键 或 按Alt+空格 可以弹出那个菜单。 ------解决方案-------------------- using System.Runtime.InteropServices; [DllImport( "user32.dll ")] public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport( "user32.dll ")] public static extern bool InsertMenu(IntPtr hMenu, uint uPosition, uint uFlags, uint uIDNewItem, string lpNewItem); public const int MF_BYCOMMAND = 0; public const int MF_STRING = 0; public const int MF_BYPOSITION = 0x400; public const int MF_SEPARATOR = 0x800; private const uint SC_ABOUT = 0x0001; public const int WM_SYSCOMMAND = 0x0112; private void Form1_Load(object sender, EventArgs e) { IntPtr vMenuHandle = GetSystemMenu(Handle, false); InsertMenu(vMenuHandle, 255, MF_STRING, SC_ABOUT, "About... "); } protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_SYSCOMMAND: if ((uint)m.WParam == SC_ABOUT) { MessageBox.Show( "Zswang 路过! "); } break; } base.WndProc(ref m); }

110,537

社区成员

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

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

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