Another gem of a function!
It's found in the CWindow.inc source code file. Great versatile quick way to get input from the user. Localized button captions, unicode aware, prompt, caption, positioning....
I am using it in WinFBE to prompt the user for a new name for a Theme.
Awesome.
I couldn't find it in the help file, I'm glad I found it here.
See: https://github.com/JoseRoca/WinFBX/blob/master/docs/Windows%20GUI/CWindow%20Class.md#AfxInputBox
Thanks. Bookmarked :)
Edit: When x,y are set to zero, the input box can be briefly seen in the top left corner of the screen, before it is centered.
For the time being, I entered a 1/3,1/2 form position, and this avoids this small issue.
I don't see such phenomenon.
#define UNICODE
#INCLUDE ONCE "Afx/CWindow.inc"
USING Afx
#define ID_INPUTBOX 101
DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
BYVAL hPrevInstance AS HINSTANCE, _
BYVAL szCmdLine AS ZSTRING PTR, _
BYVAL nCmdShow AS LONG) AS LONG
END WinMain(GetModuleHandleW(NULL), NULL, COMMAND(), SW_NORMAL)
' // Forward declaration
DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
BYVAL hPrevInstance AS HINSTANCE, _
BYVAL szCmdLine AS ZSTRING PTR, _
BYVAL nCmdShow AS LONG) AS LONG
' // Set process DPI aware
' // The recommended way is to use a manifest file
AfxSetProcessDPIAware
' // Creates the main window
DIM pWindow AS CWindow
' -or- DIM pWindow AS CWindow = "MyClassName" (use the name that you wish)
pWindow.Create(NULL, "CWindow with a button", @WndProc)
' // Sizes it by setting the wanted width and height of its client area
pWindow.SetClientSize(500, 320)
' // Centers the window
pWindow.Center
' // Adds a button
pWindow.AddControl("Button", , ID_INPUTBOX, "&Input Box", 350, 250, 75, 23)
' // Displays the window and dispatches the Windows messages
FUNCTION = pWindow.DoEvents(nCmdShow)
END FUNCTION
' ========================================================================================
' ========================================================================================
' Main window procedure
' ========================================================================================
FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
SELECT CASE uMsg
CASE WM_COMMAND
SELECT CASE GET_WM_COMMAND_ID(wParam, lParam)
CASE IDCANCEL
' // If ESC key pressed, close the application by sending an WM_CLOSE message
IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
SendMessageW hwnd, WM_CLOSE, 0, 0
EXIT FUNCTION
END IF
CASE ID_INPUTBOX
' // If ESC key pressed, close the application by sending an WM_CLOSE message
IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
DIM cws AS CWSTR = AfxInputBox(hwnd, 0, 0, "InputBox test", "What's your name?", "My name is José")
EXIT FUNCTION
END IF
END SELECT
CASE WM_SIZE
' // Optional resizing code
IF wParam <> SIZE_MINIMIZED THEN
' // Retrieve a pointer to the CWindow class
DIM pWindow AS CWindow PTR = AfxCWindowPtr(hwnd)
' // Move the position of the button
IF pWindow THEN pWindow->MoveWindow GetDlgItem(hwnd, IDCANCEL), _
pWindow->ClientWidth - 120, pWindow->ClientHeight - 50, 75, 23, CTRUE
END IF
CASE WM_DESTROY
' // Ends the application by sending a WM_QUIT message
PostQuitMessage(0)
EXIT FUNCTION
END SELECT
' // Default processing of Windows messages
FUNCTION = DefWindowProcW(hwnd, uMsg, wParam, lParam)
END FUNCTION
' ========================================================================================
With this code I am seeing it on Windows 7. Aero theme off.
Excerpt from CWindow.inc
DIM hInputBox AS HWND = pInputBox.Create(hParent, **cwsCaption, @AfxInputBoxWindowProc, x, y, 326, 142, dwStyle, dwExStyle)
' // Center the window
IF x = 0 AND y = 0 THEN pInputBox.Center(hInputBox, hParent)
As I don't have Windows 7 to test, please try the following to see of it works in your system:
In the AfxInputBox function, change
DIM dwStyle AS DWORD = WS_VISIBLE OR WS_CAPTION OR WS_POPUPWINDOW
to
DIM dwStyle AS DWORD = WS_CAPTION OR WS_POPUPWINDOW
It worked. Problem resolved :)
I just noticed that the framework version is not modal. Jose's code snippet is modal.
What would need to be done to add a modal option to the framework version?
To pass the handle of the parent window.
Thanks, got it:
s = AfxInputBox(Form1.hWindow,0,0,"Search","Look for files that match this filter:",".mp3")
When the first parameter isn't specified, what happens?
Quote from: Bumblebee on December 09, 2020, 08:47:07 PM
When the first parameter isn't specified, what happens?
The desktop becomes the owner of the popup input box form. That's because 0 is normally reserved to designate the Desktop window handle (or you could use the constant HWND_DESKTOP).