W32 textbox automatic newline - c

So I have a textbox and I want to make it go to a new line when it reaches the border. Also, I am new to this w32 API. Here is my code:
hEdit=CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD|WS_VISIBLE|
WS_BORDER, 10,
10, 160, 420, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL),
NULL);
HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault,
MAKELPARAM(FALSE,0));
SendMessage(hEdit, WM_SETTEXT, NULL,
(LPARAM)"Insert text here...");

You need to include the ES_MULTILINE style when creating the control.
http://msdn.microsoft.com/en-us/library/windows/desktop/bb775464(v=vs.85).aspx

Related

What means by 'clear the update region' when painting the client area on Windows?

Here the Painting the Window mentioned that:
After you finish painting the client area, you clear the update region, which tells the operating system that it does not need to send another WM_PAINT message until something changes.
One may write these code:
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT rect;
GetClientRect(hWnd, &rect);
HBRUSH brush = CreateSolidBrush(RGB(127, 127, 127));
FillRect(hdc, &rect, brush);
const wchar_t * lstr = L"here is information";
TextOut(hdc,
5, 5,
lstr, _tcslen(lstr));
DrawText(hdc, TEXT("Singleline in center~"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
MoveToEx(hdc, 50, 100, NULL);
LineTo(hdc, 44, 10);
LineTo(hdc, 78, 40);
Rectangle(hdc, 16, 36, 72, 70);
Rectangle(hdc, 34, 50, 54, 70);
DeleteObject(brush);
EndPaint(hWnd, &ps);
}
What means by clear here? We do not want what we have drawn to be cleared.
WM_PAINT messages are generated on demand (see Paint messages will come in as fast as you let them). For each window, the system maintains an update region. Clients can mark parts or all of a window's client area as "invalid", calling InvalidateRect or InvalidateRgn. Either one adds to the update area, but doesn't immediately trigger a WM_PAINT message.
When the system determines that it's time to send a WM_PAINT message, it is the client's responsibility to empty the update region when it's done painting so that no additional WM_PAINT messages are generated until the update region is non-empty again.
The call to BeginPaint does that for you, so you don't have to worry about this so long as you use standard WM_PAINT handling. If you do have more specific requirements (e.g. when using a Direct2D render target) you would have to manually clear the update region with a call to ValidateRect or ValidateRgn.

WINAPI: Window client area not lining up with the window

The client area of my window is not aligning properly with the outer non-client area. Take a look, this is when I am not touching the window, just after I launch it:
It's not aligned, although all dimensions across all functions are ok, 500x500 to be exact.
Now, when I resize it, it kind of aligns correctly:
HWND Window = CreateWindowEx(
0,
WindowClass.lpszClassName,
"Handmade Hero",
WS_OVERLAPPED | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
500,
0,
0,
Instance,
0);
RECT rect;
GetClientRect(Window, &rect);
Win32AllocateMemoryBuffer(&GlobalBackBuffer, rect.right, rect.bottom);
Message handling:
case WM_PAINT:
{
PAINTSTRUCT Paint;
HDC DeviceContext = BeginPaint(Window, &Paint);
Dimension rect;
GetClientRect(Window, &rect);
Win32DisplayBufferInWindow(DeviceContext, &GlobalBackBuffer, Dimension.right, Dimension.bottom);
EndPaint(Window, &Paint);
} break;
The stretchdbits:
StretchDIBits(DeviceContext,
0, 0, WindowWidth, WindowHeight,
0, 0, Buffer->BitmapWidth, Buffer->BitmapHeight,
Buffer->BitmapMemory,
&Buffer->BitmapInfo,
DIB_RGB_COLORS, SRCCOPY);
I've been tracking the dimensions across all the program in VS debugger, but couldn't track the problem. Maybe your intuition would spot something?

Draw a line next to word in WinAPI

How do I draw a line like this that is right next to a word like "Counts", in WinAPI with C?
Using Dialog Resources
Create a static text control with no text that is 1 or 2 pixels in height, turn on the border (WS_BORDER), and set its style to Static Edge (WS_EX_STATICEDGE). Then create a static text control with the word "Counts" in it on top of that. Then use CreateDialog() or DialogBox() to show the dialog box.
IDD_DIALOG1 DIALOGEX 0, 0, 172, 63
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
LTEXT "",IDC_STATIC,6,12,156,1,WS_BORDER,WS_EX_STATICEDGE
LTEXT "Counts ",IDC_STATIC,6,8,26,8
END
Note: This is verbatim what Visual Studio generated using the dialog designer.
Creating Static Controls Using CreateWindow() (as suggested by Jonathan Potter)
LRESULT OnCreate( HWND hWnd, LPCREATESTRUCT lpCreateStruct )
{
// Get default gui font
NONCLIENTMETRICS metrics;
metrics.cbSize = sizeof(NONCLIENTMETRICS);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &metrics, NULL);
HFONT hFont = CreateFontIndirect(&metrics.lfMessageFont);
// Create the line
CreateWindowEx(WS_EX_STATICEDGE, _T("STATIC"), NULL, WS_CHILD|WS_VISIBLE|WS_BORDER,
10, 17, 280, 1, hWnd, NULL, lpCreateStruct->hInstance, NULL);
// Create the Counts label
HWND hwndCounts = CreateWindow(_T("STATIC"), _T("Counts "), WS_CHILD|WS_VISIBLE,
10, 10, 50, 26, hWnd, NULL, lpCreateStruct->hInstance, NULL);
// Apply the default gui font
SendMessage(hwndCounts, WM_SETFONT, (WPARAM)hFont, TRUE);
// Cleanup the font object
DeleteObject(hFont);
}
Drawing manually on the WM_PAINT event
void OnPaint( HWND hWnd )
{
// Get the default font
NONCLIENTMETRICS metrics;
metrics.cbSize = sizeof(NONCLIENTMETRICS);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &metrics, NULL);
HFONT hFont = CreateFontIndirect(&metrics.lfMessageFont);
// Setup HDC
RECT rect;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// Select the default font
SelectObject(hdc, hFont);
// Draw the line using the button shadow
SelectObject(hdc, GetStockObject(DC_PEN));
SetDCPenColor(hdc, GetSysColor(COLOR_BTNSHADOW));
MoveToEx(hdc, 10, 17, NULL);
LineTo(hdc, 280, 17);
// Draw the word Counts overtop of the line
SetRect(&rect, 10, 10, 280, 22);
SetTextColor(hdc, GetSysColor(COLOR_BTNTEXT));
SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
DrawText(hdc, TEXT("Counts "), -1, &rect, DT_NOCLIP);
// Cleanup the font object
DeleteObject(hFont);
// Quit painting
EndPaint(hWnd, &ps);
}
Note: Something I did not account for in this example is the height of the default font. You will want to adjust the code for that.
Here is a screenshot of the output of this method.
In your example, it looked like a single one pixel line, so that's what I drew, but if you'd like to make the line look more like a 'Fixed 3D' or 'lowered bevel line' (which is what the group box tends to draw for it's border line), then you can draw another line below it with the button highlight color.
SetDCPenColor(hdc, GetSysColor(COLOR_BTNHIGHLIGHT));
MoveToEx(hdc, 10, 18, NULL);
LineTo(hdc, 280, 18);
As pointed out by Ben Voigt, it might be better to do this with DrawEdge though.
RECT line;
SetRect(&line, 10, 17, 280,17);
DrawEdge(hdc, &line, EDGE_ETCHED, BF_TOP );
Creating a Group Box Control (suggested by Hans Passant)
Hans Passant's suggestion of doing this with a Group Box did work when I tested it. It still drew a rectangle, and when you enabled visual styles it was very difficult to see. Nevertheless, this should get you started if you want to give it a go.
HWND hwndGroup = CreateWindow(_T("Button"), _T("Counts "),
WS_CHILD|WS_VISIBLE|BS_GROUPBOX, 10, 10, 280, 2, hWnd, NULL,
lpCreateStruct->hInstance, NULL);
SendMessage(hwndGroup, WM_SETFONT, (WPARAM)hFont, TRUE);
Additional Note
Something else I would like to suggest is that you use can use Spy++ which comes with Visual Studio to analyze the window you are looking at. This will tell you at the very least if it's a child control, or whether they are painting it manually. If it's a child control you will also be able to see the rectangle and styles that are applied to it, as well lots of additional information.

Win32: How to create a ListBox control using the CreateWindowExW() function?

I've been through multiple sites, documents and tutorials and they all say the same, that is, any control is nothing more than a window in Win32's API, hence one is able to use the CreateWindowExW() function to create a ListBox control/window over the main application window.
Though I get the concepts of all controls being windows with different dwStyle, I have a hard time finding out how to instantiate, to say so, the ListBox control.
I encountered a tutorial where a dialog is written to have a LISTBOX specified in its declaration as follows:
// resource.h
#define IDD_MAIN 101
#define IDC_TEXT 1000
#define IDC_NUMBER 1001
#define IDC_LIST 1002
#define IDC_ADD 1003
#define IDC_CLEAR 1004
#define IDC_REMOVE 1005
#define IDC_SHOWCOUNT 1006
// .rc resource file
IDD_MAIN DIALOG DISCARDABLE 0, 0, 207, 156
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Controls One"
FONT 8, "MS Sans Serif"
BEGIN
LTEXT "Add",IDC_STATIC,7,10,14,8
EDITTEXT IDC_TEXT,25,7,120,14,ES_AUTOHSCROLL
EDITTEXT IDC_NUMBER,150,7,21,14,ES_NUMBER
LTEXT "times.",IDC_STATIC,177,10,23,8
LISTBOX IDC_LIST,7,25,138,106,LBS_NOINTEGRALHEIGHT |
LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP
PUSHBUTTON "&Add",IDC_ADD,150,30,50,14
PUSHBUTTON "&Remove",IDC_REMOVE,150,47,50,14
PUSHBUTTON "&Clear",IDC_CLEAR,150,63,50,14
LTEXT "This item was added",IDC_STATIC,7,141,66,8
CTEXT "-",IDC_SHOWCOUNT,77,141,32,8
LTEXT "times",IDC_STATIC,114,141,17,8
END
And using it in his C program like so:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
}
Now, this I am able to do and fully understand the concepts. Aside, I would like to be able to create and design my main application window to add a ListBox control to. This tutorial example doesn't use the CreateWindowExW() function to create the control, instead, it creates a dialog that will actually be the main application window.
1 - Any clue on how to add a ListBox control to the main window in code?
I thought about creating it while handling the WM_CREATE message.
2 - Is this a good idea?
3 - What is the best practice/approach in this scenario?
In order to dynamically create a control in Win32's you need the following code:
HWND hBtn, hLabel, hListbox, hTextBox;
void InitializeComponent(HWND hWnd) {
HINSTANCE hInstance = GetModuleHandle(NULL);
// Adding a Button.
hBtn = CreateWindowExW(WS_EX_APPWINDOW,
L"BUTTON", NULL,
WS_CHILD | WS_VISIBLE,
327, 7, 70, 21,
hWnd, NULL, hInstance, NULL);
SetWindowTextW(hBtn, L"&Button");
// Adding a Label.
hLabel = CreateWindowExW(WS_EX_CLIENTEDGE,
L"STATIC", NULL,
WS_CHILD | WS_VISIBLE,
7, 7, 50, 21,
hWnd, NULL, hInstance, NULL);
SetWindowTextW(hLabel, L"Label:");
// Adding a ListBox.
hListBox = CreateWindowExW(WS_EX_CLIENTEDGE,
L"LISTBOX", NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOVSCROLL,
7, 35, 300, 200,
hWnd, NULL, hInstance, NULL);
// Adding a TextBox.
hTextBox = CreateWindowExW(WS_EX_CLIENTEDGE,
L"EDIT", NULL,
WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL,
62, 7, 245, 21,
hWnd, NULL, hInstance, NULL);
SetWindowTextW(hTextBox, L"Input text here...");
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
switch (Msg) {
case WM_CREATE:
InitializeComponent(hWnd);
break;
default:
return DefWindowProcW(hWnd, Msg, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
// Declaring, defining, registering and creating window here...
// Note that each Window/Control has to have its own Message handling function.
}
HWND hListBox; // Handle for list box control
hListBox = CreateWindowEx(
WS_EX_CLIENTEDGE, // extended window styles
"LISTBOX", // list box window class name
NULL,
WS_CHILD | WS_VISIBLE, // window styles
7, // horizontal position
35, // vertical position
300, // width
200, // height
hWnd,
NULL,
hInstance,
NULL
);
if (!hListBox){
// failed to create list box window - take actions ...
}

Displaying a bitmap on a "BUTTON" class window in WIN32

Edit: I think the WM_CREATE message isn't sent during the creation of child windows (namely my button). So by calling SendMessage during WM_CREATE, I'm sending a message to a window that hasn't been created yet. The solution for now is to call SendMessage() during the WM_SHOWWINDOW message. Do child windows send WM_CREATE messages at creation?
Why isn't the bitmap displaying on the button? The bitmap is 180x180 pixels.
I have a resource file with:
Bit BITMAP bit.bmp
I then create the main window and a child "BUTTON" window with:
HWND b, d;
b = CreateWindow(L"a", NULL, WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, 0, 0,
hInstance, 0);
d = CreateWindow(L"BUTTON", NULL, WS_CHILD | WS_VISIBLE | BS_BITMAP,
10, 10, 180, 180, b, 200, hInstance, 0);
Then, in my windows procedure, I send the "BUTTON" window the "BM_SETIMAGE" message with:
HBITMAP hbit;
case WM_CREATE: // It works if I change this to: case WM_SHOWWINDOW
hbit = LoadBitmap(hInstance, L"Bit");
SendMessage(d, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbit);
LoadBitmap() is returning a valid handle because It isn't returning NULL, and I'm able to display the bitmap in the client area using the BitBlt() function. So I'm either not sending the message correctly, or I'm not creating the "BUTTON" window correctly.
What am I doing wrong?
Thanks!
The window procedure for for your window class "a" gets called with WM_CREATE when a window of that class is created. This is during your first call to CreateWindow, which is before you create the child BUTTON window. WM_CREATE means "you are being created" - it doesn't mean "a child is being created".
The solution is to call d = CreateWindow(L"BUTTON"...) in the WM_CREATE handler for class "a":
case WM_CREATE:
d = CreateWindow(L"BUTTON", NULL, WS_CHILD | WS_VISIBLE | BS_BITMAP,
10, 10, 180, 180, hwnd, 200, hInstance, 0);
hbit = LoadBitmap(hInstance, L"Bit");
SendMessage(d, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbit);
How are you verifying that WM_CREATE isn't getting called? Since BUTTON isn't your window class (but rather defined by the OS) it owns the WndProc for the window, not you - therefore WM_CREATE shouldn't be called for the button in your code, because BUTTON isn't your class.
If you want to receive messages for the button, you'll have to subclass it, and then provide your own WndProc.

Resources