Dialog box in C++ not receiving certain keystrokes - c++

As I'm (finally) learning to code Windows apps in C++ I stumbled upon this: I have a dialog box which I create with CreateDialog() and I also have a default button. However, each time I press TAB or ENTER, nothing happens, nor does the control focus change, nor does the default button activate. Here's the resource for my dialog.
IDD_MAINWIND DIALOG 0, 0, 312, 205
STYLE DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_GROUP | WS_POPUP | WS_SYSMENU
EXSTYLE WS_EX_WINDOWEDGE
CAPTION "Dialog"
FONT 8, "Tahoma"
{
PUSHBUTTON "&Send", IDC_BUTTON_SEND , 280, 168, 22, 14, BS_DEFPUSHBUTTON
LTEXT "Hello", IDC_STATIC1, 9, 9, 296, 149, SS_LEFT | SS_NOPREFIX | SS_SUNKEN, WS_EX_TRANSPARENT
EDITTEXT IDC_MESSAGE, 9, 168, 265, 13, ES_AUTOHSCROLL
PUSHBUTTON "Tools", IDC_BUTTON_TOOLS, 8, 185, 146, 14
PUSHBUTTON "Exit", IDC_BUTTON_EXIT, 163, 185, 136, 14
}
Any help is greatly appreciated. Especially now on the 3rd day of Christmas.

The solution is to add WS_TABSTOP styles to every child element of the dialog box.
For ex.:
PUSHBUTTON "&Send", IDC_BUTTON_SEND , 280, 168, 22, 14, BS_DEFPUSHBUTTON | WS_TABSTOP
Be sure that every control has WS_VISIBLE | WS_CHILD styles.
Also do not forget to add WS_EX_CONTROLPARENT style to the window and remove WS_GROUP style from the dialog window.
Moreover
Check your dialog proc
Or if you use window proc instead use the following snippet
Tabbing is provided by the dialog manager, not the Window manager. Therefore, to get tab handling, you need to call IsDialogMessage in your message loop with the hwnd of your window:
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
if (!IsDialogMessage(hwnd, &messages))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
}
Please, learn about WS_TABSTOP and WS_GROUP dialog box items attributes.
This link should be helpful for you: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644994.aspx#controls

Related

How to get numeric updown control?

