📄 0511001.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
<link rel="stylesheet" type="text/css" href="../../vckbase.css">
</head>
<body>
<div align="justify">
<table border="0" width="100%" class="font" height="57">
<tr>
<td width="27%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
<font color="#800080">VC知识库(五)</font>
</td>
<td width="73%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
<font color="#800080">www.vckbase.com</font>
</td>
</tr>
<tr>
<td width="100%" height="4" class="header" valign="top" align="center" colspan="2">
<hr>
</td>
</tr>
<tr>
<td width="100%" height="17" class="header" valign="top" align="center" colspan="2">
<small>Visual C++中位图按钮的新颖设计</small>
</td>
</tr>
<tr>
<td width="100%" height="17" class="info" align="center" colspan="2">
<small>东北大学研究生院 赵江声</small>
</td>
</tr>
<tr>
<td width="100%" height="22" class="font" colspan="2">
<hr>
</td>
</tr>
<tr>
<td width="100%" height="5" class="font" colspan="2">
<small> 在众多的Windows软件中,位图按钮(BitmapButton)确实是一个相当重要的角色。与普通按钮相比,它以图形代替文字,形象直观,使画面更加活泼,使人机界面更加友善。笔者在用Visual
C++编制软件时,经常用到位图按钮。但在编制的过程中,发现它也还存在着美中不足的地方。
<p>一、问题的提出</p>
<p> 我们在上Internet时,经常使用各种浏览器,例如IE或Netscape,它在工具条上的位图按钮不能不给使用者留下深刻印象。当鼠标未触及时,正常状态下的位图按钮只是平面图形;一旦触及,它立即<font size="3">“</font><font face="宋体" lang="ZH-CN" size="3">浮现</font><font size="3">”</font><font face="宋体" lang="ZH-CN" size="3">出来,一方面提示用户,另一方面其色彩以及立体感的反差也给人以耳目一新的感觉。而现在众多用Visual
C++编写的软件中,位图按钮在触及前后并不改变,尽管它较普通的按钮已有很大的进步,但仍不免显得有些呆板,缺乏“动感”。本文试利用位按钮的“获得输入状态”(focused)与鼠标操作相结合加以解决。</p>
</font></small>
<p><small><font face="黑体" lang="ZH-CN">二、解决的方法及相关函数</font></small></p>
<small><font face="宋体" lang="ZH-CN" size="3">
<p> Microsoft基本类库(MFC)提供CBitmapButton类,其常用的成员函数有AutoLoad和LoadBitmaps。下面简略加以介绍:</p>
<p> 1. BOOL AutoLoad( UINT nID, CWnd* pParent );</p>
<p> 该函数将一个普通按钮与一个CBitmapButton类对象联系起来。
<ol start="2">
<li>BOOL LoadBitmaps( LPCSTR lpszBitmapResource, LPCSTR
lpszBitmapResourceSel = NULL, LPCSTR lpszBitmapResourceFocus = NULL,
LPCSTR lpszBitmapResourceDisabled = NULL );</li>
</ol>
<p> 位图按钮具有四种状态:正常(U)状态,按下(D)状态,获得输入(F)状态,
禁止(X)状态。其中F状态并不常用。</p>
<p>该函数将CBitmapButton类对象的上述四种状态与四个位图文件相对应,其中参数:</p>
<p>lpszBitmapResource是位图按钮正常状态(U)下的位图文件名。
</p>
<p> lpszBitmapResourceSel 是位图按钮按下状态(D)下的位图文件名。</p>
<p> lpszBitmapResourceFocus 是位图按钮获得输入状态(F)下的位图文件名。</p>
<p> lpszBitmapResourceDisabled 是位图按钮禁止状态(X)下的位图文件名。
<ol start="3">
<li>SetCapture( )和 ReleaseCapture( )</li>
</ol>
<p> SetCapture()对鼠标进行</font><font size="3">”</font><font face="宋体" lang="ZH-CN" size="3">捕捉</font><font size="3">”</font><font face="宋体" lang="ZH-CN" size="3">,即使鼠标光标已经移出该窗口,窗口仍能够接受到所有有关鼠标的消息。ReleaseCapture(
)则用来释放对鼠标的捕捉。</p>
<p> 当鼠标触及但尚未按下时,并不对应其中任何一种状态,可见VC++并不提供相应的机制,以解决本文所提出的问题。本程序利用鼠标在该位图按钮所在范围移动时,将其设置成获得输入状态,调入第三资源文件“F”位图文件加以实现。在鼠标触及该位图时,使之“浮现”出来,并将鼠标光标变成小手形状,再加以振铃以示提醒。同时由于使用SetCapture()函数,故需要对鼠标的各种操作进行设计。主要程序段如下。</p>
<p> </p>
</font></small>
<p><small><font face="黑体" lang="ZH-CN">三、主要程序段</font></small></p>
<small><font face="宋体" lang="ZH-CN" size="3">
<p> 笔者编写了一个小演示程序,简单模拟电器开关功能。它有两个位图按钮和一个用作状态显示屏的编辑控制框。当鼠标位于位图按钮之上时,状态显示屏均显示"鼠标的光标在位图按钮上"。电源开关开时,按“喇叭”位图按钮则会发声;否则“喇叭”位图按钮变灰,不起作用。</p>
<p>void CBmpDlg::OnMouseMove(UINT nFlags, CPoint point)</p>
<p>{</p>
<p>CWnd * pWnd;</p>
<p>HCURSOR MyCursor;</p>
<p>CRgn m_regionPower; //Region of POWER Button</p>
<p>CRgn m_regionPlay; //Region of PLAY Button</p>
<p>//建立位图按钮的矩形区域</p>
<p>m_regionPower.CreateEllipticRgnIndirect(CRect(27,56,72,92));</p>
<p>m_regionPlay.CreateEllipticRgnIndirect(CRect(78,56,120,92));</p>
<p>CString ShowString0 = "鼠标的光标在位图按钮上!!!";</p>
</font><font size="3">
<p>…</font><font face="宋体" lang="ZH-CN" size="3"> </font><font size="3">…</font><font face="宋体" lang="ZH-CN" size="3">
</font><font size="3">…</font><font face="宋体" lang="ZH-CN" size="3">
</font><font size="3">…</p>
</font><font face="宋体" lang="ZH-CN" size="3">
<p>m_bPlay = FALSE;</p>
<p>m_bPower= FALSE;</p>
<p>if (m_regionPower.PtInRegion(point)) //鼠标落在位图按钮之上</p>
<p>{</p>
<p>m_bPower = TRUE;</p>
<p>//将位图按钮设置成获得输入状态</p>
<p>pWnd= GetDlgItem(IDC_BUTTON_POWER);</p>
<p>pWnd->SetFocus();</p>
<p>SetCapture();</p>
<p>InputEdit().SetWindowText(ShowString0);</p>
<p>InputEdit().ShowWindow(TRUE);</p>
<p>//将鼠标光标变成小手形状</p>
<p>MyCursor = AfxGetApp()->LoadCursor(IDC_MYCURSOR);</p>
<p>::SetCursor(MyCursor);</p>
<p>VERIFY(m_Play.LoadBitmaps("PLAYU","PLAYD","PLAYF","PLAYX"));</p>
<p>m_bPressedPlay = FALSE;</p>
<p>return;</p>
<p>}</p>
<p>if (m_regionPlay.PtInRegion(point)) //鼠标落在位图按钮之上</p>
<p>{</p>
<p>if (m_bPowerOn) { //如果电源已被开启</p>
<p>m_bPlay = TRUE;</p>
<p>pWnd= GetDlgItem(IDC_BUTTON_PLAY);</p>
<p>pWnd->SetFocus();</p>
<p>SetCapture();</p>
<p>InputEdit().SetWindowText(ShowString0);</p>
<p>InputEdit().ShowWindow(TRUE);</p>
<p>MyCursor = AfxGetApp()->LoadCursor(IDC_MYCURSOR);</p>
<p>::SetCursor(MyCursor);</p>
<p>VERIFY(m_Power.LoadBitmaps("POWERONU","POWEROND","POWERONF"));</p>
<p>}</p>
<p>else { //如果电源已被关闭</p>
<p>ReleaseCapture();</p>
<p>InputEdit().SetWindowText(ShowString0+ShowString2);</p>
<p>InputEdit().ShowWindow(TRUE);</p>
<p>VERIFY(m_Power.LoadBitmaps("POWEROFU","POWEROFD","POWEROFF"));</p>
<p>}</p>
<p>m_bPressedPower= FALSE;</p>
<p>return;</p>
<p>}</p>
<p>//鼠标落在所有的位图按钮之外</p>
<p>ReleaseCapture();</p>
<p>InputEdit().SetWindowText(ShowString1);</p>
<p>InputEdit().ShowWindow(TRUE);</p>
<p>pWnd= GetDlgItem(IDOK);</p>
<p>pWnd->SetFocus();</p>
<p>VERIFY(m_Play.LoadBitmaps("PLAYU","PLAYD","PLAYF","PLAYX"));</p>
<p>if (m_bPowerOn)</p>
<p>VERIFY(m_Power.LoadBitmaps("POWERONU","POWEROND","POWERONF"));</p>
<p>else</p>
<p>VERIFY(m_Power.LoadBitmaps("POWEROFU","POWEROFD","POWEROFF"));</p>
<p>m_bPressedPlay = FALSE;</p>
<p>m_bPressedPower= FALSE;</p>
<p>CDialog::OnMouseMove(nFlags, point);</p>
<p>}</p>
<p>void CBmpDlg::OnLButtonDown(UINT nFlags, CPoint point)</p>
<p>{</p>
<p>CWnd *pWnd;</p>
<p>if (m_bPlay && m_bPowerOn) {</p>
<p>// Change Focus so as to Change the bitmap of m_Play</p>
<p>pWnd= GetDlgItem(IDOK);</p>
<p>pWnd->SetFocus();</p>
<p>VERIFY(m_Play.LoadBitmaps("PLAYD"));</p>
<p>m_Play.UpdateWindow();</p>
<p>m_Play.Invalidate(TRUE);</p>
<p>OnButtonPlay();</p>
<p>m_bPressedPlay = TRUE;</p>
<p>}</p>
<p>if (m_bPower== TRUE) {</p>
<p>// Change Focus so as to Change the bitmap of m_Power</p>
<p>pWnd= GetDlgItem(IDOK);</p>
<p>pWnd->SetFocus();</p>
<p>if (m_bPowerOn)</p>
<p>VERIFY(m_Power.LoadBitmaps("POWEROND"));</p>
<p>else</p>
<p>VERIFY(m_Power.LoadBitmaps("POWEROFD"));</p>
<p>m_Power.UpdateWindow();</p>
<p>m_Power.Invalidate(TRUE);</p>
<p>OnButtonPower();</p>
<p>m_bPressedPower = TRUE;</p>
<p>}</p>
<p>CDialog::OnLButtonDown(nFlags, point);</p>
<p>}</p>
<p> </p>
<p>void CBmpDlg::OnLButtonUp(UINT nFlags, CPoint point)</p>
<p>{</p>
<p>CWnd * pWnd;</p>
<p>if (m_bPressedPlay == TRUE) {</p>
<p>pWnd= GetDlgItem(IDOK);</p>
<p>pWnd->SetFocus();</p>
<p>VERIFY(m_Play.LoadBitmaps("PLAYF"));</p>
<p>m_Play.UpdateWindow();</p>
<p>m_Play.Invalidate(TRUE);</p>
<p>m_bPressedPlay = FALSE;</p>
<p>}</p>
<p>if (m_bPressedPower == TRUE) {</p>
<p>pWnd= GetDlgItem(IDOK);</p>
<p>pWnd->SetFocus();</p>
<p>if (m_bPowerOn)</p>
<p>VERIFY(m_Power.LoadBitmaps("POWERONF"));</p>
<p>else</p>
<p>VERIFY(m_Power.LoadBitmaps("POWEROFF"));</p>
<p>m_Power.UpdateWindow();</p>
<p>m_Power.Invalidate(TRUE);</p>
<p>m_bPressedPower = FALSE;</p>
<p>}</p>
<p>CDialog::OnLButtonUp(nFlags, point);</p>
<p>}</font></small>
</td>
</tr>
<tr>
<td width="100%" height="12" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="6" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="8" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="17" class="font" colspan="2"></td>
</tr>
</table>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -