VC6.0各个小模块代码整理集合

来源:互联网 发布:中国好声音网络歌手 编辑:程序博客网 时间:2021/09/29 05:04

1.对话框添加菜单

定义菜单,然后在对话框属性中填入菜单ID

2. 选择文件夹

HWND hwnd= GetSafeHwnd();   //得到窗口句柄
CString filePath= "";//得到文件路径
LPMALLOC pMalloc;
if (::SHGetMalloc(&pMalloc) == NOERROR)//取得IMalloc分配器接口
{   
BROWSEINFO  bi;
TCHAR pszBuffer[MAX_PATH];
LPITEMIDLIST  pidl;   

bi.hwndOwner  = hwnd;
bi.pidlRoot  = NULL;
bi.pszDisplayName   = pszBuffer;
bi.lpszTitle  = _T("选择文件夹"); //选择目录对话框的上部分的标题
//添加新建文件夹按钮 BIF_NEWDIALOGSTYLE
bi.ulFlags  =  BIF_NEWDIALOGSTYLE | BIF_RETURNONLYFSDIRS | BIF_RETURNFSANCESTORS;
bi.lpfn  = NULL;
bi.lParam  = 0;
bi.iImage  = 0;
if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)  //取得IMalloc分配器接口
{   
if (::SHGetPathFromIDList(pidl, pszBuffer)) //获得一个文件系统路径
{
filePath = pszBuffer;
}
pMalloc->Free(pidl);//释放内存
MessageBox(filePath);
}
pMalloc->Release();//释放接口
}

3.打开文件

//设置文件类型过滤
char filter[] = "图像 (*.bmp;*.tif;*.jpg)|*.bmp;*.tif;*.jpg|All Files (*.*)|*.*||";


//文件对话框
    CFileDialog fdlg(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, filter);


fdlg.m_ofn.lpstrInitialDir = "d:\\"; //设置初始文件目录

//弹出对话框,如果单击的是“OK”按钮
if(fdlg.DoModal() == IDOK) 
{
//得到打开的文件的文件名
CString fileName = fdlg.GetPathName();


//以对话框信息的形式提示刚才打开的文件的路径
MessageBox(fileName);
}

4.对话框中添加工具栏

首先,资源视图中添加工具栏 iD为IDR_TOOLBAR1


m_toolBar.Create(this); //创建工具栏
m_toolBar.LoadToolBar(IDR_TOOLBAR1);//加载工具栏

//控件条定位
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);

最后ctrl+w 选择command消息和工具栏ID

5.透明 对话框 窗口

//设置半透明对话框
SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, 
GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
HINSTANCE hInst = LoadLibrary("User32.DLL");  //加载库文件
if(hInst) 

typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD); 
MYFUNC func = NULL;//函数指针
//取得SetLayeredWindowAttributes函数指针 
func = (MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
//使用SetLayeredWindowAttributes函数设定透明度
if(func)func(this->GetSafeHwnd(), RGB(0, 0, 0), 200, 0x2); 
FreeLibrary(hInst); 
}

6.创建托盘

// 发送托盘消息
BOOL TrayMessage(HWND hWnd, DWORD dwMessage, HICON hIcon, PSTR pszTip)
{
    BOOL bReturn;
    NOTIFYICONDATA NofityData;
    NofityData.cbSize = sizeof(NOTIFYICONDATA);
    NofityData.hWnd = hWnd;
    NofityData.uID = IDR_MAINFRAME;//要托盘的窗口ID
    NofityData.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
    NofityData.uCallbackMessage = WM_TRAY_NOTIFICATION;//自定义的消息,被通知时,使用
    NofityData.hIcon = hIcon;
    lstrcpyn(NofityData.szTip, pszTip, sizeof(NofityData.szTip));
    bReturn = Shell_NotifyIcon(dwMessage, &NofityData);//向托盘区发送消息
    if (hIcon)
        DestroyIcon(hIcon);
    return bReturn;
}




