The inputbox function in VB is easy to use and convenient. It is used in many occasions. There is no ready-made inputbox function in VC ++ 6, do I have to add a dialog and drag and drop controls to implement this function? I have been searching for it. Can I write a function or class similar to VB inputbox just by writing code? Looking online, this idea is indeed feasible, and it has already been implemented by a pioneer (on the codeproject website, someone has already implemented code change). Download it, study it, and share it. The Code is as follows:
(Source: http://www.codeproject.com/KB/dialog/inputbox.aspx)
Inputbox. h ----------------------------------------------------
// Inputbox. h: interface for the cinputbox class.
//
//////////////////////////////////////// //////////////////////////////
# If! Defined (afx_inputbox_h00000be6b01b_c74a_45fe_af35_d6e8e4b65a1b0000included _)
# Define afx_inputbox_h1_0be6b01b_c74a_45fe_af35_d6e8e4b65a1b1_encoded _
# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000
# Define inputbox_width 400
# Define inputbox_height 125
/*
Author: Mah
Date: 13.06.2002
Description:
Similar to Visual Basic inputbox
*/
Class cinputbox
{
Static hfont m_hfont;
Static hwnd m_hwndinputbox;
Static hwnd m_hwndparent;
Static hwnd m_hwndedit;
Static hwnd m_hwndok;
Static hwnd m_hwndcancel;
Static hwnd m_hwndprompt;
Static hinstance m_hinst;
Static lresult callback wndproc (hwnd, uint message, wparam, lparam );
Public:
// Text from inputbox
Lptstr text;
Bool domodal (maid, maid );
Cinputbox (hwnd hwndparent );
Virtual ~ Cinputbox ();
};
# Endif //! Defined (afx_inputbox_h00000be6b01b_c74a_45fe_af35_d6e8e4b65a1b0000included _)
Inputbox. cpp ----------------------------------------------------
# Include "stdafx. H"
# Include "inputbox. H"
Hfont cinputbox: m_hfont = NULL;
Hwnd cinputbox: m_hwndinputbox = NULL;
Hwnd cinputbox: m_hwndparent = NULL;
Hwnd cinputbox: m_hwndedit = NULL;
Hwnd cinputbox: m_hwndok = NULL;
Hwnd cinputbox: m_hwndcancel = NULL;
Hwnd cinputbox: m_hwndprompt = NULL;
Hinstance cinputbox: m_hinst = NULL;
//////////////////////////////////////// //////////////////////////////
// Cinputbox class
//////////////////////////////////////// //////////////////////////////
//////////////////////////////////////// //////////////////////////////
// Construction/destruction
//////////////////////////////////////// //////////////////////////////
/*
Author: Mah
Date: 13.06.2002
Description:
Constructs window class inputbox
*/
Cinputbox: cinputbox (hwnd hwndparent)
{
Hinstance hinst = getmodulehandle (null );
Wndclassex wcex;
If (! Getclassinfoex (hinst, "inputbox", & wcex ))
{
Wcex. cbsize = sizeof (wndclassex );
Wcex. Style = cs_hredraw | cs_vredraw;
Wcex. lpfnwndproc = (wndproc) wndproc;
Wcex. cbclsextra = 0;
Wcex. cbwndextra = 0;
Wcex. hinstance = hinst;
Wcex. hicon = NULL; // loadicon (hinst, (lpctstr) idi_myinputbox );
Wcex. hcursor = loadcursor (null, idc_arrow );
Wcex. hbrbackground = (hbrush) (color_window );
Wcex. lpszmenuname = NULL;
Wcex. lpszclassname = "inputbox ";
Wcex. hiconsm = NULL;
If (registerclassex (& wcex) = 0)
MessageBox (null, "can't create cinputbox! "," Error ", mb_ OK );
}
M_hwndparent = hwndparent;
TEXT = NULL;
}
Cinputbox ::~ Cinputbox ()
{
If (text) Delete [] text;
}
/*
Author: Mah
Date: 13.06.2002
Description: window procedure
*/
Lresult callback cinputbox: wndproc (hwnd, uint message, wparam, lparam)
{
Logfont lfont;
Switch (Message)
{
Case wm_create:
// Font
Memset (& lfont, 0, sizeof (lfont ));
Lstrcpy (lfont. lffacename, _ T ("Arial "));
Lfont. lfheight = 16;
Lfont. lfweight = fw_normal; // fw_bold;
Lfont. lfitalic = false;
Lfont. lfcharset = default_charset;
Lfont. lfoutprecision = out_default_precis;
Lfont. lfclipprecision = clip_default_precis;
Lfont. lfquality = default_quality;
Lfont. lfpitchandfamily = default_pitch;
M_hfont = createfontindirect (& lfont );
M_hinst = getmodulehandle (null );
// Creating Edit
M_hwndedit = createjavaswex (ws_ex_staticedge,
"Edit ","",
Ws_visible | ws_child | ws_tabstop | es_autohscroll,
5, inputbox_height-50, inputbox_width-16, 20,
Hwnd,
Null,
M_hinst,
Null );
// Setting font
Sendmessage (m_hwndedit, wm_setfont, (wparam) m_hfont, 0 );
// Button OK
M_hwndok = createjavaswex (ws_ex_staticedge,
"Button", "OK ",
Ws_visible | ws_child | ws_tabstop,
Inputbox_width-100, 10, 90, 25,
Hwnd,
Null,
M_hinst,
Null );
// Setting font
Sendmessage (m_hwndok, wm_setfont, (wparam) m_hfont, 0 );
// Button cancel
M_hwndcancel = createjavaswex (ws_ex_staticedge,
"Button", "cancel ",
Ws_visible | ws_child | ws_tabstop,
Inputbox_width-100, 40, 90, 25,
Hwnd,
Null,
M_hinst,
Null );
// Setting font
Sendmessage (m_hwndcancel, wm_setfont, (wparam) m_hfont, 0 );
// Static propmpt
M_hwndprompt = createjavaswex (ws_ex_staticedge,
"Static ","",
Ws_visible | ws_child,
5, 10, inputbox_width-110, inputbox_height-70,
Hwnd,
Null,
M_hinst,
Null );
// Setting font
Sendmessage (m_hwndprompt, wm_setfont, (wparam) m_hfont, 0 );
Setfocus (m_hwndedit );
Break;
Case wm_destroy:
Deleteobject (m_hfont );
Enablewindow (m_hwndparent, true );
Setforegroundwindow (m_hwndparent );
Destroywindow (hwnd );
Postquitmessage (0 );
Break;
Case wm_command:
Switch (hiword (wparam ))
{
Case bn_clicked:
If (hwnd) lparam = m_hwndok)
Postmessage (m_hwndinputbox, wm_keydown, vk_return, 0 );
If (hwnd) lparam = m_hwndcancel)
Postmessage (m_hwndinputbox, wm_keydown, vk_escape, 0 );
Break;
}
Break;
Default:
Return defwindowproc (hwnd, message, wparam, lparam );
}
Return 0;
}
/*
Author: Mah
Date: 13.06.2002
Description:
Constructs inputbox window
*/
Bool cinputbox: domodal (lpctstr szcaption, lpctstr szprompt)
{
Rect R;
Getwindowrect (getmediatopwindow (), & R );
M_hwndinputbox = createjavaswex (ws_ex_toolwindow,
"Inputbox ",
Szcaption,
Ws_popupwindow | ws_caption | ws_tabstop,
(R. Right-inputbox_width)/2, (R. Bottom-inputbox_height)/2,
Inputbox_width, inputbox_height,
M_hwndparent,
Null,
M_hinst,
Null );
If (m_hwndinputbox = NULL)
Return false;
Setwindowtext (m_hwndprompt, szprompt );
Setforegroundwindow (m_hwndinputbox );
Enablewindow (m_hwndparent, false );
Showwindow (m_hwndinputbox, sw_show );
Updatewindow (m_hwndinputbox );
Bool ret = 0;
MSG;
Hwnd hwndfocused;
While (getmessage (& MSG, null, 0, 0 ))
{
If (msg. Message = wm_keydown)
{
If (msg. wparam = vk_escape)
{
Sendmessage (m_hwndinputbox, wm_destroy, 0, 0 );
Ret = 0;
}
If (msg. wparam = vk_return)
{
Int ncount = getwindowtextlength (m_hwndedit );
Ncount ++;
If (text)
{
Delete [] text;
TEXT = NULL;
}
TEXT = new tchar [ncount];
Getwindowtext (m_hwndedit, text, ncount );
Sendmessage (m_hwndinputbox, wm_destroy, 0, 0 );
Ret = 1;
}
If (msg. wparam = vk_tab)
{
Hwndfocused = getfocus ();
If (hwndfocused = m_hwndedit) setfocus (m_hwndok );
If (hwndfocused = m_hwndok) setfocus (m_hwndcancel );
If (hwndfocused = m_hwndcancel) setfocus (m_hwndedit );
}
}
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Return ret;
}
Code example for calling VC ++ 6 --------------------------------------------------------
Cinputbox ibox (this-> m_hwnd );
If (ibox. domodal ("Hi, it's me", "if u know what I mean! "))
MessageBox (null, ibox. Text, "aaaaaa", mb_ OK );
Wxdev-C ++ call code example ---------------------------------------------------
// Insert your code here
Cinputbox CIB (hwnd _ *) This-> gethandle ());
CIB. domodal ("system maintenance", "enter the password to exit the program for maintenance ");
MessageBox (0, CIB. Text, CIB. Text, mb_ OK );