internal static ThemeHandle Create(string className, bool throwExceptionOnFail, IntPtr hWndRef) { // HThemes require an HWND when display scaling is different between monitors. IntPtr hTheme = IntPtr.Zero; try { hTheme = UxTheme.OpenThemeData(hWndRef, className); } catch (Exception e) when(!ClientUtils.IsSecurityOrCriticalException(e)) { if (throwExceptionOnFail) { throw new InvalidOperationException(SR.VisualStyleHandleCreationFailed, e); } return(null); } if (hTheme == IntPtr.Zero) { if (throwExceptionOnFail) { throw new InvalidOperationException(SR.VisualStyleHandleCreationFailed); } return(null); } return(new ThemeHandle(hTheme)); }
private void DrawBorder(System.Drawing.Graphics g) { IntPtr hTheme = UxTheme.OpenThemeData(this.Handle, "EDIT"); // Makes sure Windows XP is running and // a .manifest file exists for the EXE. if (Environment.OSVersion.Version.Major >= 5 && Environment.OSVersion.Version.Minor > 0 && System.IO.File.Exists(Application.ExecutablePath + ".manifest") && (hTheme != IntPtr.Zero)) { //Get DC IntPtr hDC = g.GetHdc(); int state = (int)UxTheme.ETS_NORMAL; switch (this.m_state) { case State.Disabled: state = (int)UxTheme.ETS_DISABLED; break; default: break; } try { RECT r = new RECT(this.ClientRectangle.Left, this.ClientRectangle.Right, this.ClientRectangle.Top, this.ClientRectangle.Bottom); //Render button IntPtr hr = UxTheme.DrawThemeBackground(hTheme, hDC, UxTheme.EP_EDITTEXT, state, r, null); } finally { //Release DC g.ReleaseHdc(hDC); UxTheme.CloseThemeData(hTheme); } } else { using (Graphics y = this.CreateGraphics()) { ControlPaint.DrawBorder(y, ClientRectangle, SystemColors.Control, ButtonBorderStyle.Inset); } } }
private void OnBtnPaint(object sender, System.Windows.Forms.PaintEventArgs e) { IntPtr hTheme = UxTheme.OpenThemeData(this.Handle, "COMBOBOX"); // Makes sure Windows XP is running and // a .manifest file exists for the EXE. if (Environment.OSVersion.Version.Major >= 5 & Environment.OSVersion.Version.Minor > 0 & System.IO.File.Exists(Application.ExecutablePath + ".manifest") & (hTheme != IntPtr.Zero)) { //Grab DC IntPtr hDC = e.Graphics.GetHdc(); int state = UxTheme.CBXS_NORMAL; switch (this.m_state) { case State.Hot: state = UxTheme.CBXS_HOT; break; case State.Pressed: state = UxTheme.CBXS_PRESSED; break; case State.Disabled: state = UxTheme.CBXS_DISABLED; break; default: break; } try { RECT r = new RECT(e.ClipRectangle.Left, e.ClipRectangle.Right, e.ClipRectangle.Top, e.ClipRectangle.Bottom); //Render button UxTheme.DrawThemeBackground(hTheme, hDC, UxTheme.CP_DROPDOWN, state, r, null); } finally { //Release DC e.Graphics.ReleaseHdc(hDC); UxTheme.CloseThemeData(hTheme); } } else { ButtonState state = ButtonState.Normal; switch (this.m_state) { case State.Pressed: state = ButtonState.Pushed; break; case State.Disabled: state = ButtonState.Inactive; break; default: break; } ControlPaint.DrawComboButton(e.Graphics, e.ClipRectangle, state); } }
/// <summary> /// Processes Windows messages /// </summary> /// <param name="m">The Windows Message to process</param> protected override void WndProc(ref Message m) { base.WndProc(ref m); if (!this.visualStylesEnabled || this.BorderStyle != BorderStyle.Fixed3D) { return; } if (m.Msg == (int)WindowMessageFlags.WM_PRINT) { if ((m.LParam.ToInt32() & (int)WmPrintFlags.PRF_NONCLIENT) == (int)WmPrintFlags.PRF_NONCLIENT) { // open theme data IntPtr hTheme = UxTheme.OpenThemeData(this.Handle, UxTheme.WindowClasses.TreeView); if (hTheme != IntPtr.Zero) { // get the part and state needed int partId = (int)UxTheme.Parts.TreeView.TreeItem; int stateId = (int)UxTheme.PartStates.TreeItem.Normal; RECT rect = new RECT(); rect.right = this.Width; rect.bottom = this.Height; RECT clipRect = new RECT(); // draw the left border clipRect.left = rect.left; clipRect.top = rect.top; clipRect.right = rect.left + 2; clipRect.bottom = rect.bottom; UxTheme.DrawThemeBackground(hTheme, m.WParam, partId, stateId, ref rect, ref clipRect); // draw the top border clipRect.left = rect.left; clipRect.top = rect.top; clipRect.right = rect.right; clipRect.bottom = rect.top + 2; UxTheme.DrawThemeBackground(hTheme, m.WParam, partId, stateId, ref rect, ref clipRect); // draw the right border clipRect.left = rect.right - 2; clipRect.top = rect.top; clipRect.right = rect.right; clipRect.bottom = rect.bottom; UxTheme.DrawThemeBackground(hTheme, m.WParam, partId, stateId, ref rect, ref clipRect); // draw the bottom border clipRect.left = rect.left; clipRect.top = rect.bottom - 2; clipRect.right = rect.right; clipRect.bottom = rect.bottom; UxTheme.DrawThemeBackground(hTheme, m.WParam, partId, stateId, ref rect, ref clipRect); } UxTheme.CloseThemeData(hTheme); } } }