Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 39657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:53:59+00:00 2026-05-10T14:53:59+00:00

How do you draw a custom button next to the minimize, maximize and close

  • 0

How do you draw a custom button next to the minimize, maximize and close buttons within the Titlebar of the Form?

I know you need to use Win32 API calls and override the WndProc procedure, but I haven’t been able to figure out a solution that works right.

Does anyone know how to do this? More specifically, does anyone know a way to do this that works in Vista?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:53:59+00:00Added an answer on May 10, 2026 at 2:53 pm

    The following will work in XP, I have no Vista machine handy to test it, but I think your issues are steming from an incorrect hWnd somehow. Anyway, on with the poorly commented code.

    // The state of our little button ButtonState _buttState = ButtonState.Normal; Rectangle _buttPosition = new Rectangle();  [DllImport('user32.dll')] private static extern IntPtr GetWindowDC(IntPtr hWnd); [DllImport('user32.dll')] private static extern int GetWindowRect(IntPtr hWnd,                                          ref Rectangle lpRect); [DllImport('user32.dll')] private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); protected override void WndProc(ref Message m) {     int x, y;     Rectangle windowRect = new Rectangle();     GetWindowRect(m.HWnd, ref windowRect);      switch (m.Msg)     {         // WM_NCPAINT         case 0x85:         // WM_PAINT         case 0x0A:             base.WndProc(ref m);              DrawButton(m.HWnd);              m.Result = IntPtr.Zero;              break;          // WM_ACTIVATE         case 0x86:             base.WndProc(ref m);             DrawButton(m.HWnd);              break;          // WM_NCMOUSEMOVE         case 0xA0:             // Extract the least significant 16 bits             x = ((int)m.LParam << 16) >> 16;             // Extract the most significant 16 bits             y = (int)m.LParam >> 16;              x -= windowRect.Left;             y -= windowRect.Top;              base.WndProc(ref m);              if (!_buttPosition.Contains(new Point(x, y)) &&                  _buttState == ButtonState.Pushed)             {                 _buttState = ButtonState.Normal;                 DrawButton(m.HWnd);             }              break;          // WM_NCLBUTTONDOWN         case 0xA1:             // Extract the least significant 16 bits             x = ((int)m.LParam << 16) >> 16;             // Extract the most significant 16 bits             y = (int)m.LParam >> 16;              x -= windowRect.Left;             y -= windowRect.Top;              if (_buttPosition.Contains(new Point(x, y)))             {                 _buttState = ButtonState.Pushed;                 DrawButton(m.HWnd);             }             else                 base.WndProc(ref m);              break;          // WM_NCLBUTTONUP         case 0xA2:             // Extract the least significant 16 bits             x = ((int)m.LParam << 16) >> 16;             // Extract the most significant 16 bits             y = (int)m.LParam >> 16;              x -= windowRect.Left;             y -= windowRect.Top;              if (_buttPosition.Contains(new Point(x, y)) &&                 _buttState == ButtonState.Pushed)             {                 _buttState = ButtonState.Normal;                 // [[TODO]]: Fire a click event for your button                  //           however you want to do it.                 DrawButton(m.HWnd);             }             else                 base.WndProc(ref m);              break;          // WM_NCHITTEST         case 0x84:             // Extract the least significant 16 bits             x = ((int)m.LParam << 16) >> 16;             // Extract the most significant 16 bits             y = (int)m.LParam >> 16;              x -= windowRect.Left;             y -= windowRect.Top;              if (_buttPosition.Contains(new Point(x, y)))                 m.Result = (IntPtr)18; // HTBORDER             else                 base.WndProc(ref m);              break;          default:             base.WndProc(ref m);             break;     } }  private void DrawButton(IntPtr hwnd) {     IntPtr hDC = GetWindowDC(hwnd);     int x, y;      using (Graphics g = Graphics.FromHdc(hDC))     {         // Work out size and positioning         int CaptionHeight = Bounds.Height - ClientRectangle.Height;         Size ButtonSize = SystemInformation.CaptionButtonSize;         x = Bounds.Width - 4 * ButtonSize.Width;         y = (CaptionHeight - ButtonSize.Height) / 2;         _buttPosition.Location = new Point(x, y);          // Work out color         Brush color;         if (_buttState == ButtonState.Pushed)             color = Brushes.LightGreen;         else             color = Brushes.Red;          // Draw our 'button'         g.FillRectangle(color, x, y, ButtonSize.Width, ButtonSize.Height);     }      ReleaseDC(hwnd, hDC); }  private void Form1_Load(object sender, EventArgs e) {     _buttPosition.Size = SystemInformation.CaptionButtonSize; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 223k
  • Answers 223k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should avoid re-inventing the wheel here. Check out gearman.… May 13, 2026 at 12:41 am
  • Editorial Team
    Editorial Team added an answer If you think you have to do that, you could… May 13, 2026 at 12:41 am
  • Editorial Team
    Editorial Team added an answer Try this: import sys current_module = sys.modules[__name__] In your context:… May 13, 2026 at 12:41 am

Related Questions

What I'm trying to do is replicate the NSTokenField like UITextField seen in the
I have a custom control: it's managed code, which subclasses System.Windows.Forms.Control. I want to
I have to build a web application in a couple of weeks in your
in java when you want to do custom painting in a panel, you usually

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.