Extended the MessageBox function of winform and Customized button text

Source: Internet
Author: User
Tags save file

Recently, in a company project, the customer requested to add a user confirmation action in the function operations, such as "send", "save file", or "cancel ". The first thought of this requirement is MessageBox. Show (). However, the text in the show dialog box is fixed and cannot be changed to the text you need.

What should we do?
Create a form by yourself to show? The method is too stupid and not highly reusable.
Show the MessageBox directly and add the prompt content to tell the user which button represents what it means? It's too people-oriented !!!

It seems that we still need to use the Win32 API. After studying the displayed MessageBox with spy ++, it seems that it is not complicated. Replace the text named "button" of the subcontrol class in the window with the desired content.

Just do it, roll up your pants, pick up your hoe, and encapsulate a "messageboxex" class in MessageBox. add a "buttontitles" string array parameter to the show parameter, which is an array of content to be modified. The specific implementation is not described. See the attachedCode.

The method for obtaining the dialog box form handle is to refer to a code for extending the openfiledialog form. The specific source is not remembered. In fact, with this foundation, you can do a lot of things on the show form, such as adding an image, button, or ***. This depends on the specific needs. Hope to introduce something.

Code:

class messageboxex
{< br> // test sample
static void test ()
{< br> show ("message ", "title", messageboxbuttons. yesnocancel, new string [] {"button 1 (& O)", "button 2 (& T)", "button 3 (& H )"});
}

Public static dialogresult show (string text, string caption, messageboxbuttons buttons, string [] buttontitles)
{< br> dummyform FRM = new dummyform (buttons, buttontitles );
frm. show ();
frm. watchforactivate = true;
dialogresult result = MessageBox. show (FRM, text, caption, buttons);
frm. close ();
return result;
}

Class dummyform: Form
{
Intptr _ HANDLE;
Messageboxbuttons _ buttons;
String [] _ buttontitles = NULL;

Bool _ watchforactivate = false;

Public bool watchforactivate
{
Get {return _ watchforactivate ;}
Set {_ watchforactivate = value ;}
}

Public dummyform (messageboxbuttons buttons, string [] buttontitles)
{
_ Buttons = buttons;
_ Buttontitles = buttontitles;

// make it invisible on the Interface
This. TEXT = "";
This. startposition = formstartposition. manual;
This. location = new point (-32000,-32000);
This. showintaskbar = false;
}

Protected override void onshown (eventargs E)
{
Base. onshown (E );
// Hide yourself and you will not be able to see it in the task list
Nativewin32api. setwindowpos (this. Handle, intptr. 0, 0, 0, 0,659 );
}

Protected override void wndproc (Ref system. Windows. Forms. Message m)
{
If (_ watchforactivate & M. MSG = 0x0006)
{
_ Watchforactivate = false;
_ HANDLE = M. lparam;
Checkmsgbox ();
}
Base. wndproc (ref m );
}

Private void checkmsgbox ()
{
If (_ buttontitles = NULL | _ buttontitles. Length = 0)
Return;

// Index of the button title
Int buttontitleindex = 0;
// Obtain the sub-control handle
Intptr H = nativewin32api. getwindow (_ HANDLE, occommon. Message. gw_child );
While (H! = Intptr. Zero)
{
// Assign the button title in order
If (nativewin32api. getwindowclassname (h). Equals ("button "))
{
If (_ buttontitles. length> buttontitleindex)
{
Nativewin32api. setwindowtext (H, _ buttontitles [buttontitleindex]);
Buttontitleindex ++;
}
}
H = nativewin32api. getwindow (H, occommon. Message. gw_hwndnext );
}
}
}
}

Message used:

Public const int gw_child = 5;
Public const int gw_hwndnext = 2;

Win32 API used:

[dllimport ("user32.dll", charset = charset. auto)]
Public static extern bool setwindowpos (intptr hwnd, intptr hwndinsertafter, int X, int y, int width, int height, int flags );
[dllimport ("user32.dll")]
Public static extern intptr getwindow (intptr hwnd, long wcmd);
[dllimport ("user32.dll")]
Public static extern bool setwindowtext (intptr hwnd, string lpstring);
[dllimport ("user32.dll")]
Public static extern int getclassnamew (intptr hwnd, [financialas (unmanagedtype. lpwstr)] stringbuilder lpstring, int nmaxcount);

Public static string getwindowclassname (intptr handle)
{
Stringbuilder sb = new stringbuilder (256 );

Getclassnamew (handle, Sb, SB. capacity); // obtain the window class name and save it in strclass.
Return sb. tostring ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.