Win32 API ListView Creation (C++) - c++

I want to create a ListView in c++.
My code so far:
InitCommonControls(); // Force the common controls DLL to be loaded.
HWND list;
// window is a handle to my window that is already created.
list = CreateWindowEx(0, (LPCSTR) WC_LISTVIEWW, NULL, WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_SHOWSELALWAYS | LVS_REPORT, 0, 0, 250, 400, window, NULL, NULL, NULL);
LVCOLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvc.iSubItem = 0;
lvc.pszText = "Title";
lvc.cx = 50;
lvc.fmt = LVCFMT_LEFT;
ListView_InsertColumn(list, 0, &lvc);
But if I compile and execute the code, just a blank window is beeing showed. Compiler: MinGW on Windows 7 (x86).
Can anybody help me showing the listview properly?

Here is the link to original MSDN sample code of ListView control written in Windows API and C.
It compiles in VC++ 2010.

WC_LISTVIEWW (notice the extra W on the end) is a wchar_t*, but you are type-casting it to a char*. That will only compile if UNICODE is not defined, making the generic CreateWindowEx() map to CreateWindowExA(). Which means you are trying to create a Unicode window with the Ansi version of CreateWindowEx(). That will not work.
You need to either:
use the generic WC_LISTVIEW so it matches the generic CreateWindowEx(), and get rid of the type-cast:
list = CreateWindowEx(..., WC_LISTVIEW, ...);
keep using WC_LISTVIEWW, but call CreateWindowExW() instead:
list = CreateWindowExW(..., WC_LISTVIEWW, ...);

Related

How to create a window that behaves like taskbar?

I want to ask how to create a window that behaves like taskbar (shell_traywnd)?
In a windows app called Enable Viacam (camera mouse for disabled people) I saw that the app creates a taskbar-like window on the top of the screen (see the image below) which pulls all other windows underneath it.
Enable_Viacam's window (top of screen)
I used Winspector software to examine this Enable Viacam's window to see its WS_/WS_EX_ properties so that I would try emulate it, but calling CreateWindowEx with those properties didn't give me the wanted result..
Here's my attempt (message loop & window procedure not shown here)
hwnd = CreateWindowEx(
WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CONTROLPARENT,
"#32770","Window",
WS_OVERLAPPEDWINDOW | WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_BORDER | DS_3DLOOK,
0, /* x */
0, /* y */
GetSystemMetrics(SM_CXSCREEN), /* width */
50, /* height */
NULL,NULL,hInstance,NULL);
Any ideas greatly appreciated,
thank you!
Apparently this window type is called an appbar
I created window with style WS_EX_TOOLWINDOW and WS_POPUP and followed that MSDN link
My code now is
APPBARDATA abd = {0};
abd.cbSize = sizeof(APPBARDATA);
abd.hWnd = hwnd;
abd.uCallbackMessage = 888;
SHAppBarMessage(ABM_NEW, &abd);
abd.uEdge = ABE_TOP;
abd.rc.left = 0;
abd.rc.right = GetSystemMetrics(SM_CXSCREEN);
abd.rc.top = 0;
abd.rc.bottom = height;
SHAppBarMessage(ABM_QUERYPOS, &abd);
abd.rc.bottom = abd.rc.top + height;
SHAppBarMessage(ABM_SETPOS, &abd);
Have fun coding guys

Is it possible to change font for an edit control without affecting the other lines?

