locked
dynamic center button RRS feed

  • Question

  • Hello,

    I'm using in my application (on Win. Mobile 5) a control button and i would like to align it to the centrer of the dialog but i couldn't find any
    command (message) that i can use and i'm not using MFC, does anyone know how to do it?
    Thanks.
    Sunday, July 5, 2009 3:37 PM

Answers

  • Sorry, you said *not* using MFC, but for some reason I read it as "using MFC". I actually had to try to remember the MFC methods...

    You don't need to override button painting. Simply reposition it in response to WM_SIZE in your dialog's WindowProc (ServiceCB)

    MoveWindow(servBtn, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, TRUE);
    Alex Feinman. MVP .NET Compact Framework
    Tuesday, July 7, 2009 6:21 AM

All replies

  • In your parent dialog process WM_SIZE message. Use it to reposition your button manually:

    MyDlg::OnSize( UINT nType, int cx, int cy )
    {
        CRect rcButton;
        myButton.GetWindowRect(rcButton);
        rcButton.OffsetRect((cx - rcButton.Width()) / 2 - rcButton.Left, 0);
        rcButton.SetWindowPos(NULL, rcButton.Left, rcButton.Top, rcButton.Width(), rcButton.Height(), SWP_NOZORDER);
    }
    Alex Feinman. MVP .NET Compact Framework
    Sunday, July 5, 2009 6:40 PM
  • Hello Alex,

    In order to use your solution i'll have use MFC and currently i'm not using it.
    The API which i use to create my dialog is very simple:

    CreateDialog(g_hInst, 
    	    MAKEINTRESOURCE(IDD_SERV), 
    	    g_hwndBody, 
    	    (DLGPROC) ServiceCB);

    So, correct me if i'm wrong, but i'll have to do something like that:

    HWND servBtn = GetDlgItem(hDlg, IDC_BUTTON_SERVICE);
    GetClientRect(servBtn, &rt);
    //do some calculations to center the button
    //...
    DrawFrameControl(hdc, &rt, DFC_BUTTON, DFCS_BUTTONPUSH);

    Am i right, or is there another way yo do it?

    Thanks.

    Monday, July 6, 2009 6:38 AM
  • Sorry, you said *not* using MFC, but for some reason I read it as "using MFC". I actually had to try to remember the MFC methods...

    You don't need to override button painting. Simply reposition it in response to WM_SIZE in your dialog's WindowProc (ServiceCB)

    MoveWindow(servBtn, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, TRUE);
    Alex Feinman. MVP .NET Compact Framework
    Tuesday, July 7, 2009 6:21 AM
  • Hi,

    I just tried to use the MoveWindow function and it seems to do the work but from some reason i keep getting the rt.top value as zero
    and not the real value of the control position in the window (I created a window and placed the main controls in it and i need to replace this control
    due to alignment issues), do you know why?

    Thansk for the quick reply!
    Tuesday, July 7, 2009 6:37 AM