I have searched in toolbox and many post but don't know where to find this type of control to use on MFC:
Does such a control exist in MFC?
The control is known as a "Spin Control" in the Visual Studio Resource editor and they are normally associated with a "Buddy" edit control. You can set this in the "Behaviour" group of its "Properties" window – with "Auto Buddy" set to "True", it will associate with the 'nearest' edit control (actually, the previous control in the Z-order). You will probably also want to have "Set Buddy Integer" set to "True".
There are numerous routines associated with such controls (such as those to set/get the range and position), and the behaviour is encapsulated in the CSpinButtonCtrl class, for which there is a good overview here.
If you prefer to manually edit/create your resource scripts, then you'll need a control of the msctls_updown32 class, which should be added immediately following the buddy edit window, as in the following code excerpt:
IDP_OPTMD DIALOGEX 0, 0, 262, 180
STYLE WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_CAPTION
CAPTION L"Dilation/Skeletonisation"
FONT 10, P_GUIFONT
{
GROUPBOX L"Pixel Dilation Options", IDC_DILPG, 10, 6, 247, 99, BS_GROUPBOX | SS_NOTIFY
AUTOCHECKBOX L"Enable advanced options dialogue box?", IDC_DILEO, 17, 17, 160, 11, BS_AUTOCHECKBOX
LTEXT L"Cycles =", IDC_DCYCS|0x8000, 190, 18, 30, 11, SS_NOTIFY
EDITTEXT IDC_DCYCS, 221, 17, 28, 12, ES_CENTER | ES_NUMBER, WS_EX_CLIENTEDGE
CONTROL 1, IDC_DCYUD, L"msctls_updown32", UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_AUTOBUDDY |UDS_ARROWKEYS
| UDS_NOTHOUSANDS, 241, 17, 12, 10
//...

Win32 Memory Access Exception with 4+ Edittext controls

I am currently working on a project in win32 using Visual Studio 2010. I am encountering a problem where any dialog I have defined in a resource script (.rc) throws a first chance exception after calling CreateDialog IF the specific dialog has more than 3 Edittext's in it. This is consistent across dialogs in the solution.
The memory access error does not cause execution to halt, but it does cause the dialog to not render on the screen. If I add the DS_NOFAILCREATE style flag, the other controls in the dialog will render, but only 3 of the Edittext's will.
When I run the application created from the build process, all of the dialogs render as expected, and it looks like no first chance exceptions are being thrown.
Any clues or insights into this would be very helpful!
What is happening:
// in my cpp file
HWND myWnd = CreateDialog( hInst, "MY_DIALOG" , pHwnd, (DLGPROC) DlgProc);
//in the header, this setup works fine in other pars of the project
LRESULT CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
In this example, myWnd is coming back as NULL, with an error code of 0. The handle is null because of a failure during creation, which seems to occur on the 4th Edittext control in the resource script. This causes the dialog to not appear when I call ShowWindow().
I am wondering why the CreateDialog is throwing this error ONLY when there are more than 4 Edittext controls.
Adding the rc file
MY_DIALOG DIALOGEX 50, 50, 1000, 1000
STYLE DS_SETFONT | WS_CHILD | WS_BORDER | DS_NOFAILCREATE
FONT 8, "MS Sans Serif", 0,0,0x0
BEGIN
EDITTEXT IDC_EDIT1, 222, 33, 40, 14 , ES_CENTER | ES_AUTOHSCROLL
EDITTEXT IDC_EDIT2, 222, 50, 40, 14 , ES_CENTER | ES_AUTOHSCROLL
EDITTEXT IDC_EDIT3, 222, 65, 40, 14 , ES_CENTER | ES_AUTOHSCROLL
EDITTEXT IDC_EDIT4, 222, 85, 40, 14 , ES_CENTER | ES_AUTOHSCROLL
etc...
END
The error thrown (which does not halt execution) is:
First-chance exception at 0x763aa225 in Program.exe: 0xC0000005: Access violation reading location 0x4c41434f
This is thrown 2-3 times PER editbox over the third.

Tab Control: Partial paint when restoring dialogbox

The edges of my tab control (SysTabControl32) are not fully repainted after restoring dialog box from minimized state.
Example:
The control is defined in the resource file (EDIT all occurences of IDD_VJOYCONF):
IDD_VJOYCONF DIALOGEX 0, 0, 245, 282
STYLE DS_SETFONT | DS_MODALFRAME | DS_3DLOOK | DS_FIXEDSYS | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "vJoyConf - Configure vJoy Devices"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
PUSHBUTTON "Reset All",IDC_RSTALL,98,261,50,14
CONTROL "",IDC_DEVTABS,"SysTabControl32",TCS_MULTILINE | TCS_TOOLTIPS,7,7,231,247
END
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
IDD_VJOYCONF, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 238
TOPMARGIN, 7
BOTTOMMARGIN, 275
END
END
#endif // APSTUDIO_INVOKED
Developing with VS2013 on Windows 8.
The problem was solved by lifting the margin of the dialog box internal to the tab control.
The more interesting thing is how I found the problem.
I used Sysinternals Process Explorer. It has a 'Find Window's Process' button:
[EDIT:]
I hovered over the entire dialog box, highlighting its various elements until I've highlighted the culprit element:
You can clearly see that it's bottom is clipped exactly where the problem is. This is the dialog box that is embedded in the tab control.
The solution was to raise the dialog box so it won't be clipped.

How do I pass a data type to a resource (.rc) file in Win32 C++?

I'd like to center a dialog box in my Win32 application, but am having trouble passing constants to my resource.rc file. I make the following declaration in resource.h:
const int SCREENX = GetSystemMetrics(SM_CXSCREEN);
However, when I replace 100 with SCREENX in my .rc file (below) and build, I get: error RC2108: expected numerical dialog constant.
#include <windows.h>
#include "resource.h"
#include "afxres.h"
// I'd like to replace "100" with "GetSystemMetrics(SM_CXSCREEN)"
ID_ABOUT DIALOG DISCARDABLE 100, 0, 237, 87
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "About"
FONT 10, "MS Sans Serif"
BEGIN
GROUPBOX "Contact", IDC_CONTACT, 7, 43, 98, 39, WS_CHILD | WS_VISIBLE
LTEXT "My info", IDC_CONTACT, 16, 53, 85, 25, WS_CHILD | WS_VISIBLE
END
Neither can I use GetSystemMetrics(SM_CXSCREEN) directly in the desired location (same error).
I'd think it would be routine to pass data types to .rc, so I must be missing something basic here. (Resource files are giving me no end of headaches.) Thank you!
A resource file is compiled into a bunch of static data that is stored in your executable. For example, a dialog resource might be compiled into a DLGTEMPLATE structure.
In other words, everything in a compiled resource is a constant.
GetSystemMetrics(SM_CXSCREEN) is not a constant. It evaluates to the current screen width in pixels at run-time.
As the other answer says, to center your dialog use the DS_CENTER style or handle WM_INITDIALOG.
To center a Dialog on the screen, just OR the DS_CENTER style to the other window style for the dialog.
If you want to use GetSystemMetrics, do this in WM_INITDIALOG and position the dialog there.
A resource file is just a script.

win32 api c++ :How can i color only some text in color?

i have edit text control with text that im loading to it ,
i want some of the text to be in color x and some in color y , how can it be done?
also can i make clickble link inside of the edittext?
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG1 DIALOG 0, 0, 369, 318
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Win32 demo"
FONT 8, "Ms Shell Dlg"
{
EDITTEXT IDC_EDIT2, 8, 1, 353, 86, NOT WS_BORDER | ES_AUTOHSCROLL | ES_MULTILINE | ES_READONLY
}
You can't do that in a standard edit control. You need a rich edit control for that. Based on your previous question I do wonder if what you really need is a hyperlink which you can do with the SysLink control.