本文介绍了在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);
}
}

}


}
}
解决方案

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);
				}
			}

		}


	}
}
解决方案


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

10-29 06:32