VC++编程常见问题解答

来源:互联网 发布:idg联合校招 知乎 编辑:程序博客网 时间:2024/05/16 12:06
31、如何在对话框中显示一个位图 
  这要归功于Win 32先进的静态控件和Microsoft的资源编辑器, 在对话框中
显示位图是很容易的, 只需将图形控件拖到对话中并选择适当属性即可,用户也
可以显示图标、位图以及增强型元文件。 

32、如何改变对话或窗体视窗的背景颜色 
调用CWinApp : : SetDialogBkColor可以改变所有应用程序的背景颜色。第
一个参数指定了背景颜色,第二个参数指定了文本颜色。下例将应用程序对话设
置为蓝色背景和黄色文本。 
BOOL CSampleApp : : InitInstance ( ) 

    … 
  //use blue dialog with yellow text . 
  SetDialogBkColor (RGB (0, 0, 255 ), RGB ( 255 , 255 , 0 ) ) ; 
    … 

   需要重画对话(或对话的子控件)时,Windows向对话发送消息WM_CTLCOLOR
,通常用户可以让Windows选择绘画背景的刷子,也可重置该消息指定刷子。下例
说明了创建一个红色背景对话的步骤。 
  首先,给对话基类增加一人成员变量CBursh : 
class CMyFormView : public CFormView 

  … 
  private : 
    CBrush m_ brush ; // background brush 
  … 
} ; 
  其次, 在类的构造函数中将刷子初始化为所需要的背景颜色。 
CMyFormView : : CMyFormView ( ) 

  // Initialize background brush . 
  m_brush .CreateSolidBrush (RGB ( 0, 0, 255 ) ) 

   最后,使用ClassWizard处理WM_CTLCOLOR消息并返回一个用来绘画对话背景
的刷子句柄。注意:由于当重画对话控件时也要调用该函数,所以要检测nCtlColor参量。 
HBRUSH CMyFormView : : OnCtlColor (CDC* pDC , CWnd*pWnd , UINT nCtlColor ) 


  // Determine if drawing a dialog box . If we are , return +handle to 
  //our own background brush . Otherwise let windows handle it . 
  if (nCtlColor = = CTLCOLOR _ DLG ) 
    return (HBRUSH) m_brush .GetSafeHandle ( ) ; 

  return CFormView : : OnCtlColor (pDC, pWnd , nCtlColor ); 

33、如何获取一个对话控件的指针 
  有两种方法。其一,调用CWnd: : GetDlgItem,获取一个CWnd*指针调用成员
函数。下例调用GetDlgItem,将返回值传给一个CSpinButtonCtrl*以便调用 
CSpinButtonCtrl : : SetPos 函数: 
BOOL CSampleDialog : : OnInitDialog ( ) 

  CDialog : : OnInitDialog ( ) ; 
  //Get pointer to spin button . 
  CSpinButtonCtrl * pSpin - ( CSpinButtonCtrl *) GetDlgItem (IDC_SPIN) ; 
  ASSERT _ VALID (pSpin) ; 
  //Set spin button's default position . 
  pSpin —> SetPos (10) ; 
  return TRUE ; 

  其二, 可以使用ClassWizard将控件和成员变量联系起来。在ClassWizard中
简单地选择Member Variables标签,然后选择Add Variable …按钮。如果在对话
资源编辑器中,按下Ctrl键并双击控件即可转到Add Member Variable对话。 
34、如何禁止和使能控件 

  控件也是窗口,所以可以调用CWnd : : EnableWindow使能和禁止控件。 
//Disable button controls . 
m_wndOK.EnableWindow (FALSE ) ; 
m_wndApply.EnableWindow (FALSE ) ; 

35、如何改变控件的字体 
  由于控件是也是窗口,用户可以调用CWnd: : SetFont指定新字体。该函数用
一个Cfont指针,要保证在控件撤消之前不能撤消字体对象。下例将下压按钮的字
体改为8点Arial字体: 
//Declare font object in class declaration (.H file ). 
private : 
  Cfont m_font ; 
// Set font in class implementation (.Cpp file ). Note m_wndButton is a 
//member variable added by ClassWizard.DDX routines hook the member 
//variable to a dialog button contrlo. 
BOOL CSampleDialog : : OnInitDialog ( ) 

  … 
  //Create an 8-point Arial font 
  m_font . CreateFont (MulDiv (8 , -pDC—> GetDeviceCaps (LOGPIXELSY) , 72). 
    0 , 0 , 0 , FW_NORMAL , 0 , 0, 0, ANSI_CHARSER, OUT_STROKE_PRECIS , 
    CLIP_STROKE _PRECIS , DRAFT _QUALITY VARIABLE_PITCH |FF_SWISS, _T ("Arial") ); 

  //Set font for push button . 
  m_wndButton . SetFont (&m _font ); 
  … 


36、如何在OLE控件中使用OLE_COLOR数据类型 
  诸如COleControl : : GetFortColor和COleControl : : GetBackColor等函
数返回OLE _COLOR数据类型的颜色,而GDI对象诸如笔和刷子使用的是COLORREF数
据类型,调用COleControl : : TranslateColor可以很容易地将OLE_COLOR类型改
为COLORREF类型。下例创建了一个当前背景颜色的刷子: 
void CSampleControl : : OnDraw (CDC* pdc 
  const Crect& rcBounds , const Crect& rcInvalid ) 

  //Create a brush of the cuttent background color . 
  CBrush brushBack (TranslateColor (GetBackColor ( ) ) ); 

  //Paint the background using the current background color . 
  pdc—> FilllRect (rcBounds , &brushBack) ; 

  //other drawign commands 

  … 


37、在不使用通用文件打开对话的情况下如何显示一个文件列表 
  调用CWnd: : DlgDirList或者CWnd: : DlgDirListComboBox, Windows 将自
动地向列表框或组合框填充可用的驱动器名或者指定目录中的文件,下例将
Windows目录中的文件填充在组合框中: 
BOOL CSampleDig : : OnInitDialog ( ) 

  CDialog : : OnInitDialog ( ) 
  TCHAR szPath [MAX_PATH] = {"c://windows"} ; 
  int nReslt = DlgDirListComboBox (szPath , IDC_COMBO , IDC_CURIDIR, 
    DDL_READWRITE |DDL_READONLY|DDL_HIDDEN| 
    DDL_SYSTEM|DDL_ARCHIVE ) ; 
  return TRUE ; 


38、为什么旋转按钮控件看起来倒转 
  需要调用CSpinCtrl : : SetRange 设置旋转按钮控件的范围,旋转按钮控件
的缺省上限为0,缺省下限为100,这意味着增加时旋转按控件的值由100变为0。
下例将旋转按钮控件的范围设置为0到100: 
BOOL CAboutDlg : : OnInitDialog ( ) 

  CDialog : : OnInitDialog ( ) 

  //set the lower and upper limit of the spin button 
  m_wndSpin . SetRange ( 0 ,100 ) ; 

  return TRUE ; 

  Visual C++ 4.0 Print对话中的Copise旋转按钮控件也有同样的问题:按下
Up按钮时拷贝的数目减少,而按下Down 按钮时拷贝的数目增加。 

39为什么旋转按钮控件不能自动地更新它下面的编辑控件 
  如果使用旋转按钮的autu buddy特性, 则必须保证在对话的标记顺序中
buddy窗口优先于旋转按钮控件。从Layout菜单中选择Tab Order菜单项(或者按
下Crtl+D)可以设置对话的标签顺序。 

40、如何用位图显示下压按钮 
  Windows 95按钮有几处新的创建风格,尤其是BS_BITMAP和BS_ICON,要想具
有位图按钮,创建按钮和调用CButton : : SetBitmap或CButton : : SetIcon时
要指定BS_BITMAP或BS_ICON风格。 
  首先,设置按钮的图标属性。 
  然后,当对话初始化时调用CButton: : SetIcon。注意:下例用图标代替位
图,使用位图时要小心,因为不知道背景所有的颜色——并非每个人都使用浅灰色。 
BOOL CSampleDlg : : OnInitDialog ( ) 

CDialog : : OnInitDialog ( ) ; 

//set the images for the push buttons . 
BOOL CSampleDlg : : OnInitDialog ( ) 

CDialog : : OnInitDialog ( ) ; 

//set the images for the push buttons . 
m_wndButton1.SetIcon (AfxGetApp ( ) —> LoadIcon (IDI _ IPTION1) ) 
m_wndButton2.SetIcon (AfxGetApp ( ) —> LoadIcon (IDI _ IPTION2) ) 
m_wndButton3.SetIcon (AfxGetApp ( ) —> LoadIcon (IDI _ IPTION3) ) 

  return TRUE ; 
} 


原创粉丝点击