I have tried everything but couldn't get the TAB key working to move the focus from one control to another control in a resource dialog. Here is the code:
IDD_DLG_DIALOG DIALOGEX 0, 0, 219, 198
STYLE DS_SETFONT | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_EX_CONTROLPARENT
EXSTYLE WS_EX_APPWINDOW
CAPTION "Caption"
FONT 8, "Tw Cen MT", 400, 0, 0x0
BEGIN
DEFPUSHBUTTON "Done",IDOK,162,175,50,16
EDITTEXT IDC_EDIT1,27,13,185,12,ES_AUTOHSCROLL | WS_TABSTOP | WS_VISIBLE | WS_CHILD
PUSHBUTTON "Add",IDC_Add,109,30,33,13,WS_TABSTOP | WS_VISIBLE | WS_CHILD
LISTBOX IDC_LIST1,7,49,205,121,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP | WS_VISIBLE | WS_CHILD
EDITTEXT IDC_EDIT2,27,31,81,12,ES_AUTOHSCROLL | WS_TABSTOP | WS_VISIBLE | WS_CHILD
LTEXT "Name",IDC_STATIC,7,33,18,11
LTEXT "Link",IDC_STATIC,7,15,15,11
PUSHBUTTON "Delete",IDC_DEL,144,30,33,13 | WS_TABSTOP | WS_VISIBLE | WS_CHILD
CONTROL "Autorun at startup",IDC_CHECK1,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,174,70,10
PUSHBUTTON "Edit",IDC_EDIT,179,30,33,13,WS_TABSTOP | WS_VISIBLE | WS_CHILD
END
Main While loop:
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine,int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow)) return FALSE;
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_STEALTHDIALOG);
while (GetMessage(&msg, NULL, 0, 0))
{
if((!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))||(!IsDialogMessage(msg.hwnd,&msg)))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
Please identify the problem here. Thanks.
Change your || to a &&. At the moment, if TranslateAccelerator returns 0 (which it will unless an accelerator key has been pressed), IsDialogMessage will never be called - and that's what handles the tab key.
(Additionally, as Marco A. implies in his answer, you should pass the HWND of your dialog, and not msg.hwnd as the first parameter of the IsDialogMessage call. But this is immaterial unless IsDialogMessage actually gets called, which it almost always won't while you have || instead of &&).
According to this post and confirmed by this kb article:
TranslateAccelerator() sends WM_COMMAND messages to the window whose
handle you pass as an argument to TranslateAccelerator().
You are probably calling TranslateAccelerator() passing the hwnd member
of your MSG structure; this will be the handle of the window that has
the focus. You should pass the handle of your main windows instead.
And that applies to your code.
Related
I have a button and an EDIT window i named "textbox" initially. What i want to achieve is: when i press the button, the EDIT window's border will be remove and it's text is also changed. Here is how i initial them:
HWND textbox; //global variable
//in WM_CREATE:
CreateWindowEx(NULL, L"BUTTON", L"Remove border",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 10, 10, 150, 40, hwnd,
(HMENU)IDC_TEXTBOX, NULL, NULL);
textbox = CreateWindowEx(
NULL, L"EDIT", NULL,
WS_CHILD | WS_BORDER | WS_VISIBLE | ES_MULTILINE,
100, 100, 200, 100, hwnd, (HMENU) 0, NULL, NULL
);
SetWindowText(textbox, L"the initial text");
Since there is WS_BORDER in its style at the start, i thought removing it from window style will remove the border so this is my first attempt:
//In WM_COMMAND
case IDC_TEXTBOX: //if button is pressed
lStyle = GetWindowLongPtr(textbox, GWL_STYLE);
lStyle &= ~(WS_CHILD | WS_VISIBLE | ES_MULTILINE);
SetWindowLongPtr(textbox, GWL_STYLE, lStyle);
SetWindowPos(textbox, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE |
SWP_NOZORDER | SWP_NOOWNERZORDER);
SetWindowText(textbox, L"how to remove the border around this text???");
break;
The code above didn't work. The window disappeared after i pressed the button. In the second attempt i followed the answer in this question:
case IDC_TEXTBOX:
lStyle = GetWindowLongPtr(textbox, GWL_STYLE);
lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU);
SetWindowLongPtr(textbox, GWL_STYLE, lStyle);
lExStyle = GetWindowLongPtr(textbox, GWL_EXSTYLE);
lExStyle &= ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
SetWindowLongPtr(textbox, GWL_EXSTYLE, lExStyle);
SetWindowPos(textbox, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE |
SWP_NOZORDER | SWP_NOOWNERZORDER);
SetWindowText(textbox, L"how to remove the border around this text???");
break;
This time, the text has changed but the border is still around after pressing the button:
so the question is: What did i do wrong in the 1st and 2nd attempt? And what should i do to remove the EDIT window border?
I would have made this a comment under your answer, but I don't have enough reputation.
Others in the comments have said that "Many of the system controls cache their initial styles and never update them" but strangely you can add a border after creation but just not remove it. So it seems that the border style is actually not cached.
My current solution is to recreate the control. Even though I can live with this solution (at the moment), I do not like it.
-Edit-
Never mind the above. I found that adding WS_BORDER after creation is actually a different border than the border added with WS_BORDER at creation. When you create the edit control with WS_BORDER and add WS_BORDER to it again with SetWindowLong() then the edit control now has 2 visible borders appearing as a single 2 pixel border. The WS_BORDER added after creation can be removed after creation but the initial WS_BORDER added at creation cannot be removed. So it appears that the initial edit border actually does get cached.
I am trying to modify the color of items in my Listbox.
I use WinApi C++ in Visual Studio 2017.
To create my listbox, I wrote that code:
hListbox = CreateWindowW(L"Listbox", L"My Listbox", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | WS_TABSTOP | WS_BORDER | LBS_EXTENDEDSEL, 100, 60, 300, 2400, hWnd, NULL, NULL, NULL);
Add the items:
SendMessage(hListbox, LB_ADDSTRING, NULL, (LPARAM)Text); (...)
Is it possible to change the color of the text or the background, without changing the type of my object? Or is there an other easy way to create a listbox with colored items?
Below is code, which I am using for creating child windows:
case WM_CREATE:
hInst = ((LPCREATESTRUCT) lParam) -> hInstance;
hWnd1 = CreateWindowEx( WS_EX_CLIENTEDGE | WS_EX_LEFT,
"edit", NULL,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | ES_MULTILINE | WS_VSCROLL,
0, 0, 0, 0,
hWnd, (HMENU) 1,
hInst, NULL );
hWnd2 = CreateWindowEx( WS_EX_CLIENTEDGE | WS_EX_LEFT,
"edit", NULL,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | ES_MULTILINE | WS_VSCROLL,
0, 0, 0, 0,
hWnd, (HMENU) 2,
hInst, NULL );
But this code produces horizontal splitter, and I want vertical splitter.
First I thought, if I change height and width parameters, I could create vertical splitter. But it was of no use.
For full code and sample example (in order to save space on SO):
http://old.sumitbirla.com/software/src/splitter.c
So, what is exactly keyword/parameter, which would produce vertical splitter bar.
The code you posted just creates two child windows. It has nothing to do with vertical or horizontal splitter. Handle the WM_SIZE message in the parent window to position and size the child windows for a vertical or horizontal split.
This is simple. I created an EDIT control like this:
HWND MYTEXT= CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", L"",
WS_CHILD|WS_VISIBLE|ES_LEFT|ES_MULTILINE,
20, 120, 150, 20, hWnd, NULL, hInst, NULL);
but when I type text inside of it I can't type more text than the width of the EDIT control. When I reach the end it's like there's no more space and I get a beep. How can I make the text scroll in this situation?
You can give your edit control the WS_HSCROLL and/or WS_VSCROLL window styles. For instance:
HWND myText
= CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", L"",
WS_CHILD | WS_VISIBLE | WS_HSCROLL | ES_LEFT | ES_MULTILINE,
20, 120, 150, 20, hWnd, NULL, hInst, NULL);
Alternately, as Matthew T. Staebler rightfully suggests, use ES_AUTOHSCROLL and/or ES_AUTOVSCROLL (note the ES_ prefix, as these are edit styles, not window styles).
How do I capture key presses when a list view control has focus?
My window creation code looks like
// Window creation
HWND hwnd = CreateWindowEx(WS_EX_TOPMOST | WS_EX_APPWINDOW, g_szClassName, "Test", WS_VISIBLE | WS_BORDER | WS_CAPTION | WS_DLGFRAME | WS_POPUP | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 209, 351, 0, 0, hInstance, 0);
HWND hwnd_list = CreateWindowEx(0, WC_LISTVIEW, "", WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_SINGLESEL | LVS_NOCOLUMNHEADER | LVS_EX_FULLROWSELECT, 1, 246, 201, 55, hwnd, (HMENU)IDL_LISTVIEW, hInstance, 0);
Inside WndProc I process the WM_KEYPRESS message and create a MessageBox displaying the virtual code, but its only triggering when I press keys after clicking outside the list view.
You'll need to take a look at control subclassing, http://msdn.microsoft.com/en-us/library/bb773183%28VS.85%29.aspx.
If you're using MFC, it's a bit less painful. (Back in the day when Borland was still alive, it was a breeze in OWL, but that's ancient history.)
You need to subclass the listview proc and then use 'WM_KEYDOWN' to capture the input.