BEGIN_MESSAGE_MAP(CTrayDialogDlg, CDialog)
//{{AFX_MSG_MAP(CTrayDialogDlg)
ON_MESSAGE(WM_TRAY_NOTIFICATION,OnTrayNotification)
END_MESSAGE_MAP()


在CXXXXDlg::OnSysCommand中

if(nID == SC_MINIMIZE)
{
TrayMessage(m_hWnd, NIM_ADD, NULL, "托盘实例");
TrayMessage(m_hWnd, NIM_MODIFY, m_hIcon, "托盘实例");
ShowWindow(SW_HIDE);
}


LONG CTrayDialogDlg::OnTrayNotification(WPARAM wparam, LPARAM lparam)
{
switch (lparam )
{
case WM_RBUTTONUP:       // 在托盘上右键弹起时的响应处理
case WM_LBUTTONDBLCLK:// 双击托盘时的响应处理
ShowWindow(SW_SHOW);
   TrayMessage(m_hWnd, NIM_DELETE, NULL, "");   //托盘消失
}
return 0;
}

17属性对话框  向导对话框

CMyPropertySheet::CMyPropertySheet

class CPropertyPage1 : public CPropertyPage

class CPropertyPage2 : public CPropertyPage

CMyPropertySheet 构造函数中:

AddPage(&m_PropertyPage1);
AddPage(&m_PropertyPage2);
//SetWizardMode();不带这句是属性页。带了是向导对话框。

//如果是向导,在

//CPropertyPage1和CPropertyPage2的OnInitDialog中  :

CPropertyPage::OnInitDialog();
CPropertySheet* pDlg = (CPropertySheet*) GetParent();   
pDlg->SetFinishText("完成");

//在CPropertyPage::OnSetActive中

设置上一页 下一页SetWizardButtons()

  • PSWIZB_BACK   Back button

  • PSWIZB_NEXT   Next button

  • PSWIZB_FINISH   Finish button

  • PSWIZB_DISABLEDFINISH   Disabled Finish button

18播放AVI视频

使用

CAnimation 

Initialization:Create() Creates an animation control and attaches it to a CAnimateCtrl object. CreateEx() Creates an animation control with the specified Windows extended styles and attaches it to a CAnimateCtrl object. Operations:Close() Closes the AVI clip that was previously opened. Open() Opens an AVI clip from a file or resource and displays the first frame.  Play() Plays the AVI clip without sound. Seek() Displays a selected single frame of the AVI clip. Stop() Stops playing the AVI clip.


19.获取驱动器列表

列表控件 listcontrl
void CDiskListDlg::OnButtonList()
{
    m_listBox.ResetContent();        //情况列表
    char  drives[128];                //存储所以驱动器名称
    char* pDrive;                    //驱动器指针
    //取得系统的第一个逻辑驱动器
    if (!GetLogicalDriveStrings(sizeof(drives), drives))
    {
        MessageBox("获取驱动器失败。");
        return ;
    }
    
    pDrive = drives;                //指向第一个逻辑驱动器
    //将驱动器字符放入列表框中
    while(*pDrive)
    {
        //将驱动器名称加入列表中
        m_listBox.AddString(pDrive);
        //指向下一个驱动器标识符
        pDrive += strlen(pDrive) + 1;                
    }            
}
20.组合框 (COMBOX)使用

初始化

