我用别人的方法换标题栏以后弄背景或者图片的话都不能从本地资源那里导入了,是怎么回事啊?大家帮帮忙啊 ,...代码在这namespace MyQQ
{
public partial class MainForm : Form
{
public LoginForm loginform;
/// <summary>
/// 表示鼠标按下并调整大小位置时的数据状态
/// </summary>
internal static class MState
{
private static int m_Left;
private static int m_Top;
private static int m_Right;
private static int m_Bottom;
private static int m_LastX;
private static int m_LastY;
private static MouseAction m_State;
private static MouseAction m_LastState;
/// <summary>
/// 窗体的左坐标
/// </summary>
public static int Left
{
get { return MState.m_Left; }
}
/// <summary>
/// 窗体的上坐标
/// </summary>
public static int Top
{
get { return MState.m_Top; }
}
/// <summary>
/// 窗体的右坐标
/// </summary>
public static int Right
{
get { return MState.m_Right; }
}
/// <summary>
/// 窗体的下坐标
/// </summary>
public static int Bottom
{
get { return MState.m_Bottom; }
}
/// <summary>
/// 当前的鼠标状态
/// </summary>
public static MouseAction State
{
get { return MState.m_State; }
}
/// <summary>
/// 鼠标按下时的Y坐标
/// </summary>
public static int LastX
{
get { return MState.m_LastX; }
}
/// <summary>
/// 鼠标按下时的X坐标
/// </summary>
public static int LastY
{
get { return MState.m_LastY; }
}
/// <summary>
/// 鼠标按下时的状态
/// </summary>
public static MouseAction LastState
{
get { return MState.m_LastState; }
}
/// <summary>
/// 设置数据值
/// </summary>
/// <param name="bounds">窗体的大小位置</param>
/// <param name="point">鼠标的位置</param>
public static void SetData(Rectangle bounds, Point point)
{
m_Left = bounds.Left;
m_Top = bounds.Top;
m_Right = bounds.Right;
m_Bottom = bounds.Bottom;
m_LastX = point.X;
m_LastY = point.Y;
m_LastState = m_State;
}
/// <summary>
/// 设置状态
/// </summary>
/// <param name="state">要设置的状态值</param>
public static void SetState(MouseAction state)
{
m_State = state;
}
/// <summary>
/// 重置状态
/// </summary>
public static void ResetState()
{
m_State = MouseAction.None;
m_LastState = MouseAction.None;
}
}
internal enum MouseAction
{
None,
Left,
LeftTop,
Top,
RightTop,
Right,
RightBottom,
Bottom,
LeftBottom
} [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
public static extern int GetWindowLong(HandleRef hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong); internal static int WM_NCHITTEST = 0x84; //移动鼠标,按住或释放鼠标时发生的系统消息
internal static IntPtr HTCLIENT = (IntPtr)0x1;//工作区
internal static IntPtr HTSYSMENU = (IntPtr)3;//系统菜单
internal static IntPtr HTCAPTION = (IntPtr)0x2; //标题栏 private int m_BorderWidth = 4;
private int m_CaptionHeight = 22;
public MainForm()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
InitializeComponent();
}
{
public partial class MainForm : Form
{
public LoginForm loginform;
/// <summary>
/// 表示鼠标按下并调整大小位置时的数据状态
/// </summary>
internal static class MState
{
private static int m_Left;
private static int m_Top;
private static int m_Right;
private static int m_Bottom;
private static int m_LastX;
private static int m_LastY;
private static MouseAction m_State;
private static MouseAction m_LastState;
/// <summary>
/// 窗体的左坐标
/// </summary>
public static int Left
{
get { return MState.m_Left; }
}
/// <summary>
/// 窗体的上坐标
/// </summary>
public static int Top
{
get { return MState.m_Top; }
}
/// <summary>
/// 窗体的右坐标
/// </summary>
public static int Right
{
get { return MState.m_Right; }
}
/// <summary>
/// 窗体的下坐标
/// </summary>
public static int Bottom
{
get { return MState.m_Bottom; }
}
/// <summary>
/// 当前的鼠标状态
/// </summary>
public static MouseAction State
{
get { return MState.m_State; }
}
/// <summary>
/// 鼠标按下时的Y坐标
/// </summary>
public static int LastX
{
get { return MState.m_LastX; }
}
/// <summary>
/// 鼠标按下时的X坐标
/// </summary>
public static int LastY
{
get { return MState.m_LastY; }
}
/// <summary>
/// 鼠标按下时的状态
/// </summary>
public static MouseAction LastState
{
get { return MState.m_LastState; }
}
/// <summary>
/// 设置数据值
/// </summary>
/// <param name="bounds">窗体的大小位置</param>
/// <param name="point">鼠标的位置</param>
public static void SetData(Rectangle bounds, Point point)
{
m_Left = bounds.Left;
m_Top = bounds.Top;
m_Right = bounds.Right;
m_Bottom = bounds.Bottom;
m_LastX = point.X;
m_LastY = point.Y;
m_LastState = m_State;
}
/// <summary>
/// 设置状态
/// </summary>
/// <param name="state">要设置的状态值</param>
public static void SetState(MouseAction state)
{
m_State = state;
}
/// <summary>
/// 重置状态
/// </summary>
public static void ResetState()
{
m_State = MouseAction.None;
m_LastState = MouseAction.None;
}
}
internal enum MouseAction
{
None,
Left,
LeftTop,
Top,
RightTop,
Right,
RightBottom,
Bottom,
LeftBottom
} [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
public static extern int GetWindowLong(HandleRef hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong); internal static int WM_NCHITTEST = 0x84; //移动鼠标,按住或释放鼠标时发生的系统消息
internal static IntPtr HTCLIENT = (IntPtr)0x1;//工作区
internal static IntPtr HTSYSMENU = (IntPtr)3;//系统菜单
internal static IntPtr HTCAPTION = (IntPtr)0x2; //标题栏 private int m_BorderWidth = 4;
private int m_CaptionHeight = 22;
public MainForm()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
InitializeComponent();
}
/// 重写以处理重置鼠标光标信息
/// </summary>
/// <param name="e"></param>
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
if (this.Bounds.Contains(Cursor.Position))
{
this.Cursor = Cursors.Default;
}
}
/// <summary>
/// 由指定点设置鼠标动作
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
private MouseAction setMouse(Point point)
{
Rectangle rect = this.ClientRectangle; if ((point.X <= rect.Left + m_BorderWidth) && (point.Y <= rect.Top + m_BorderWidth))
{
//左上
this.Cursor = Cursors.SizeNWSE;
return MouseAction.LeftTop;
}
else if ((point.X >= rect.Left + rect.Width - m_BorderWidth) && (point.Y <= rect.Top + m_BorderWidth))
{
//右上
this.Cursor = Cursors.SizeNESW;
return MouseAction.RightTop;
}
else if ((point.X <= rect.Left + m_BorderWidth) && (point.Y >= rect.Top + rect.Height - m_BorderWidth))
{
//左下
this.Cursor = Cursors.SizeNESW;
return MouseAction.LeftBottom;
}
else if ((point.X >= rect.Left + rect.Width - m_BorderWidth) && (point.Y >= rect.Top + rect.Height - m_BorderWidth))
{
//右下
this.Cursor = Cursors.SizeNWSE;
return MouseAction.RightBottom;
}
else if ((point.X <= rect.Left + m_BorderWidth - 1))
{
//左
this.Cursor = Cursors.SizeWE;
return MouseAction.Left;
}
else if ((point.X >= rect.Left + rect.Width - m_BorderWidth))
{
//右
this.Cursor = Cursors.SizeWE;
return MouseAction.Right;
}
else if ((point.Y <= rect.Top + m_BorderWidth - 1))
{
//上
this.Cursor = Cursors.SizeNS;
return MouseAction.Top;
}
else if ((point.Y >= rect.Top + rect.Height - m_BorderWidth))
{
//下
this.Cursor = Cursors.SizeNS;
return MouseAction.Bottom;
}
this.Cursor = Cursors.Default;
return MouseAction.None;
}
-----------
这句话就决定了你的窗体是自绘的,系统不再自动处理绘图,所以你换了背景图看不出效果。
你可以重写基类的OnPaint方法使用e.Graphics.DrawImage来绘你换的背景图,比如:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawImage(this.BackgroundImage, 0, 0, this.Width, this.Height);
}
{
//base.OnPaintBackground(e);这里的注释不要去掉。
}