NO

Author Topic: Child Window  (Read 4442 times)

nancy

  • Guest
Child Window
« on: June 04, 2010, 02:07:51 AM »
Hallo,

I would like to create a window with a statusbar and scrollbars.
The scrollbares should NOT include the statusbar.
Does this mean that I have to create an additional child window with WS_V(H)SCROLL enabeled?


Nancy

addniim

  • Guest
Re: Child Window
« Reply #1 on: June 04, 2010, 05:54:45 AM »
Yes, I'm too!  :o

nancy

  • Guest
Re: Child Window
« Reply #2 on: June 04, 2010, 03:09:35 PM »
I tried it this way:

------------------------------------------------------------------------
#define WIN32_LEAN_AND_MEAN  /* speed up compilations */
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <tchar.h>

#define IDR_MNU_MAIN   2001
#define ID_STATUSBAR    7001
#define IDR_ICO_MAIN   8001

static HWND hwndStatus, hwndImg;
static int  WidthsStatus[2];
static HINSTANCE hi;

void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) {}
void Main_OnDestroy(HWND hwnd) { PostQuitMessage(0);}


BOOL Main_OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
{
   hwndStatus = CreateWindowEx(
      0L,                     // extended style
      STATUSCLASSNAME,         // status bar
      "",                     // title
      WS_CHILD | WS_BORDER |
      WS_VISIBLE | SBS_SIZEGRIP,   // styles
      0,0,0,0,               // x,y,width,height
      hwnd,                  // parent
      (HMENU)ID_STATUSBAR,      // id
      hi,                     // instance
      NULL);                  // window data

   if (!hwndStatus) MessageBox (NULL, "Status Bar not created!", NULL, MB_OK );

   hwndImg = CreateWindowEx(0L,
         _T("MyImgClass"),
         (LPCTSTR) NULL,
         WS_CHILDWINDOW | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL,
         0,0,0,0,
          hwnd,     
          NULL,
          hi,
         NULL);
             
   if (!hwndImg) MessageBox (NULL, "Image Window not created!", NULL, MB_OK );
   

   ShowWindow(hwndImg, SW_SHOW);

   SendMessage (hwndStatus, SB_SETPARTS, 2, (LPARAM)WidthsStatus);
   SendMessage (hwndStatus, SB_SETTEXT, 0, (LPARAM)"Mouse:");
   SendMessage (hwndStatus, SB_SETTEXT, 1 | SBT_POPOUT, (LPARAM)"Coords:");
       
        return 1;
}


void Main_OnSize(HWND hwnd, UINT state, int cx, int cy)
{
   MoveWindow (hwndImg, 0, 0, cx, cy - 10, TRUE);
    MoveWindow (hwndStatus, 0, cy - 10, cx, cy, TRUE);
   WidthsStatus[0] = cx / 4;
   WidthsStatus[1] = -1;
    SendMessage (hwndStatus, SB_SETPARTS, 2, (LPARAM)WidthsStatus);
}


LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        HANDLE_MSG(hwnd, WM_COMMAND,   Main_OnCommand);
        HANDLE_MSG(hwnd, WM_DESTROY,   Main_OnDestroy);
        HANDLE_MSG(hwnd, WM_CREATE,      Main_OnCreate);
        HANDLE_MSG(hwnd, WM_SIZE,      Main_OnSize);
     
        default: return DefWindowProc(hwnd, msg, wParam, lParam);
    }
}

void Img_OnPaint(HWND hwnd)
{
    PAINTSTRUCT ps;
    RECT rc;
    BeginPaint(hwnd, &ps);
    GetClientRect(hwnd, &rc);
    EndPaint(hwnd, &ps);
}

void Img_OnHScroll(HWND hwnd, HWND hwndCtl, UINT code, int pos) {}
void Img_OnVScroll(HWND hwnd, HWND hwndCtl, UINT code, int pos) {}

LRESULT CALLBACK ImgWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        HANDLE_MSG(hwnd, WM_PAINT,      Img_OnPaint);
        HANDLE_MSG(hwnd, WM_VSCROLL,   Img_OnVScroll);
        HANDLE_MSG(hwnd, WM_HSCROLL,   Img_OnHScroll);
     
        default: return DefWindowProc(hwndImg, msg, wParam, lParam);
    }
}

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    INITCOMMONCONTROLSEX icc;
    WNDCLASSEX wc;
    HWND hwnd;
    MSG msg;

   hi=hInstance;

    icc.dwSize = sizeof(icc);
    icc.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&icc);

    wc.cbSize         = sizeof(WNDCLASSEX);
    wc.lpszClassName   = _T("MyClass");
    wc.lpfnWndProc      = MainWndProc;
    wc.style         = CS_VREDRAW | CS_HREDRAW;
    wc.hInstance      = hInstance;
    wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDR_ICO_MAIN));
    wc.hIconSm         = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_ICO_MAIN),
                     IMAGE_ICON, 16, 16, 0);
    wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground   = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName    = MAKEINTRESOURCE(IDR_MNU_MAIN);
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = 0;

    if (!RegisterClassEx(&wc)) return 1;

    wc.cbSize         = sizeof(WNDCLASSEX);
    wc.lpszClassName   = _T("MyImgClass");
    wc.lpfnWndProc      = ImgWndProc;
    wc.style         = CS_VREDRAW | CS_HREDRAW;
    wc.hInstance      = hi;
    wc.hIcon         = NULL;
    wc.hIconSm         = NULL;
    wc.hCursor         = LoadCursor(NULL, IDC_CROSS);
    wc.hbrBackground   = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName    = 0;
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = 0;

    if (!RegisterClassEx(&wc)) return 2;

    hwnd = CreateWindowEx(0L,
   _T("MyClass"),
        _T("digiMouse"),
        WS_OVERLAPPEDWINDOW,
        0,
        0,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hwnd) return 1;

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}
------------------------------------------------------------------------

But my child window is not createt. What I am doing wrong?

Nancy

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Child Window
« Reply #3 on: June 04, 2010, 03:53:29 PM »
hwndImg is NULL at creating phase. Use that hwnd instead.
default: return DefWindowProc(hwndImg, msg, wParam, lParam);  <----

Code: [Select]
LRESULT CALLBACK ImgWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        HANDLE_MSG(hwnd, WM_PAINT,      Img_OnPaint);
        HANDLE_MSG(hwnd, WM_VSCROLL,   Img_OnVScroll);
        HANDLE_MSG(hwnd, WM_HSCROLL,   Img_OnHScroll);
    
        default: return DefWindowProc(hwnd, msg, wParam, lParam);
    }
}

Code: [Select]
void Main_OnSize(HWND hwnd, UINT state, int cx, int cy)
{
RECT rcs;
SendMessage(hwndStatus, WM_SIZE, state, cx);
GetClientRect(hwndStatus, &rcs);
MoveWindow (hwndImg, 0, 0, cx, cy - rcs.bottom, TRUE);
   //MoveWindow (hwndStatus, 0, cy - 10, cx, cy, TRUE);
   WidthsStatus[0] = cx / 4;
   WidthsStatus[1] = -1;
    SendMessage (hwndStatus, SB_SETPARTS, 2, (LPARAM)WidthsStatus);
}
« Last Edit: June 04, 2010, 04:06:16 PM by timovjl »
May the source be with you

nancy

  • Guest
Re: Child Window
« Reply #4 on: June 04, 2010, 05:56:45 PM »
Thank you! That took me hours.

Nancy