关闭

MFC 增加工具条类的问题

457人阅读 评论(0) 收藏 举报
分类:

#pragma once

class CToolbar
{
public:
 CToolbar(void);
public:
 ~CToolbar(void);
 void  SetCtrlAction(HWNDhWnd,int nIndex);
};

 

#include "StdAfx.h"
#include "Toolbar.h"

CToolbar::CToolbar(void)
{
}

CToolbar::~CToolbar(void)
{
}
// TODO: 在此处引用程序需要的其他头文件
///////////////////////////////////////////
#define WM_AMS_ACTION_UNKNOWN     -1         //无法识别的类型;
#define WM_AMS_ACTION_SCLICK    //控件的单击;
#define WM_AMS_ACTION_DLICK    //控件的双击;
#define WM_AMS_ACTION_CLOSE    //关闭;
#define WM_AMS_ACTION_MIN     //最小化;
#define WM_AMS_ACTION_MAX     //最大化;
#define WM_AMS_ACTION_RESTORE    //还原;
#define WM_AMS_ACTION_ENABLE    //Enable;
#define WM_AMS_ACTION_DISABLE    //Disable;
#define WM_AMS_ACTION_SHOW     //显示;
#define WM_AMS_ACTION_HIDE            //隐藏;
#define WM_AMS_ACTION_TOPMOST  10         //置顶;
#define WM_AMS_ACTION_NOTOPMOST   11         //取消置顶;
#define WM_AMS_ACTION_SETFOCUSE   12         //设置焦点;
#define WM_AMS_ACTION_LOSTFOCUSE   13         //去除焦点;

#define WM_AMS_CTRLTYPE_BTN          //普通按钮;
#define WM_AMS_CTRLTYPE_RADIOBTN           //单选按钮
#define WM_AMS_CTRLTYPE_CHECKBOX           //复选按钮;


#define  WM_AMS_CHECKSTATE_CHECK         //选中
#define WM_AMS_CHECKSTATE_UNCHECK    // 不选中;

#define WM_AMS_TYPE_LISTBOX                //listbox控件
#define WM_AMS_TYPE_COMBOBOX               //combobox控件;


BOOL SetItemAction(HWND hwnd,int nAction)
{
 if (hwnd == NULL)
  return FALSE;

 switch(nAction)
 {
 case WM_AMS_ACTION_SCLICK:
  ::SendMessage(hwnd,WM_LBUTTONDOWN, 0, 0);
  ::SendMessage(hwnd,WM_LBUTTONUP, 0, 0);
  break;
 case WM_AMS_ACTION_DLICK:
  ::SendMessage(hwnd,WM_LBUTTONDOWN, 0, 0);
  ::SendMessage(hwnd,WM_LBUTTONUP, 0, 0);
  ::SendMessage(hwnd,WM_LBUTTONDOWN, 0, 0);
  ::SendMessage(hwnd,WM_LBUTTONUP, 0, 0);
  break;
 case WM_AMS_ACTION_CLOSE:
  ::SendMessage(hwnd,WM_CLOSE,0,0);
  break;
 case WM_AMS_ACTION_MIN:
  ::ShowWindow(hwnd,SW_MINIMIZE);
  break;
 case WM_AMS_ACTION_MAX:
  ::ShowWindow(hwnd,SW_MAXIMIZE);
  break;
 case WM_AMS_ACTION_RESTORE:
  ::ShowWindow(hwnd,SW_RESTORE);
  break;
 case WM_AMS_ACTION_ENABLE:
  ::EnableWindow(hwnd,TRUE);
  break;
 case WM_AMS_ACTION_DISABLE:
  ::EnableWindow(hwnd,FALSE);
  break;
 case WM_AMS_ACTION_SHOW:
  ::ShowWindow(hwnd,SW_SHOW);
  break;
 case WM_AMS_ACTION_HIDE:
  ::ShowWindow(hwnd,SW_HIDE);
  break;
 case WM_AMS_ACTION_TOPMOST:
  ::SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE| SWP_NOSIZE);
  break;
 case WM_AMS_ACTION_NOTOPMOST:
  ::SetWindowPos(hwnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);

 default:
  break;

 }
 return TRUE;
}

BOOL SetWindowTopmost(HWND hWnd,BOOL isTopmost)
{
 if (hWnd == NULL)
  return FALSE;

 if(isTopmost)
 {
  BOOL bIsMinimized =::IsIconic(hWnd);
  //如果该窗口最小化了,则恢复原状;
  if (bIsMinimized)
  {
   SetItemAction(hWnd,WM_AMS_ACTION_RESTORE);
   Sleep(500);
  }
  //将该窗口置顶
  SetItemAction(hWnd,WM_AMS_ACTION_TOPMOST);
 }
 else
  SetItemAction(hWnd,WM_AMS_ACTION_NOTOPMOST);

 return TRUE;
}

//执行操作
void  CToolbar::SetCtrlAction(HWND hWnd,intnIndex)
{
 if (!hWnd)
  return;


 int nBtnCount =::SendMessage(hWnd,TB_BUTTONCOUNT,(WPARAM)0,(LPARAM)0);
 if(nIndex >= nBtnCount)
  return;

 HWND hMainWnd =::GetAncestor(hWnd,GA_ROOT);//回复句柄到指定窗口的父窗口
 //鼠标事件
 POINT ptOld = {0};
 GetCursorPos(&ptOld);//屏幕坐标显示光标的位置

 DWORD dwProcessID = 0;
 unsigned long nSize=0;

 RECT rectItem={0};
 HANDLE hProcess = NULL;
 RECT *pRect = NULL;

 GetWindowThreadProcessId(hWnd,&dwProcessID);//找到指定窗口的创建者并返回其线程ID

 hProcess =OpenProcess(PROCESS_ALL_ACCESS,false,dwProcessID);//打开一个已经存在的线程对象,并返回其句柄
 if (!hProcess)
 {
  //::MessageBox(NULL,_T("获取进程句柄操作失败!"),_T("错误!"),MB_OK);//在程序运行过程中消息提示窗体
  return ;
 }

 pRect=(RECT*)VirtualAllocEx(hProcess, NULL,sizeof(RECT), MEM_COMMIT,PAGE_READWRITE);//储备或提交的内存区域内的指定进程的虚拟地址空间
 if (!pRect)
 {
  //MessageBox(NULL,_T("无法分配内存!"),_T("错误!"),MB_OK);
 }

 WriteProcessMemory(hProcess, pRect,&rectItem, sizeof(RECT), NULL);//写rectItem到指定的进程的内存区域
 ::SendMessage(hWnd, TB_GETITEMRECT,(WPARAM)nIndex, (LPARAM)pRect);
 ReadProcessMemory(hProcess,pRect,&rectItem,sizeof(RECT),&nSize);//Thisfunction reads memory in a specified process. The entire area to beread must be accessible

 POINT pt={0};
 pt.x = rectItem.left +(rectItem.right-rectItem.left)/2;
 pt.y = rectItem.top +(rectItem.bottom-rectItem.top)/2;
 ::ClientToScreen(hWnd,&pt);//转换成屏幕坐标

 SetCursorPos(pt.x, pt.y);//该函数把光标移到屏幕的指定位置

 //将该窗口置顶
 HWND hForeWnd =NULL;        
 DWORDdwForeID;        
 DWORDdwCurID;        
 hForeWnd =::GetForegroundWindow();        
 dwCurID = ::GetCurrentThreadId();        
 dwForeID = ::GetWindowThreadProcessId(hForeWnd,NULL);   
 ::AttachThreadInput( dwCurID, dwForeID,TRUE);
 SetWindowTopmost(hMainWnd,TRUE);
 Sleep(80);

 mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0,0,0);
 mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0,0);

 Sleep(30);
 SetWindowTopmost(hMainWnd,FALSE);
 ::AttachThreadInput(dwCurID, dwForeID,FALSE);

 VirtualFreeEx(hProcess, pRect, 0,MEM_RELEASE);

 SetCursorPos(0, 0);
 CloseHandle(hProcess);

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:988514次
    • 积分:17715
    • 等级:
    • 排名:第539名
    • 原创:741篇
    • 转载:522篇
    • 译文:0篇
    • 评论:105条
    最新评论