コード例 #1
0
ファイル: ToolStripTextBox.cs プロジェクト: dox0/DotNet471RS3
            private void WmNCPaint(ref Message m)
            {
                if (!IsPopupTextBox)
                {
                    base.WndProc(ref m);
                    return;
                }

                // Paint over the edges of the text box.

                // Using GetWindowDC instead of GetDCEx as GetDCEx seems to return a null handle and a last error of
                // the operation succeeded.  We're not going to use the clipping rect anyways - so it's not
                // that bigga deal.
                HandleRef hdc = new HandleRef(this, UnsafeNativeMethods.GetWindowDC(new HandleRef(this, m.HWnd)));

                if (hdc.Handle == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }
                try {
                    // Dont set the clipping region based on the WParam - windows seems to hack out the two pixels
                    // intended for the non-client border.

                    Color outerBorderColor = (MouseIsOver || Focused) ? ColorTable.TextBoxBorder : this.BackColor;
                    Color innerBorderColor = this.BackColor;

                    if (!Enabled)
                    {
                        outerBorderColor = SystemColors.ControlDark;
                        innerBorderColor = SystemColors.Control;
                    }
                    using (Graphics g = Graphics.FromHdcInternal(hdc.Handle)) {
                        Rectangle clientRect = AbsoluteClientRectangle;

                        // could have set up a clip and fill-rectangled, thought this would be faster.
                        using (Brush b = new SolidBrush(innerBorderColor)) {
                            g.FillRectangle(b, 0, 0, this.Width, clientRect.Top);                                  // top border
                            g.FillRectangle(b, 0, 0, clientRect.Left, this.Height);                                // left border
                            g.FillRectangle(b, 0, clientRect.Bottom, this.Width, this.Height - clientRect.Height); // bottom border
                            g.FillRectangle(b, clientRect.Right, 0, this.Width - clientRect.Right, this.Height);   // right border
                        }

                        // paint the outside rect.
                        using (Pen p = new Pen(outerBorderColor)) {
                            g.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1);
                        }
                    }
                }
                finally {
                    UnsafeNativeMethods.ReleaseDC(new HandleRef(this, this.Handle), hdc);
                }
                // we've handled WM_NCPAINT.
                m.Result = IntPtr.Zero;
            }