| 网站首页 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛 |
 
| 技术教程首页 | 开发语言 | WEB开发 | .NET技术 | 数据库 | 操作系统 | 网页制作 |
 
 
您现在的位置: 编程中国 >> 技术教程 >> 开发语言 >> VC++ >> VC技术资料 >> 正文
  ►  积累的VC编程小技巧之对话框
积累的VC编程小技巧之对话框
作者:huzunbo    阅读人次:……    文章来源:VC在线    发布时间:2007-9-3    网友评论()条
 

6.对话框上建立View的方法

OnInitDialog()
{
  CDialog:;OnInitDialog();

CRect rectWindows;
GetWinodwRect(&rectWindows);
CRuntimeClass *pViewClass=RUNTIME_CLASS(CXXXView);
CCreateContext *pContext=new CCreateContext;
pContext->m_pCurrentDoc=NULL;
pContext->m_pCurrentFrame=NULL;
pContext->m_pLastView=NULL;
pContext->m_pNewDocTemplate=NULL;
pContext->m_pNewViewClass=pViewClass;

CWnd *pWnd=DYNAMIC_DOWNCAST(CWnd,pviewClass->CreateObject());
pWnd->Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,CRect(0,0,0,0),this,pContext);
delete pContext;
CXXXView *pView=DYUNAMIC_DOWNCAST(CXXXView,pWnd);
...............
}

 

7.模态对话框初始显示位置的控制

正确的方法是在OnInitDialog中添加MoveWindow,如:
     MoveWindow(0, 1, 300, 200);
    需要注意的是前两个参数不能都为0。如果你确实希望把窗口放在(0, 0)处,可以在对话框设计窗口的属性中选中Absolute Align,然后再加入
     MoveWindow(0, 0, 300, 200);
    为什么会是这样?你看了MFC的源程序就会明白。原来MFC在调用你的OnInitDialog之后,会调用CDialog::CheckAutoCenter()(在dlgcore.cpp中)检查是否需要将窗口居中,你看了这个函数后就明白为什么需要上面那么做了。

 

8.动态修改对话框的大小

  [问题提出]
    关于如何动态改变对话框的大小,我做了个Demo,大家看看.

  [程序实现]
     //本函数使用方法:
     //第一个参数:如果是TRUE表示显示扩展的对话框,如果是FALSE,表示缩小对话框。
     //第二个参数:表示本对话框的HWND
     //第三个参数:表示缩小后大小的控件的ID

    void COptionDlg::ExpandBox(BOOL fExpand, HWND hwnd, int nIDDefaultBox)
    {
         CWnd *pWndBox=GetDlgItem(nIDDefaultBox);
         RECT rcDefaultBox,rcChild,rcIntersection,rcWnd;
         pWndBox->GetWindowRect(&rcDefaultBox);
         HWND hwndChild = ::GetTopWindow(hwnd);
         for (; hwndChild != NULL; hwndChild = ::GetNextWindow(hwndChild,GW_HWNDNEXT))
         {
                  ::GetWindowRect(hwndChild, &rcChild);
                  if (!IntersectRect(&rcIntersection, &rcChild, &rcDefaultBox))
                           ::EnableWindow(hwndChild, fExpand);
         }
         ::GetWindowRect(hwnd, &rcWnd);
         if (GetWindowLong(hwnd, GWL_USERDATA) == 0)
         {
                  SetWindowLong(hwnd, GWL_USERDATA,
                           MAKELONG(rcWnd.right - rcWnd.left,
                           rcWnd.bottom - rcWnd.top));
                  ::ShowWindow(pWndBox->m_hWnd, SW_HIDE);
         }
         ::SetWindowPos(hwnd, NULL, 0, 0,
                  rcDefaultBox.right - rcWnd.left,
                  rcDefaultBox.bottom - rcWnd.top,
                  SWP_NOZORDER | SWP_NOMOVE);
         if(fExpand)
         {
                  DWORD dwDims = GetWindowLong(hwnd, GWL_USERDATA);
                  ::SetWindowPos(hwnd, NULL, 0, 0,
                           LOWORD(dwDims), HIWORD(dwDims), SWP_NOZORDER | SWP_NOMOVE);
                  ::SendMessage(hwnd, DM_REPOSITION, 0, 0);
         }
    }

 

9.隐藏对话框窗口(窗口没有焦点时)

在程序启动时InitDialog中使用SetWindowPos将窗体设置到屏幕以外
然后再隐藏
1.OnInitDialog()函数里设置定时器:(WINDOWS   API里面响应消息WM_INITDIALOG  
  SetTimer(1,   1,   NULL);  
  2.
添加处理WM_TIMER的消息处理函数OnTimer,添加代码:    
  if(nIDEvent   ==   1)    
  {  
      DeleteTimer(1);  
      ShowWindow(SW_HIDE);    
  } 

 

10.如何实现点击对话框外的地方使对话框到主窗口的后面

将桌面做为父窗口
pMDlg = new CMDlg;
pMDlg->Create(IDD_M_DIALOG,CWnd::GetDesktopWindow()/*
设置父窗口 */);
pMDlg->ShowWindow(SW_SHOW);
然后在任务栏里隐藏对话框程序
如何让对话框应用程序在在任务栏上不出现,并且不隐藏窗口。
[解决方法]
   
把对话框的扩展属性修改成为WS_EX_TOOLWINDOW
[程序实现]
   
把对话框的属性设置成为toolwindow,然后在需要的地方执行本代码。
   DWORD Style = ::GetWindowLong(AfxGetMainWnd()->m_hWnd,GWL_EXSTYLE);
   Style = WS_EX_TOOLWINDOW ;
   AfxGetMainWnd()->ShowWindow(FALSE);
   ::SetWindowLong(AfxGetMainWnd()->m_hWnd,GWL_EXSTYLE,Style);
   AfxGetMainWnd()->ShowWindow(TRUE);

 

11.怎么让无模式对话框显示在主窗口后面

要解决这个问题的关键在于CDialogCreate并不能建立一个无属主的窗口.必须用另外方式建窗口.  
   
  比如你的对话框类叫CDlgNoOwner,CMainFrame中加一个CDlgNoOwner类的成员变量,  
  弹出这个对话框的消息处理函数为  
   
  void   CMainFrame::OnNoowner()    
  {  

CDlgNoOwner   *m_dlgTest=new   CDlgNoOwner(this);  
      HWND   hwndDlg=::CreateDialog( AfxGetInstanceHandle(),MAKEINTRESOURCE(                              CDlgNoOwner::IDD),NULL/*owner*/,NULL/*dlgproc*/);
      //
注意此处DLGPROCNULL,并不要紧,因为接下要subclass  
      m_dlgTest->SubclassWindow   (hwndDlg);//
挂接到成员变量!  
      m_dlgTest->ShowWindow   (SW_SHOW);  
      //
这时可以看到一个"自由"的对话框弹出,和你的主窗口是平起平坐的.  
  }  
   
 
当然不要忘了在对话框关闭时DestroyWindow()..那都是在对话框类中的标准处理了.

 

12.如何得到屏幕的真实尺寸(以对话框为例)

[问题提出]
我的屏幕是1024*800,如何得到屏幕的真实大小,我用GetSystemMetrics(SM_CYFULLSCREEN)得到的高度总是小于800
[问题解答]
GetSystemMetrics(SM_CYFULLSCREEN)得到的只是屏幕用户区的大小。要得到屏幕的真实大小需要使用
GetDeviceCaps函数,API函数原型是这样的:

int GetDeviceCaps(
  HDC hdc,     // handle to DC
  int nIndex   // index of capability
);
///得到屏幕尺寸的代码如下
void CMyDlg::OnPaint()
{
   CPaintDC dc(this);
   int cx = ::GetDeviceCaps(dc.m_hDC,HORZRES);///得到宽度
   int cy = ::GetDeviceCaps(dc.m_hDC,VERTRES);///得到高度
   CDialog::OnPaint();

}

 

13.如何在对话框中加入工具条

   OnInitDialog 中加入下面代码:

  BOOL CYourDlg::OnInitDialog()

  {

       CDialog::OnInitDialog();  

 

       // Create the toolbar. To understand the meaning of the styles used, you

       // can take a look at the MSDN for the Create function of the CToolBar class.

       ToolBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_TOOLTIPS |CBRS_FLYBY | CBRS_BORDER_BOTTOM);

 

      // I have assumed that you have named your toolbar's resource as IDR_TOOLBAR1.

      // If you have given it a different name, change the line below to accomodate

      // that by changing the parameter for the LoadToolBar function.

      ToolBar.LoadToolBar(IDR_TOOLBAR1);

 

      CRect rcClientStart;

      CRect rcClientNow;

      GetClientRect(rcClientStart);

 

      // To reposition and resize the control bar

      RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,0, reposQuery, rcClientNow);

     CPoint ptOffset(rcClientNow.left - rcClientStart.left,rcClientNow.top-rcClientStart.top);

 

     CRect rcChild;

     CWnd* pwndChild = GetWindow(GW_CHILD);

 

     while (pwndChild)

     {

       pwndChild->GetWindowRect(rcChild);

       ScreenToClient(rcChild);

       rcChild.OffsetRect(ptOffset);

       pwndChild->MoveWindow(rcChild, FALSE);

       pwndChild = pwndChild->GetNextWindow();

     }

 

     CRect rcWindow;

     GetWindowRect(rcWindow);

     rcWindow.right += rcClientStart.Width() - rcClientNow.Width();

     rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();

     MoveWindow(rcWindow, FALSE);

 

     // And position the control bars

     RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);

 

     return TRUE;  // return TRUE  unless you set the focus to a control

  }

 

14.如何改变对话框的形状

可用下面一些涵数:

 CreatePolygonRgn
     CreateRectRgn
     CreateRoundRectRgn
.

  CRgn m_rgn;  // Put this in your dialog's header file. i.e. a member variable
 
  // This Gets the size of the Dialog: This piece of code is to be placed in the
  // OnInitDialog Function of your dialog.
 
  CRect rcDialog
  GetClientRect(rcDialog);
 
  // The following code Creates the area and assigns it to your Dialog
  m_rgn.CreateEllipticRgn(0, 0, rcDialog.Width(), rcDialogHeight());
  SetWindowRgn(GetSafeHwnd(), (HRGN) m_rgn, TRUE);

 

15.如何在对话框中加入状态条

     定义 CStatusBar 变量:

  CStatusBar m_StatusBar;

     定义状态条指定状态:

  static UINT BASED_CODE indicators[] =

  {

     ID_INDICATOR_CAPS,

     ID_INDICATOR_NUM

  };

      OnInitDialog 中加入下面代码:

 

  m_StatusBar.CreateEx(this,SBT_TOOLTIPS,WS_CHILD|WS_VISIBLE|CBRS_BOTTOM,AFX_IDW_STATUS_BAR);

 

  // Set the indicators namely caps and nums lock status

  m_StatusBar.SetIndicators(indicators,sizeof(indicators)/sizeof(UINT));

 

  CRect rect;

  GetClientRect(&rect);

             

  m_StatusBar.SetPaneInfo(0,ID_INDICATOR_CAPS,SBPS_NORMAL,rect.Width()/2);

  m_StatusBar.SetPaneInfo(1,ID_INDICATOR_NUM,SBPS_STRETCH ,rect.Width()/2);

 

  RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,ID_INDICATOR_NUM);

  m_StatusBar.GetStatusBarCtrl().SetBkColor(RGB(180,180,180));

 

16.如何实现非客户区移动

可用下面二种方法:

  // Handler for WM_LBUTTONDOWN message

 

  void CYourDialog::OnLButtonDown(UINT nFlags, CPoint point)

  {

     CDialog::OnLButtonDown(nFlags, point);

     PostMessage( WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( point.x, point.y));

  }

 

  // Handler for WM_NCHITTEST message

 

  LONG CYourDialog::OnNcHitTest( UINT uParam, LONG lParam )

  { 

     int xPos = LOWORD(lParam);

     int yPos = HIWORD(lParam);

     UINT nHitTest = CDialog::OnNcHitTest(CSize(xPos, yPos));

     return (nHitTest == HTCLIENT) ? HTCAPTION : nHitTest;

  }

 

17.如何使对话框初始为最小化状态

OnInitDialog 中加入下面代码:

SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, NULL);

 

18.如何限定对话框大小范围

WM_SIZING中加入下面代码:

   void CYourDialog::OnSizing(UINT fwSide, LPRECT pRect)

  {

     if(pRect->right - pRect->left <=200)

       pRect->right = pRect->left + 200;

      

     if(pRect->bottom - pRect->top <=200)

       pRect->bottom = pRect->top + 200;

 

     CDialog::OnSizing(fwSide, pRect);

  }

上一页  [1] [2] 

 

 
文章录入:编辑01    责任编辑:编辑01 
  • 上一篇文章:

  • 下一篇文章:

  •  
    相关文章
    原创地带
    24小时热门帖子