在Windows 7中的Titlebar中绘制图形 [英] Drawing Graphics in Titlebar in Windows 7

查看:53
本文介绍了在Windows 7中的Titlebar中绘制图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 7的标题栏和边框中绘图有问题。下面的代码在XP中工作正常,但在Windows 7中失败。有趣的是,如果我将此代码放入表单并从其他表单继承,我可以在设计时看到图形绘图。但是当我运行应用程序并显示表单时,图形就会消失。有谁知道我可以做些什么来解决这个问题,以便它可以在Windows 7中运行。



使用System; 
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;

命名空间WindowsFormsApplication1
{
公共部分类Form1:表格
{
public Form1(){
InitializeComponent();
}

[DllImport(user32.dll)]
static extern int ReleaseDC(IntPtr hWnd,IntPtr hDC);

[DllImport(User32.dll)]

private static extern IntPtr GetWindowDC(IntPtr hWnd);

protected override void WndProc(ref System.Windows.Forms.Message m){

const int WM_NCPAINT = 0x85;
const int WM_NCCREATE = 0x0081;
const int WM_NCCALCSIZE = 0x0083;
const int WM_NCACTIVATE = 0x0086;

SolidBrush myBrush;

base.WndProc(ref m);
if(m.Msg == WM_NCACTIVATE || m.Msg == WM_NCPAINT || m.Msg == WM_NCCREATE || m.Msg == WM_NCCALCSIZE){
IntPtr hdc = GetWindowDC(m.HWnd );
if((int)hdc!= 0){
Graphics g = Graphics.FromHdc(hdc);

myBrush = new SolidBrush(SystemColors.ActiveCaption);
Pen myPen = new Pen(myBrush,5);

myBrush = new SolidBrush(Color.FromArgb(255,255,0,0));
g.FillRectangle(myBrush,0,0,150,SystemInformation.CaptionHeight);

myBrush = new SolidBrush(Color.FromArgb(120,255,0,0));
myPen = new Pen(myBrush,2);
g.DrawRectangle(myPen,0,1,150,SystemInformation.CaptionHeight);

myBrush = new SolidBrush(Color.FromArgb(255,255,0,0));
g.FillEllipse(myBrush,150 - 20,0,30,SystemInformation.CaptionHeight);

myBrush = new SolidBrush(Color.FromArgb(80,255,0,0));
g.FillEllipse(myBrush,150 - 17,1,30,SystemInformation.CaptionHeight + 1);

myBrush = new SolidBrush(Color.FromArgb(150,255,0,0));
myPen = new Pen(myBrush,SystemInformation.FrameBorderSize.Width * 2);
g.DrawRectangle(myPen,0,0,this.Width,this.Height);


myBrush = new SolidBrush(Color.White);
Font myFont = new Font(new FontFamily(Arial),18,FontStyle.Bold,GraphicsUnit.Pixel);
g.DrawString(Some Text,myFont,myBrush,5,3);

g.Flush();
ReleaseDC(m.HWnd,hdc);
}
}

}


}
}

解决方案

正如我所说,这个问题很有意思,但是我认为解决这个问题太无聊了。 :-)



我的想法是:无论如何,你在最好的情况下可以达到的效果是非常有限的,但是通过使用这样的效果,你会严重牺牲平台兼容性你的代码。



通常的(我认为,实用的)解决方法是:创建一个没有标题栏和边框的模板,并模仿所有非客户端通过客户区域中的自定义控件放置区域。从风格的角度来看,即使您可以检查系统范围的Windows指标,也不应该尝试遵循常规的Windows样式。恰恰相反:风格应该脱颖而出并独立于系统范围的UI风格。你甚至可以轻松制作非矩形窗口及其内部元素。



我非常肯定你知道这种可能性。我只是试图说服你说它具有实际意义,而绘制非客户区域的想法是值得怀疑的。将其他常规窗户留给Aero,无论如何都难以模仿,在没有平台不兼容的情况下做自己的事情。



-SA

I am having a problem drawing in the titlebar and border in Windows 7. The code below works fine in XP, but fails in Windows 7. Interestingly, if I place this code into a form and inherit it from another form, I can see the graphics drawing at design time. But when I run the app and display the form the graphics go away. Does anyone know what I can do to fix this so that it works in Windows 7.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1() {
			InitializeComponent();
		}

		[DllImport("user32.dll")]
		static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

		[DllImport("User32.dll")]

		private static extern IntPtr GetWindowDC(IntPtr hWnd);

		protected override void WndProc(ref System.Windows.Forms.Message m) {
			
			const int WM_NCPAINT = 0x85;
			const int WM_NCCREATE = 0x0081;
			const int WM_NCCALCSIZE = 0x0083;
			const int WM_NCACTIVATE = 0x0086;

			SolidBrush myBrush;

			base.WndProc(ref m);
			if (m.Msg == WM_NCACTIVATE || m.Msg == WM_NCPAINT || m.Msg == WM_NCCREATE || m.Msg == WM_NCCALCSIZE) {
				IntPtr hdc = GetWindowDC(m.HWnd);
				if ((int)hdc != 0) {
					Graphics g = Graphics.FromHdc(hdc);

					myBrush = new SolidBrush(SystemColors.ActiveCaption);
					Pen myPen = new Pen(myBrush, 5);
					
					myBrush = new SolidBrush(Color.FromArgb(255, 255, 0, 0));
					g.FillRectangle(myBrush, 0, 0, 150, SystemInformation.CaptionHeight); 

					myBrush = new SolidBrush(Color.FromArgb(120, 255, 0, 0));
					myPen = new Pen(myBrush, 2);
					g.DrawRectangle(myPen, 0, 1, 150, SystemInformation.CaptionHeight);

					myBrush = new SolidBrush(Color.FromArgb(255, 255, 0, 0));
					g.FillEllipse(myBrush, 150 - 20, 0, 30, SystemInformation.CaptionHeight); 

					myBrush = new SolidBrush(Color.FromArgb(80, 255, 0, 0));
					g.FillEllipse(myBrush, 150 - 17, 1, 30, SystemInformation.CaptionHeight + 1); 

					myBrush = new SolidBrush(Color.FromArgb(150, 255, 0, 0));
					myPen = new Pen(myBrush, SystemInformation.FrameBorderSize.Width * 2);
					g.DrawRectangle(myPen, 0, 0, this.Width, this.Height);


					myBrush = new SolidBrush(Color.White);
					Font myFont = new Font(new FontFamily("Arial"), 18, FontStyle.Bold, GraphicsUnit.Pixel);
					g.DrawString("Some Text", myFont, myBrush, 5, 3);

					g.Flush();
					ReleaseDC(m.HWnd, hdc);
				}
			}

		}


	}
}

解决方案

As I say, the question is interesting, but going in for resolving it I think is too boring. :-)

My idea is: the effects you can achieve in best case are very limiting anyway, but by using such effects you badly sacrifice platform compatibility of your code.

The usual (and I think, practical) workaround is this: create a form without title bar and borders and mimic all non-client area by custom controls placed in client area. From the stylistic point of view, you should not try to follow regular windows styles, even though you could check with system-wide windows metrics. Just the opposite: the style should stand out and be independent on system-wide UI style. You can even easily make non-rectangular window and its inner elements.

I''m pretty much sure you knew about this possibility. I merely tried to convince you that it makes practical sense, while the idea to paint non-client areas is questionable. Leave other, regular windows to Aero, which you hardly can mimic anyway, do something of your own without platform incompatibilities.

—SA


这篇关于在Windows 7中的Titlebar中绘制图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