我在設置Control的自定義邊框時遇到問題。此邊框可以用DrawRectangle的,DrawBorder或其他任何東西,只要我得到這個行爲更改控件的邊框
顯然,最黑暗的邊界就是previouse境界是來完成。其他邊界圍繞它試圖模仿淡出(或其他)。現在,最具挑戰性的是我無法重寫OnPaint或擴展任何其他控件。這必須適用於所有控件! 這是我的擴展提供程序的一部分,我在控件具有焦點時設置這些邊界(如Google Chrome)。
到目前爲止,我已經想出了這個...... 當在擴展提供程序字典中添加控件時,我會連接控件的Enter和Leave事件。在那裏,我得到控制的父母發射事件,並在該形式上繪製這3個矩形。這樣我就在非客戶區域分類繪畫。剩下的事情是繪製控制的實際邊界。我已經嘗試過,但無濟於事。
我也迷上了該控件的paint事件,但ControlPaint.DrawBorder()不起作用。
好的,所以這是在進入和離開的方法。
private void BojajGlow(Graphics gfx, Graphics gfxCtrl, Control parent, Control kontrola, bool novi)
{
Rectangle[] rect = new Rectangle[3];
for (int i = 0; i < 3; i++)
{
int x = kontrola.Location.X - (i + 1);
int y = kontrola.Location.Y - (i + 1);
int w = kontrola.Size.Width + 2 * (i + 1) - 1;
int h = kontrola.Size.Height + 2 * (i + 1) - 1;
rect[i] = new Rectangle(x, y, w, h);
}
if (novi)
{
Color boja = DohvatiOpcije(kontrola).Boja;
for (int i = 0; i < 3; i++)
{
if (i > 0)
boja = Posvjetli(95, ControlPaint.Light(boja));
Pen olovka = new Pen(boja);
olovka.EndCap = olovka.StartCap = LineCap.Round;
olovka.Width = 1;
GraphicsPath gfxPath = new GraphicsPath();
gfxPath.AddRectangle(rect[i]);
gfx.DrawPath(olovka, gfxPath);
}
}
else
{
for (int i = 0; i < 3; i++)
{
Pen olovka = new Pen(parent.BackColor);
olovka.EndCap = olovka.StartCap = LineCap.Round;
olovka.Width = 1;
GraphicsPath gfxPath = new GraphicsPath();
gfxPath.AddRectangle(rect[i]);
gfx.DrawPath(olovka, gfxPath);
}
}
}
從Enter事件它會這樣調用
if (((Control)sender).Parent != null)
BojajGlow(Graphics.FromHwnd(((Control)sender).Parent.Handle), Graphics.FromHwnd(((Control)sender).Handle), ((Control)sender).Parent, (Control)sender, true);
沒有人對此有什麼寶貴的意見?
您是使用WinForms還是WPF? – Bernard
我正在使用WinForms。我不知道WPF,但是我已經看到了一些例子,在這個例子中這是微不足道的:) – Rob
使用用戶選擇的主題設置和WM_NCPAINT消息的默認處理程序繪製邊框。一個難以處理的信息。最簡單的方法是使用無邊框控件,並讓父級繪製邊框。 –