I was wondering how to create a text box that can respond to the "enter" key and create a new line just like the field I am typing in now.
CreateWindow ("edit", 0, WS_CHILD | WS_VISIBLE | WS_BORDER, 5, 5, 130, 20, hwnd, (HMENU) 1000, GetModuleHandle (NULL), NULL);
That is what I have currently, but when I create it, it is a tiny strip and when I press the "Enter" key, it does not jump down to the next line like a paragraph. Any ideas? I am using C++ not VC++ or any .net framework and I am on Windows 7. Thank you.
You need to use the "ES_MULTILINE" flag. See Edit Control Styles
Related
I'm a beginner and today is my first day in learning to create Windows application.
I have two buttons.
#define BUTTON_SW 1
#define BUTTON_SW2 2
HWND Button1;
HWND Button2;
Button1 = CreateWindow("button", "Enter", WS_VISIBLE | WS_CHILD, 215, 10, 40, 25, hwnd, HMENU(BUTTON_SW), NULL, NULL);
Button2 = CreateWindow("button", "You'll be gone", WS_VISIBLE | WS_CHILD, 260, 10, 95, 25, hwnd, HMENU(BUTTON_SW2), NULL, NULL);
When Button1 is clicked, how can I hide Button2 or make it lose its WS_VISIBLE flag and reflect current situation correctly, like this?
LONG style = GetWindowLong(Button2,GWL_STYLE);
style = style | WS_VISIBLE; // style = style & ~WS_VISIBLE
SetWindowLong(Button2,GWL_STYLE,style);
This works very well. However, once WS_VISIBLE flag is assigned, button still stays invisible until the first mouseclick on it.
Vice versa, when I use style = style & ~WS_VISIBLE; once WS_VISIBLE flag is removed, button becomes passive (unclickable) but stays visible.
How to fix this? Tried many things that I've found online but couldn't fix it. Also please don't suggest me to buy a decent book, I don't have the money for now.
(P.S: ShowWindow function with SW_HIDE/SW_SHOW somehow doesn't work for me, perhaps I'm using it wrong. Can you help me on how I can hide this Button2 correctly? I'm trying the following command, but nothing happens.)
ShowWindow(GetDlgItem(Button2, 2), SW_HIDE);
#Edit: I've noticed that when I minimize the app and maximize it
again, the state of button updates. But how will it automatically
update the state?
This should work
ShowWindow(Button2, SW_HIDE);
or
ShowWindow(GetDlgItem(DialogHWND, BUTTON_SW2), SW_HIDE);
GetDlgItem needs HWND of the parent window (dialog) as first argument.
In order to a window to reflect changes you must ask the OS to do it.
Learn about RedrawWindow and the invalidated area.
Note that some actions, like resizing or restoring from minimized, automatically makes the OS to invalidate the region and redraw it.
Use:
RedrawWindow(Button2,NULL,NULL,RDW_INVALIDATE | RDW_INTERNALPAINT);
I am looking to create a checkbox in the C++ WinAPI with a custom bitmap resource as the actual check box.
Unfortunately, when I create a WM_NOTIFY-compatible checkbox (That actually deals with NM_CUSTOMDRAW in a meaningful way), it seems to work just fine except for one thing: The checkbox seems to fade when changing states. This has to do with the visual style, it seems, so I simply unset the visual style with SetWindowTheme(chkLogging, _T(" "), _T(" "));.
HWND chkLogging = CreateWindowEx(
NULL,
_T("button"),
_T("Export to log file"),
WS_VISIBLE | WS_CHILD | BS_CHECKBOX | BS_NOTIFY,
300, 100, 100, 20,
hWnd, (HMENU)IDC_EXPORTLOG, NULL, NULL);
CheckDlgButton(hWnd, IDC_EXPORTLOG, BST_CHECKED);
SetWindowTheme(chkLogging, _T(" "), _T(" "));
Unfortunately, setting the theme for that HWND will override the NM_CUSTOMDRAW rendering. I have tested this by removing the SetWindowTheme line and trying with the default theme, and the checkbox NM_CUSTOMDRAW worked like a charm.
This typically wouldn't be an issue, but it actually works as seemingly intended for normal buttons.
Any assistance on this matter would be greatly appreciated :)
I'm having a problem with multiple lines on a label.
I've tried using ES_MULTILINE, but whenever I use that I get a grey/black rectangle on the location of the label, without the text of the label.
CreateWindowW(L"STATIC", selectedPatternProblem, WS_CHILD | WS_VISIBLE | SS_LEFT | ES_MULTILINE | WM_CTLCOLORSTATIC,500, 190, 380, 90,*hwnd, (HMENU) 1, NULL, NULL);
And when I add WM_CTLCOLORSTATIC, the rectangle is gone, but so is my text.
The 'selectedPatternProblem' is too big to display on just one line, so my question is:
How can I display my label on multiple lines?
Edit: After deleting ES_MULTILINE and WM_CTLCOLORSTATIC, I found out my word was too long. But now I wonder how can I display one long word on multiple lines?
You're using an edit control style (ES_MULTILINE) with a static control, which is not designed to support it. That style probably maps to SS_BLACKRECT or SS_GRAYRECT, which would explain the behavior you're observing.
You should not have to add any style to your current control to achieve what you want, because you're already specifying SS_LEFT, and the documentation says:
SS_LEFT
A simple rectangle and left-aligns the text in the rectangle. The text
is formatted before it is displayed. Words that extend past the end of
a line are automatically wrapped to the beginning of the next
left-aligned line. Words that are longer than the width of the control
are truncated.
Since you're specifying neither SS_LEFTNOWORDWRAP nor one of the SS_*ELLIPSIS styles, the text should wrap between word boundaries.
Note you're also using the WM_CTLCOLORSTATIC message as a style, which will definitely not work as you expect. You should remove that value from your control's styles.
ES_MULTILINE is suitable for Edit controls not labels
MSDN:
Designates a multiline edit control. The default is single-line edit
control.
When the multiline edit control is in a dialog box, the default
response to pressing the ENTER key is to activate the default button.
I used an edit control as an alternative to multi line edit control. I had set the following properties in resource file
EDITTEXT IDC_COMMENT,58,53,75,41,ES_MULTILINE | ES_AUTOHSCROLL | ES_READONLY | NOT WS_BORDER
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
winapi CreateWindowEx -> create button with user system styles?
Hi,
I am kinda new to WinAPI and C++ and I am using Visual Studio 2010. I want to create some buttons in the main window. So there are two questions:
1) can I use a dialog window created with resource editor as a main window so that I wouldnt have to create all controls "by hand" in the "post-WM_CREATE message" section?
2) (If I cant use resource script made window with buttons as a main window) When I "hand make" button using CreateWindowEx like this:
case WM_CREATE:
{
HFONT buttonFont = CreateFont(-11, 0, 0, 0, 400, FALSE, FALSE, FALSE, 1, 400, 0, 0, 0, fontButtonFont);
HWND bMainOK = CreateWindowEx(
0,
WC_BUTTON,
szOkButton,
WS_VISIBLE | WS_CHILD | WS_TABSTOP | BS_PUSHBUTTON,
24, 200, 75, 23,
hWnd,
0,
hInst,
0);
SendMessage(bMainOK, WM_SETFONT, (WPARAM)buttonFont, FALSE);
}
I get very ugly oldstyle button. How do I make it look like Win7/Vista button? Or better how do I make it behave as system style setting says (when using XP get XP style button, when using Vista get Vista style button etc.)?
Thanks
You need to link a manifest to your app that specifies v6 common controls. Websearch will do the rest for you.
I need to create some controls in a MFC dialog dynamically. The creation works fine so far, but the controls created dynamically are looking different from controls created with the resource editor. Some controls even behave different. I think, that I'm missing some initializations the generated code does.
Currently I only create CStatic and CEdit controls. Both don't use the standard windows font when I create them dynamically (the font looks more like the default font used prior to Windows 95, if I remember correctly).
Also, the CEdit control behaves different from when I create it with the resource editor. The dynamically created control seems to limit the text length to the visible size. I can set a longer text with SetWindowText() and read the full text back in with GetWindowText(), but the user can't enter a text longer than the size displayed. The CEdit control created by the resource editor behaves different: If the user enters a text longer than what can be displayed, the entered text ist "scrolled" inside the control (no scrollbars, as its only a single line control).
I tried fixing that problem by calling SetLimitText() on the control, but that didn't change the behavior.
The controls are saved to arrays defined in the dialog-class:
CStatic** m_pLabels;
CEdit** m_pEdits;
The creation of the controls happens in the OnInitDialog() method of the dialog-class:
for (int i = 0; i < max; i++)
{
m_pLabels[i] = new CStatic();
m_pLabels[i]->Create("key", WS_CHILD | WS_VISIBLE | SS_RIGHT,
CRect(10, 10 + i * 30, 130, 35 + i * 30), this);
m_pEdits[i] = new CEdit();
m_pEdits[i]->CreateEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_BORDER,
CRect(133, 10 + i * 30, 350, 35 + i * 30), this, i + 100);
m_pEdits[i]->SetLimitText(499);
m_pEdits[i]->SetWindowText("value to be edited");
}
Thanks for your help!
Dynamically created controls always get the stock font initially: the usual approach is to just set the control's font to the parent dialog's font: something like
pEdits[i]->SetFont(GetFont());
I think the best way to do is to put a control through dialog editor on a dialog, set it's visual styles to the ones of your choice and then open the .rc file in a text editor and copy the styles of that control from there. This way you will be able to create controls that are far more closer to the ones you add through dialog editor.
e.g., after putting a simple button on a dialog having OK/Cancel buttons and a text control, my dialog looks like this in the .rc file:
IDD_MFCAPP_DIALOG DIALOGEX 0, 0, 320, 200
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "MFCApp"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,263,7,50,16
PUSHBUTTON "Cancel",IDCANCEL,263,25,50,16
CTEXT "TODO: Place dialog controls here.",IDC_STATIC,10,96,300,8
PUSHBUTTON "Button1",IDC_BUTTON1,43,17,50,14
END
Now, since I have all the information about how the dialog was created at the back-end, I can simply replicate this behviour through code.
P.S. Off course, you would do this in a separate test project.
You need to add the ES_AUTOHSCROLL style to the editbox. This style is responsible for scrolling the text leftwards when you enter more text than the box can display.
Opening .rc files in text editor and looking up the styles of controls can help you find out such points.