`
gaofen100
  • 浏览: 1189728 次
文章分类
社区版块
存档分类
最新评论

VC++问题集4

 
阅读更多

31.如何实现禁用标题栏上的"最大化"、"最小化"和"关闭"按钮
禁用这些按钮涉及到的函数有:
LONG GetWindowLong:获得有关窗口的信息.
Long SetWindowLong:改变指定窗口的属性.
CMenu * GetSystemMenu:允许应用程序为复制或修改而访问窗口菜单.
UINT GetMenuItemID:返回位于菜单中指定位置处的项目的菜单ID。
UINT EnableMenuItem:使指定项目的菜单项有效,无效或变灰。
如下代码:
//禁用最小化按钮
void CMainFrame::OnMenudismin()
{
// TODO: Add your command handler code here
//获得窗口风格
Style = ::GetWindowLong(m_hWnd,GWL_STYLE);
//设置新的风格
Style &= ~(WS_MINIMIZEBOX);
::SetWindowLong(m_hWnd,GWL_STYLE,Style);
GetWindowRect(&Rect);
//重画窗口边框
::SetWindowPos(m_hWnd,HWND_TOP,Rect.left,Rect.top,Rect.Width(),
Rect.Height(),SWP_DRAWFRAME);
}
//禁用最大化按钮
void CMainFrame::OnMenudismax()
{
// TODO: Add your command handler code here
//获得窗口风格
Style = ::GetWindowLong(m_hWnd,GWL_STYLE);
//设置新的风格
Style &= ~(WS_MAXIMIZEBOX);
::SetWindowLong(m_hWnd,GWL_STYLE,Style);
GetWindowRect(&Rect);
//重画窗口边框
::SetWindowPos(m_hWnd,HWND_TOP,Rect.left,Rect.top,Rect.Width(),
Rect.Height(),SWP_DRAWFRAME);
}
//禁用关闭按钮
void CMainFrame::OnMenudisclose()
{
// TODO: Add your command handler code here
//获得系统菜单
CMenu *pMenu = GetSystemMenu(false);
//获得关闭按钮ID
UINT ID = pMenu->GetMenuItemID(pMenu->GetMenuItemCount()-1);
//使关闭按钮无效
pMenu->EnableMenuItem(ID,MF_GRAYED);
}
//使最小化按钮有效
void CMainFrame::OnMenuablemin()
{
// TODO: Add your command handler code here
//获得窗口风格
Style = ::GetWindowLong(m_hWnd,GWL_STYLE);
//设置新的风格
Style |= WS_MINIMIZEBOX;
::SetWindowLong(m_hWnd,GWL_STYLE,Style);
GetWindowRect(&Rect);
//重画窗口边框
::SetWindowPos(m_hWnd,HWND_TOP,Rect.left,Rect.top,Rect.Width(),
Rect.Height(),SWP_DRAWFRAME);
}
//使最大化按钮有效
void CMainFrame::OnMenuablemax()
{
// TODO: Add your command handler code here
//获得窗口风格
Style = ::GetWindowLong(m_hWnd,GWL_STYLE);
//设置新的风格
Style |= WS_MAXIMIZEBOX;
::SetWindowLong(m_hWnd,GWL_STYLE,Style);
GetWindowRect(&Rect);
//重画窗口边框
::SetWindowPos(m_hWnd,HWND_TOP,Rect.left,Rect.top,Rect.Width(),
Rect.Height(),SWP_DRAWFRAME);
}
//使关闭按钮有效
void CMainFrame::OnMenuableclose()
{
// TODO: Add your command handler code here
//获得系统菜单
CMenu *pMenu = GetSystemMenu(false);
//获得关闭按钮ID
UINT ID = pMenu->GetMenuItemID(pMenu->GetMenuItemCount()-1);
//使关闭按钮可用
pMenu->EnableMenuItem(ID,MF_ENABLED);
}
32.如何设置半透明窗体
要实现窗体的半透明效果,首先需要窗体具有0x80000值的扩展风格,然后调用User32动态库中

的SetLayeredWindowAttributes函数设置半透明窗体。在Visual C++中,

SetLayeredWindowAttributes函数并没有被直接封装,需要用户手动从user32动态库中导入。
使窗体具有0x80000值的扩展风格很容易,可以直接调用SetWindowLong API函数实现。
导入SetLayeredWindowAttributes函数,首先需要定义一个与SetLayeredWindowAttributes函数

具有相同函数原型的函数指针。
然后调用LoadLibrary函数加载user32动态库,最后调用GetProcAddress函数将

SetLayeredWindowAttributes
如下例:
//设置窗口扩展风格
SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(GetSafeHwnd(),
GWL_EXSTYLE)|0x80000);

typedef BOOL (WINAPI *FSetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);

FSetLayeredWindowAttributes SetLayeredWindowAttributes ;

HINSTANCE hInst = LoadLibrary("User32.DLL");

SetLayeredWindowAttributes =
(FSetLayeredWindowAttributes)GetProcAddress(hInst,"SetLayeredWindowAttributes");

if (SetLayeredWindowAttributes)
SetLayeredWindowAttributes(GetSafeHwnd(),RGB(0,0,0),128,2);
FreeLibrary(hInst);
33.如何实现百叶窗窗体
要实现百叶窗效果,可以使用Sleep函数,让界面在绘制的时候一点一点的绘制,就能实现。
参考如下代码:在OnPaint函数中.
int i,j,w,h;
CPaintDC dc(this);
CBitmap bit;
CDC mendc;
CRect rect;
this->GetWindowRect(&rect);
w=rect.Width();
h=rect.Height();
bit.LoadBitmap(IDB_BITMAP1);
mendc.CreateCompatibleDC(&dc);
mendc.SelectObject(&bit);

for(i=0;i<20;i++)
{

for(j=i;j<w;j+=20)
{
dc.BitBlt(j,0,1,h,&mendc,j,0,SRCCOPY);
Sleep(2);

}
}
mendc.DeleteDC();
::DeleteObject(&bit);
34.图文控件是实现
在Visual c++中,可以通过改写CButton的DrawItem方法实现自定义按钮的绘制。在按钮中绘制

图标主要使用了DrawItem方法中的lpDrawItemStruct参数的hDC成员来实现的。DrawItem方法是一个

虚拟方法,用于绘制控件的外观。。有关信息请查看MSDN。
如下示例:
1.派生一个CImageButton类。
void ImageButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc ;
dc.Attach(lpDrawItemStruct->hDC);
if (m_pImagelist)
{
UINT state = lpDrawItemStruct->itemState; //获取状态
UINT action = lpDrawItemStruct->itemAction;
//获取图像列中图像大小
IMAGEINFO imageinfo;
m_pImagelist->GetImageInfo(m_ImageIndex,&imageinfo);
CSize imagesize;
imagesize.cx = imageinfo.rcImage.right-imageinfo.rcImage.left;
imagesize.cy = imageinfo.rcImage.bottom - imageinfo.rcImage.top;
//在按钮垂直方向居中显示位图
CRect rect;
GetClientRect(rect);
CPoint point;
point.x = 5;
point.y = (rect.Height() - imagesize.cy)/2;
m_pImagelist->Draw

(&dc,m_ImageIndex,point,ILD_NORMAL|ILD_TRANSPARENT);
}
}
35.边框的绘制:
绘制边框我们使用FrameRect函数,FrameRect函数在矩形周围绘制边框。
如下示例:
HBRUSH CColourEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Change any attributes of the DC here
CDC* dc = GetDC(); //获取画布对象
CRect rect;
GetClientRect(rect); //获取客户区域
rect.InflateRect(1,1,1,1);//将客户区域增大一个像素
CBrush brush (m_Colour);//创建画刷
dc->FrameRect(rect,&brush);//绘制边框
// TODO: Return a non-NULL brush if the parent's handler should not be

called
return NULL;
}

36.设置编辑框文本颜色:
可以使用SetTextColor函数改变编辑框中文本的颜色,通过对消息WM_CTLCOLOR的响应来实现对

SetTextColor函数的调用。
如下示例:
HBRUSH CColourTextDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor == CTLCOLOR_EDIT)
pDC->SetTextColor(colour);
return hbr;
}
注:通过调用SetFocus函数能够响应WM_CTLCOLOR消息,既能让OnCtlColor函数调用.
37.一般的消息响应宏:
ON_BN_CLICKED( <id>, <memberFxn> ) afx_msg void memberFxn( );
ON_BN_DISABLE( <id>, <memberFxn> ) afx_msg void memberFxn( );
ON_BN_DOUBLECLICKED( <id>, <memberFxn> ) afx_msg void memberFxn( );
ON_BN_HILITE( <id>, <memberFxn> ) afx_msg void memberFxn( );
ON_BN_PAINT( <id>, <memberFxn> ) afx_msg void memberFxn( );
ON_BN_UNHILITE( <id>, <memberFxn> ) afx_msg void memberFxn( );

38.列表项的提示条
在默认情况下ListBox控件是不会自动显示水平滚动条的,如果ListBox控件的字符超出了列表的

宽度,那么超出的部分将无法显示。但我们可以通过加提示条的方法来解决这个问题.

39.关于数据库的操作:
先定义数据库操作的对象
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
CString TheValue;
打开数据库
::CoInitialize(NULL);先初始化环境
m_pConnection=NULL;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->ConnectionString="uid=admin;pwd=111;DRIVER={Microsoft Access Driver

(*.mdb)};DBQ=mrsoft.mdb;";
m_pConnection->Open(L"",L"",L"",adCmdUnspecified);

读取数据库
m_pRecordset=m_pConnection->Execute((_bstr_t)("select * from info"),NULL,adCmdText);
::CoInitialize();

注:关于CoInitialize()我还不太清楚,若有它的详细信息请告诉我,我的QQ是616124480,邮箱

号码是wjh_monkey@163.com。谢谢。

40.关于带查询功能的ComboxBox控件
查询功能的ComboBox控件主要是指当用户在ComboBox控件中输入字符时,如果ComboBox控件中有

以该字符开头的字符串,ComboBox控件就将该字符串完全显示出来。
我们可以通过ComboBox中的SelctString方法实现在ComboBox控件中查找符合要求的字符串,通过

SetEditSel方法使字符串处于选中状态。
下面为示例代码:
void AutoComplete::OnEditupdate()
{
if(!m_bAutoComplete)
return;
CString str;
GetWindowText(str);
int nLength=str.GetLength();
DWORD dwCurSel=GetEditSel();

DWORD dStart =LOWORD(dwCurSel);
DWORD dEnd =HIWORD(dwCurSel);
if(SelectString(-1,str)==CB_ERR)
{
SetWindowText(str);
if(dwCurSel!=CB_ERR)
SetEditSel(dStart,dEnd);
}
GetWindowText(str);
if(dEnd < nLength && dwCurSel!=CB_ERR)
SetEditSel(dStart,dEnd);
else
SetEditSel(nLength,-1);
}
BOOL AutoComplete::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message==WM_KEYDOWN)
{
m_bAutoComplete=true;
int nVirtKey=(int)pMsg->wParam;
if(nVirtKey==VK_DELETE||nVirtKey==VK_BACK)
m_bAutoComplete=false;
}
return CComboBox::PreTranslateMessage(pMsg);
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics