Click here to Skip to main content
14,638,468 members
Rate this:
Please Sign up or sign in to vote.
See more:
Well I never did this before, I don't get any errors but I can't see any header control in my App.

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif
#define _WIN32_IE 0x0300
#include <tchar.h>
#include <windows.h>
#include <commctrl.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

TCHAR szClassName[ ] = _T("Header Control");
HINSTANCE hInst;
HWND hHeader;
int ID_HEADER;


/**********************************************************************************/
HWND DoCreateHeader(HWND hwnd)
{
    HWND hHeader;
    RECT rcParent;
    HDLAYOUT hdl;
    WINDOWPOS wp;

    INITCOMMONCONTROLSEX icex;
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_LISTVIEW_CLASSES;

    InitCommonControlsEx(&icex);

    if((hHeader = CreateWindowEx(0, WC_HEADER, (LPCSTR)NULL, WS_CHILD | WS_VISIBLE |
                             HDS_BUTTONS | HDS_HORZ | WS_BORDER,
                             0, 0, 0, 0, hwnd, (HMENU)ID_HEADER, hInst, (LPVOID)NULL)) = NULL);
                    return (HWND)NULL;

    GetClientRect(hwnd, &rcParent);

    hdl.prc = &rcParent;
    hdl.pwpos = (&wp);


    if(!SendMessage((HWND)hHeader, HDM_LAYOUT, 0, (LPARAM)&hdl))
        return (HWND)NULL;

    SetWindowPos(hHeader, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags | SWP_SHOWWINDOW);

    return(hHeader);
}
/**********************************************************************************/
int DoInsertItem(HWND hwnd, int iInsertAfter, int nWidth, LPTSTR lpsz)
{
    HDITEM hdi;
    int index;

    hdi.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH;
    hdi.cxy = nWidth;
    hdi.pszText = lpsz;
    hdi.cchTextMax = sizeof(hdi.pszText)/sizeof(hdi.pszText[0]);
    hdi.fmt = HDF_LEFT | HDF_STRING;

    index = SendMessage(hHeader, HDM_INSERTITEM, (WPARAM)iInsertAfter, (LPARAM)&hdi);

    return index;
}
/**********************************************************************************/
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_CREATE:
        {
            DoCreateHeader(hwnd);
            break;
        }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
/**********************************************************************************/
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nCmdShow)
{
    HWND hwnd;
    MSG messages;
    WNDCLASSEX exc;

    exc.cbClsExtra    = 0;
    exc.cbWndExtra    = 0;
    exc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
    exc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    exc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    exc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    exc.hInstance     = hInst;
    exc.lpfnWndProc   = WndProc;
    exc.lpszClassName = szClassName;
    exc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
    exc.cbSize        = sizeof (WNDCLASSEX);
    exc.lpszMenuName  = NULL;

    if(!RegisterClassEx(&exc))
        return 0;

    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, szClassName, _T("Header Control"),
                          WS_OVERLAPPEDWINDOW, 350, 130, 700, 500, HWND_DESKTOP,
                          NULL, hInst, NULL) ;

    ShowWindow(hwnd, nCmdShow);

    while(GetMessage(&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    return messages.wParam;
}


What I have tried:

Nothing so far.. :\
I am looking right now if I miss something...
Please help!
Posted
Updated 3-Jul-20 5:59am
v5
Comments
Richard MacCutchan 3-Jul-20 11:08am
   
Please do not just dump your code and expect us to figure out what your problem is. Use the Improve question link above and add proper details of what is wrong, including any error messages.
M@gelearn 3-Jul-20 11:58am
   
Sorry about that.. I didn't say I have errors if you read well the message.. people today get mad cause they have their own problem, and they don't want other, that doesn't mean anyone have to be upset I don't expect anyone to resolve my problem who is bored to such a simple code just ignore me, I'm not at your level. If anyone wanna make me understand what I have done wrong or maybe I miss something bless him. I said I've copy the same link and I don't get any errors my window appears I just don't see any Header control. That's all
Thank you very much for the help..
0x01AA 3-Jul-20 12:10pm
   
But it appears that you have copied a few lines of code from somewhere and do not understand them and are now expecting this forum to explain the code to you...
Richard MacCutchan 3-Jul-20 12:50pm
   
I did say "any error messages", because your question was unclear so I had no idea what was happening when you ran your code. I don't know where you are getting your samples from but most sample code on Microsoft's documentation website does work as listed.
Rate this:
Please Sign up or sign in to vote.

Solution 1

I just used the code from How to Create a Header Control - Win32 apps | Microsoft Docs[^] and it works fine.
   
Comments
0x01AA 3-Jul-20 12:03pm
   
I don't like votes below 4 without a comment, so have something for the balance.
Richard MacCutchan 3-Jul-20 12:48pm
   
Thanks. I guess he didn't like my original comment.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Thank you.. that helps very much !!!
   
Comments
0x01AA 3-Jul-20 12:13pm
   
A 1 for accepting your non solution by yourself as a solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100