请问如何用win32api实现MFC里的"CenterWindow"函数的功能?

finalcrasher 2006-02-12 10:40:49
CenterWindow的功能就是使窗体居中显示,因为我最近用win32sdk编程,要用到这个功能。我查看MFC里CenterWindow的源代码,如下:

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;
}
}
}

上述代码用win32api改写时基本无问题,但"WM_QUERYCENTERWND"这个是MFC里定义的,win32sdk里找不到,所以想请教高手该如何解决。
...全文
474 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
finalcrasher 2006-02-13
  • 打赏
  • 举报
回复
ok,谢谢各位的帮助!
palmax 2006-02-12
  • 打赏
  • 举报
回复
自己用MoveWindow()就可以实现了,先获得屏幕的分辨率,然后获得自己窗口的尺寸,然后二者的x和y分别做差除2,然后用MoveWindow()把自己的窗口移动到那个坐标不就OK了?
syy64 2006-02-12
  • 打赏
  • 举报
回复
可以根据它的功能,自定义一个消息。
DentistryDoctor 2006-02-12
  • 打赏
  • 举报
回复
#define WM_QUERYCENTERWND 0x036B // lParam = HWND to use as centering parent
布学无数 2006-02-12
  • 打赏
  • 举报
回复
#define CW_SCREEN 1
#define CW_WORKAREA 3

BOOL WINAPI CenterWindow(HWND hWnd,
DWORD dwFlags)
{
RECT rtWindow = {0};
RECT rtContainer = {0};

GetWindowRect(hWnd,&rtWindow);
rtWindow.right -= rtWindow.left;
rtWindow.bottom -= rtWindow.top;

switch(dwFlags)
{
case CW_SCREEN:
rtContainer.right = GetSystemMetrics(SM_CXSCREEN);
rtContainer.bottom = GetSystemMetrics(SM_CYSCREEN);
break;
case CW_WORKAREA:
SystemParametersInfo(SPI_GETWORKAREA,0,&rtContainer,0);
break;
}

return SetWindowPos(hWnd,NULL,(rtContainer.right - rtWindow.right) / 2,(rtContainer.bottom - rtWindow.bottom) / 2,0,0,SWP_NOSIZE);
}
vc++利用ADOX创建数据库 // ADOXCreateDatabaseDlg.cpp : implementation file // #include "stdafx.h" #include "ADOXCreateDatabase.h" #include "ADOXCreateDatabaseDlg.h" #include "Shlwapi.h" #pragma comment(lib,"shlwapi.lib") // Download by http://www.codefans.net #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CADOXCreateDatabaseDlg dialog CADOXCreateDatabaseDlg::CADOXCreateDatabaseDlg(CWnd* pParent /*=NULL*/) : CDialog(CADOXCreateDatabaseDlg::IDD, pParent) { //{{AFX_DATA_INIT(CADOXCreateDatabaseDlg) m_dbName = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CADOXCreateDatabaseDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CADOXCreateDatabaseDlg) DDX_Text(pDX, IDC_DBNAME, m_dbName); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CADOXCreateDatabaseDlg, CDialog) //{{AFX_MSG_MAP(CADOXCreateDatabaseDlg) ON_WM_SYSCOMM

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