m_imageList.Create(16, 16, ILC_COLOR|ILC_MASK, 4, 4);//CImageList
    m_imageList.Add(LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON1)));
    m_imageList.Add(LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON2)));
    m_imageList.Add(LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON3)));
    m_imageList.Add(LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON4)));
    m_ComBoxEx.SetImageList(&m_imageList);//插入icon
    
    for(int i=0; i<m_imageList.GetImageCount(); i++)
     {
      COMBOBOXEXITEM     cbi ={0};
      CString            str;
      int                nItem;

      cbi.mask = CBEIF_IMAGE | CBEIF_INDENT | CBEIF_OVERLAY |
       CBEIF_SELECTEDIMAGE | CBEIF_TEXT;

      cbi.iItem = i;
      str.Format(_T("Numb %2d"), i);
      cbi.pszText = (LPTSTR)(LPCTSTR)str;
      cbi.cchTextMax = str.GetLength();
      cbi.iImage = i;
      cbi.iSelectedImage = i;
      cbi.iOverlay = 2;
      cbi.iIndent = 0;//(i & 0x03);   //Set indentation according
      //to item position
      nItem = m_ComBoxEx.InsertItem(&cbi);//插入项目
      ASSERT(nItem == i);
     }
    m_ComBoxEx.SetCurSel(0);//默认选中第一个


然后添加CBN_EDITCHANGE消息处理函数

21groupbox(分组框)使用

CButton*   m_GroupBox1;     
m_GroupBox1=new   CButton;   
m_GroupBox1->Create("Group   one",WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
CRect(10,10,150,100),this,IDC_STATIC);  

CButton *myButton1 = new   CButton;
myButton1->Create("radio1",BS_AUTORADIOBUTTON,
CRect(10, 20, 100, 30),m_GroupBox1,IDC_RADIO1);  //将IDC_RADIO1放入m_GroupBox1
myButton1->ShowWindow(true);   

CButton *myButton2 = new   CButton;
myButton2->Create("radio2",BS_AUTORADIOBUTTON,
CRect(10, 40, 100, 50),m_GroupBox1,IDC_RADIO2);  
myButton2->ShowWindow(true); 


CButton*   m_GroupBox2;     
m_GroupBox2=new   CButton;   
m_GroupBox2->Create("Group   two",WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
CRect(10,100,150,180),this,IDC_STATIC);  

CButton *myButton3 = new   CButton;
myButton3->Create("radio1",BS_AUTORADIOBUTTON,
CRect(10, 20, 100, 30),m_GroupBox2,IDC_RADIO3);  
myButton3->ShowWindow(true);   

CButton *myButton4 = new   CButton;
myButton4->Create("radio2",BS_AUTORADIOBUTTON,
CRect(10, 40, 100, 50),m_GroupBox2,IDC_RADIO4);  
myButton4->ShowWindow(true); 

22.快捷键 hot key 控件

拖拽一个hot key控件,添加变量

m_hotkey.SetHotKey(VK_F2,MOD_CONTROL);
m_hotkey.ShowWindow(SW_HIDE);
WORD virtualcode,modifiers;
m_hotkey.GetHotKey(virtualcode,modifiers);
//设置热键ID=100
RegisterHotKey(this->m_hWnd,100,modifiers,virtualcode);//注册快捷键

重载OnHotKey函数 ON_MESSAGE(WM_HOTKEY,OnHotKey)

void CHotKeyDlg::OnHotKey(WPARAM wParam,LPARAM lParam)

m_path = "readme.txt";
if (wParam == 100)
{
//UpdateData(TRUE); //执行
ShellExecute(this->m_hWnd,"open",m_path,NULL,NULL,SW_SHOW);
}
}

23 ip control ip控件

UpdateData();


if(m_IPAddre.IsBlank())   //判断是否为空 
{
MessageBox("IP地址为空!");
return ;
}  


BYTE nField0,nField1,nField2,nField3;


if(m_IPAddre.GetAddress(nField0,nField1,nField2,nField3) !=4)
{
MessageBox("你输入的IP地址不完整!");


m_IPAddre.SetFieldFocus(0);
return;
}
char buf[100];
wsprintf(buf,"ip地址为 %d.%d.%d.%d",nField0,nField1,nField2,nField3);
  MessageBox(buf);

24 列表框list control 点击排序

重载CListCtrl的OnColumnclick函数,调用

BEGIN_MESSAGE_MAP(ListCtrlEx, CListCtrl)
ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
END_MESSAGE_MAP()


