我创建了一个Dialog,如下:
hwnd=CreateDialog(hInstance,szAppName,NULL,NULL) ;
ShowWindow(hwnd, iCmdShow);
UpdateWindow (hwnd);
可是我如何才能指定该Dialog在屏幕中的位置,例如居中等等

解决方案 »

  1.   

    void CWnd::CenterWindow(CWnd* pAlternateOwner)
    {
    ASSERT(::IsWindow(m_hWnd)); // determine owner window to center against
    DWORD dwStyle = GetStyle();
    HWND hWndCenter = pAlternateOwner->GetSafeHwnd();
    if (pAlternateOwner == NULL)
    {
    if (dwStyle & WS_CHILD)
    hWndCenter = ::GetParent(m_hWnd);
    else
    hWndCenter = ::GetWindow(m_hWnd, GW_OWNER);
    if (hWndCenter != NULL)
    {
    // let parent determine alternate center window
    HWND hWndTemp =
    (HWND)::SendMessage(hWndCenter, WM_QUERYCENTERWND, 0, 0);
    if (hWndTemp != NULL)
    hWndCenter = hWndTemp;
    }
    } // get coordinates of the window relative to its parent
    CRect rcDlg;
    GetWindowRect(&rcDlg);
    CRect rcArea;
    CRect rcCenter;
    HWND hWndParent;
    if (!(dwStyle & WS_CHILD))
    {
    // don't center against invisible or minimized windows
    if (hWndCenter != NULL)
    {
    DWORD dwStyle = ::GetWindowLong(hWndCenter, GWL_STYLE);
    if (!(dwStyle & WS_VISIBLE) || (dwStyle & WS_MINIMIZE))
    hWndCenter = NULL;
    }  MONITORINFO mi;
    mi.cbSize = sizeof(mi); // center within appropriate monitor coordinates
    if (hWndCenter == NULL)
    {
    HWND hwDefault = AfxGetMainWnd()->GetSafeHwnd(); GetMonitorInfo(
    MonitorFromWindow(hwDefault, MONITOR_DEFAULTTOPRIMARY), &mi);
    rcCenter = mi.rcWork;
    rcArea = mi.rcWork;
    }
    else
    {
    ::GetWindowRect(hWndCenter, &rcCenter);
    GetMonitorInfo(
    MonitorFromWindow(hWndCenter, MONITOR_DEFAULTTONEAREST), &mi);
    rcArea = mi.rcWork;
    }
    }
    else
    {
    // center within parent client coordinates
    hWndParent = ::GetParent(m_hWnd);
    ASSERT(::IsWindow(hWndParent)); ::GetClientRect(hWndParent, &rcArea);
    ASSERT(::IsWindow(hWndCenter));
    ::GetClientRect(hWndCenter, &rcCenter);
    ::MapWindowPoints(hWndCenter, hWndParent, (POINT*)&rcCenter, 2);
    } // find dialog's upper left based on rcCenter
    int xLeft = (rcCenter.left + rcCenter.right) / 2 - rcDlg.Width() / 2;
    int yTop = (rcCenter.top + rcCenter.bottom) / 2 - rcDlg.Height() / 2; // if the dialog is outside the screen, move it inside
    if (xLeft < rcArea.left)
    xLeft = rcArea.left;
    else if (xLeft + rcDlg.Width() > rcArea.right)
    xLeft = rcArea.right - rcDlg.Width(); if (yTop < rcArea.top)
    yTop = rcArea.top;
    else if (yTop + rcDlg.Height() > rcArea.bottom)
    yTop = rcArea.bottom - rcDlg.Height(); // map screen coordinates to child coordinates
    SetWindowPos(NULL, xLeft, yTop, -1, -1,
    SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
    }
      

  2.   

    可以使用SetWindowPos来设置窗口大小
      

  3.   

    用SetWindowPos 设置位置