Hello I want to know if it is possible to change the font of an edit control for some lines only without affecting the remaining:
In my Edit control I have a text but I want some headlines and titles in bigger font and bold while the other lines are with smaller font.
I tried SendMessage(hEdit, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(0, true));
But it sets the whole text in the passed in font.
I thought some messing up with SelectObject(hDcEdit, hFont); But I don't know if it is correct and how.
A standard Edit Control (think, Notepad) does not support what you are looking for. It only supports one Font for the entire text.
What you are looking for is a RichEdit Control instead (think, Wordpad), and in particular its EM_SETCHARFORMAT message, which can be used to apply different formatting (including fonts, colors, etc) to different sections of text.
This is not working with the default Editcontrol, but you can use a Richeditcontrol
#include <Windows.h>
#include <CommCtrl.h>
HINSTANCE relib = LoadLibrary("riched32.dll");
if (relib == NULL) {
MessageBox(NULL, "couldn't load richedit32.dll", "", MB_ICONEXCLAMATION);
hEdit = CreateWindow(RICHEDIT_CLASS, "", WS_VISIBLE | WS_CHILD | ES_MULTILINE |
ES_AUTOHSCROLL | ES_AUTOVSCROLL | WS_VSCROLL | WS_HSCROLL, 0, 0, 200, 200, hWnd, NULL,
NULL, NULL);
Now to set the font to your Richeditcontrol use:
CHARFORMAT2 cf;
memset(&cf, 0, sizeof cf);
cf.cbSize = sizeof cf;
cf.dwMask = CFM_FACE;
wsprintf(cf.szFaceName, "Arial"); //Here you can set the fontname you wont (C:/Windows/Fonts)
SendMessage(hEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);

Win32 Edit printing incorrect characters

I am using a Win32 edit to display debugging information, and I have placed the edit, along with the rest of my basic GUI in a class. But when I output anything to the edit it displays '??????????????????????????'. I think the error lies in my MyGUI::append(LPCSTR) method, although it has always worked perfectly in the past. Any comments/ideas/solutions will be appreciated. If I need to post all the code pertaining to my GUI class please let me know so.
My class lies in the namespace Interface, along with the stand-alone WindowProcedure function, which I call when registering the application with the WNDCLASSEX object.
The win32 edit is not created in the WM_CREATE handle within the WindowProcedure(as it probably should be) as I could not place the function inside my GUI class.
Method that creates the edit:
HWND createEdit( HINSTANCE hInst, HWND hwnd, int appBott, int appTop ){
return CreateWindowEx( WS_EX_APPWINDOW,
TEXT("EDIT"), TEXT(""),
WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY | ES_MULTILINE| WS_VSCROLL | WS_HSCROLL,
10, 10, appBott-25, appTop-50,
hwnd,
(HMENU) 102,
hInst,
NULL );
}
Used in 'guiCreate()' method as:
HWND hEdit = createEdit( hInst, hWin, appWidth, appHeight );
Method that displays text in edit:
void Interface::MyGUI::append( LPCSTR text ){
if( created && !stopAll ){
int TextLen = SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
SendMessageW(hEdit, EM_SETSEL, (WPARAM)TextLen, (LPARAM)TextLen);
SendMessageW(hEdit, EM_REPLACESEL, FALSE, (LPARAM) text);
}
}
Used in main program as:
MyGUI form(); //initialize form
form.append( (LPCSTR)"Example text\n" );
Input text: 'Example text.\n'
Displayed text: '?????????????? l'
You are targeting ANSI it would seem. In that case, don't call SendMessageW, call SendMessageA or even SendMessage and let that be expanded to SendMessageA.
You call SendMessageW but pass ANSI encoded text. When you called SendMessageW you promised to send UTF-16 encoded text.
However, you should stop targeting ANSI I think. Target Unicode instead. Stop using the TEXT() macro and use the L prefix for your string literals. And stop casting string types. That (LPCSTR) cast is asking for trouble. When you cast like that you tell the compiler that you know better than it does. And usually that is not the case.

winapi: removing decoration

This looks like a duplicate but hear me first. This is more on the debugging side.
I'm trying to remove the borders of my window using the method here.
What are some things that will make these functions not work? Hiding windows using ShowWindow(Handle, SW_HIDE) doesn't work also. I've made my own Window class with many functions so I don't wanna paste my whole code here.
Here's my Initialization function for the window:
HRESULT SampleWindow::InitializeSimple(SampleWindow* win)
{
HRESULT hr;
HWND hWnd;
SampleWindow* sampleWin;
sampleWin = new SampleWindow();
aMWindowProps->Center();
hWnd = CreateWindowEx(
NULL,
aMWindowProps->aWindowClass,
aMWindowProps->aWindowTitle,
WS_OVERLAPPEDWINDOW,
aMWindowProps->aRealLeft,
aMWindowProps->aRealTop,
aMWindowProps->GetRelativePosWidth(),
aMWindowProps->GetRelativePosHeight(),
HWND_DESKTOP,
NULL,
*aMWindowProps->aHInstance,
sampleWin);
aMWindowProps->aHwnd = &hWnd;
hr = hWnd ? S_OK : E_FAIL;
win->aHwnd = &hWnd;
//ShowWindow(hWnd, SW_SHOWNORMAL);
return hr;
}
WindowProps as you can see contains various info about the window being created.
I also have a HWND pointer variable in my class which points to the window handler.
Here are some things I've tried on my main, where sw2 is a pointer to my window class:
ShowWindow(*sw2->aHwnd, SW_SHOW);
//ShowWindow(*sw2->aHwnd, nCmdShow);
LONG lStyle = GetWindowLong(*sw2->aHwnd, GWL_STYLE);
lStyle &= WS_POPUP;
//lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU);
SetWindowLong(*sw2->aHwnd, GWL_STYLE, lStyle);
//ShowWindow(*sw2->aHwnd, SW_MINIMIZE);
//ShowWindow(*sw2->aHwnd, SW_HIDE);
//ShowWindow(*sw2->aHwnd, SW_HIDE);
//SetWindowLong(*sw2->aHwnd, GWL_STYLE, GetWindowLong(*sw2->aHwnd, GWL_STYLE) && ~ WS_BORDER && ~ WS_SIZEBOX && ~ WS_DLGFRAME);
SetWindowPos(*sw2->aHwnd, HWND_TOP, sw2->aMWindowProps->aRealLeft, sw2->aMWindowProps->aRealTop, sw2->aMWindowProps->aRealWidth, sw2->aMWindowProps->aRealHeight, SWP_FRAMECHANGED);
//LONG lExStyle = GetWindowLong(*sw2->aHwnd, GWL_EXSTYLE);
//lExStyle &= ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
//SetWindowLong(*sw2->aHwnd, GWL_EXSTYLE, lExStyle);
//SetWindowPos(*sw2->aHwnd, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER);
I'd just like some suggestions on where to debug my code. I know the functions work as I've tested it on a much simpler window project (sample project from Microsoft).
As Jonathan Potter already pointed out in his comment, your mistake is:
aMWindowProps->aHwnd = &hWnd;
hr = hWnd ? S_OK : E_FAIL;
win->aHwnd = &hWnd;
where HWND hWnd is only valid in the scope of the current methode. I guess that you defined aHwnd in your class as something like:
HWND *aHwnd;
which is at least unnecessary. You seem to mistake a HANDLE (a HWND is nothing else) as a kind of instance/object itself. It isn't, it is more like a pointer or reference. You can always safely write:
HWND myAttribute=hWnd;
As long as you use it in the same process. (Window handles are even valid across process boundaries, but do not tell anyone about that).
I fact I know no situation where you keep a pointer to a handle instead of the handle itself.
Notice, I explicit wrote about handles, as HWND are a kind of standard window handles.

How Create derived from CWnd POP UP Window?

I created class CSurfaceWnd from CWnd by Class Wizard. I tried to create window but getting error.
That's my code of creating:
if(!m_pSurfaceWnd)
{
CString m_NameClass = AfxRegisterWndClass(
CS_VREDRAW | CS_HREDRAW,
::LoadCursor(NULL, IDC_ARROW),
(HBRUSH) ::GetStockObject(WHITE_BRUSH),
::LoadIcon(NULL, IDI_APPLICATION));
m_pSurfaceWnd = new CSurfaceWnd;
CRect rcTemp;
GetWindowRect(rcTemp);
VERIFY(m_pSurfaceWnd->CreateEx(WS_EX_CLIENTEDGE, m_NameClass, NULL, WS_POPUP | WS_VISIBLE, rcTemp, mpWnd, 1));
//DWORD dw =GetLastError();
m_pSurfaceWnd->ShowWindow(SW_SHOW);
}
else
m_pSurfaceWnd->ShowWindow(SW_SHOW);
How can You see I'm creating pop up window that's why I'm using CreateEx. I have registered class and in debug mode I see a number of new class in m_NameClass. But CreateEx returned false.
Please help me. Probably you will see some error that I can't see.Please Don't send me to MSDN I have read it a lot of times.
Thank you
First of all, you have to check whether mpWnd is valid object.
BOOL isValid = ::IsWindow(mpWnd->GetSafeHwnd());
If mpWnd is invalid value, CreateEx function will return 0 because of WS_POPUP style.