int CALLBACK ListCtrlEx::Compare(LPARAM lParam1,LPARAM lParam2,LPARAM lParamSort)
{
//ListCtrlEx* pListCtrl = reinterpret_cast<ListCtrlEx*>(lParamSort);

//pListCtrl->......
//如果返回-1表示不进行排序
return 1;
}
void ListCtrlEx::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
SortItems(Compare, reinterpret_cast<DWORD>(this));
*pResult = 0;
}

添加记录

m_list.InsertColumn(0, "date", LVCFMT_LEFT, 70);//增加data列
m_list.InsertColumn(1, "num", LVCFMT_LEFT, 70);//增加num列
m_list.InsertItem(0, "");//插入第一条记录
m_list.SetItemText(0, 0, "a");//设置记录内容
m_list.SetItemText(0, 1, "001");
m_list.InsertItem(1, "");//插入第二条记录
m_list.SetItemText(1, 0, "b");//设置记录内容
m_list.SetItemText(1, 1, "002");

25 scroll bar 滚动条

CScrollBar*   pSB   =   (CScrollBar*)GetDlgItem(IDC_HSCROLL);   
pSB->SetScrollRange(nMin,   nMax);  

---

void CScrollBarDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
// TODO: Add your message handler code here and/or call default
int   nTemp1,   nTemp2;   
    
nTemp1   =   pScrollBar->GetScrollPos();  //当前位置
CString str;
str.Format("%d",nTemp1);
GetDlgItem(IDC_HDATA)->SetWindowText(str);
switch(nSBCode)
{   
case   SB_THUMBPOSITION:   //鼠标拖动后,松手
pScrollBar->SetScrollPos(nPos);   
break;   
case   SB_LINELEFT:    //走一行
nTemp2   =   (nMax   -   nMin)   /   10;   
if   ((nTemp1   -   nTemp2)   >   nMin)
{   
nTemp1   -=   nTemp2;   
}   
else
{   
nTemp1   =   nMin;   
}   
pScrollBar->SetScrollPos(nTemp1);   
break;   
case   SB_LINERIGHT:     //走一行
nTemp2   =   (nMax   -   nMin)   /   10;   
if   ((nTemp1   +   nTemp2)   <   nMax)
{   
nTemp1   +=   nTemp2;   
}   
else
{   
nTemp1   =   nMax;   
}   
pScrollBar->SetScrollPos(nTemp1);   
break;   

CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

25 图文按钮

http://blog.csdn.net/jiht594/article/details/7195012

//创建图像列表
m_images.Create(24, 24, ILC_COLOR24 | ILC_MASK, 1, 0);
m_images.Add(AfxGetApp()->LoadIcon(IDI_ICON1)); //加载图标
m_images.Add(AfxGetApp()->LoadIcon(IDI_ICON2));

m_button1.SetImageList(&m_images);//设置图像列表
m_button2.SetImageList(&m_images);

//设置按钮显示的图像索引
m_button1.SetImageIndex(0);
m_button2.SetImageIndex(1);

26 tree control 树控件

//初始化树形控件节点
TV_INSERTSTRUCT treeCtrlItem;
HTREEITEM hTreeItem;
treeCtrlItem.hInsertAfter = TVI_LAST;
treeCtrlItem.item.mask = TVIF_TEXT|TVIF_PARAM;

//设置第一级节点
treeCtrlItem.hParent = TVI_ROOT;
treeCtrlItem.item.pszText = "C:";
treeCtrlItem.item.lParam = 1;  //根
hTreeItem = m_tree.InsertItem(&treeCtrlItem);
//设置第二级节点
treeCtrlItem.hParent = hTreeItem;
treeCtrlItem.item.pszText = "Program Files";
treeCtrlItem.item.lParam = 2;
m_tree.InsertItem(&treeCtrlItem);
treeCtrlItem.item.pszText = "My Documents";
treeCtrlItem.item.lParam = 2;
m_tree.InsertItem(&treeCtrlItem);
treeCtrlItem.item.pszText = "Windows";
treeCtrlItem.item.lParam = 2;
hTreeItem = m_tree.InsertItem(&treeCtrlItem);
//设置第一级节点
treeCtrlItem.hParent = TVI_ROOT;
treeCtrlItem.item.pszText = "D:";
treeCtrlItem.item.lParam = 1;  //根
hTreeItem = m_tree.InsertItem(&treeCtrlItem);
//设置第二级节点
treeCtrlItem.hParent = hTreeItem;
treeCtrlItem.item.pszText = "Tool";
treeCtrlItem.item.lParam = 2;
m_tree.InsertItem(&treeCtrlItem);
treeCtrlItem.item.pszText = "Image";
treeCtrlItem.item.lParam = 2;
m_tree.InsertItem(&treeCtrlItem);
treeCtrlItem.item.pszText = "Source";
treeCtrlItem.item.lParam = 2;
m_tree.InsertItem(&treeCtrlItem);


//获取节点名字,显示出来

void CTreeDirDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) 
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;


CString strPath;
CString strInfo;
//得到选择的项
HTREEITEM currItem = m_tree.GetSelectedItem();
TV_ITEM treeCtrlItem;
treeCtrlItem.hItem = currItem;
m_tree.GetItem(&treeCtrlItem);
//获取得到项的文本
strInfo = m_tree.GetItemText(m_tree.GetSelectedItem());
long currlParam = treeCtrlItem.lParam;
if(currlParam == 1)
{ //如果是根节点
strPath = strInfo;
}
else
{
//如果不是跟节点
treeCtrlItem.hItem = m_tree.GetParentItem(m_tree.GetSelectedItem());
m_tree.GetItem(&treeCtrlItem);
currItem = treeCtrlItem.hItem;
strPath = m_tree.GetItemText(currItem);
strPath += "\\";
strPath += strInfo;
}
//设置显示被选择的项
GetDlgItem(IDC_STATIC_SELECTD)->SetWindowText(strPath);


*pResult = 0;
}

27 重命名文件 文件夹

MoveFile 移动文件或文件夹(包括子文件,可实现重命名)

To specify how to move the file, use the MoveFileEx or MoveFileWithProgress function.

28 读写ini文件

::WritePrivateProfileString("节点", "键1", "值1", "c:\\products.ini");

::WritePrivateProfileString("节点", "键2", "值2", "c:\\products.ini");

//c:\\products.ini中的内容
//[节点]
//键1=值1
//键2=值2

CString product;

::GetPrivateProfileString("ProductName","键1", NULL ,product.GetBuffer(128), 128, "c:\\products.ini");

29获取文件信息

SHGetFileInfo


  SHGFI_ICON; //获得图标

  SHGFI_DISPLAYNAME; //获得显示名
  SHGFI_TYPENAME; //获得类型名
  SHGFI_ATTRIBUTES; //获得属性
  SHGFI_LARGEICON; //获得大图标
  SHGFI_SMALLICON; //获得小图标
  SHGFI_PIDL; // pszPath是一个标识符

获取文件图标,画出

void CGetIconView::OnXXXX() 
{
// TODO: Add your command handler code here
CGetIconDoc *pDoc = GetDocument();
SHFILEINFO shfi; 
CDC *pDC = GetDC();
memset (&shfi, 0, sizeof(shfi)); 
SHGetFileInfo (pDoc->m_filePath, //文件名 
FILE_ATTRIBUTE_NORMAL, &shfi, 
sizeof (shfi), SHGFI_ICON  | SHGFI_USEFILEATTRIBUTES); 


//HICON hIcon = CopyIcon (shfi.hIcon); 
pDC   ->DrawIcon(10,10,shfi.hIcon);   
DestroyIcon (shfi.hIcon); 
}

0 0