www.pudn.com > DesktopSearchBar.rar > CamelDlg.cpp
// CamelDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Camel.h"
#include "CamelDlg.h"
#include "AboutDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MYWM_NOTIFYICON WM_USER+5
/////////////////////////////////////////////////////////////////////////////
// CCamelDlg dialog
CCamelDlg::CCamelDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCamelDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCamelDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCamelDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCamelDlg)
DDX_Control(pDX, IDC_EDIT_KW, m_kw);
DDX_Control(pDX, IDC_CHECK_YAHOO, m_yahoo);
DDX_Control(pDX, IDC_CHECK_SOSO, m_soso);
DDX_Control(pDX, IDC_CHECK_GOOGLE, m_google);
DDX_Control(pDX, IDC_CHECK_BAIDU, m_baidu);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCamelDlg, CDialog)
//{{AFX_MSG_MAP(CCamelDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(IDOK, OnSearch)
ON_WM_CONTEXTMENU()
ON_COMMAND(ID_MENU_ABOUT, OnMenuAbout)
ON_COMMAND(ID_MENU_EXIT, OnMenuExit)
ON_COMMAND(ID_MENU_HELP, OnMenuHelp)
ON_WM_DESTROY()
ON_COMMAND(ID_MENU_AUTO, OnMenuAuto)
ON_COMMAND(ID_MENU_HIDE, OnMenuHide)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCamelDlg message handlers
BOOL CCamelDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
/*透明背景-----------------------------------*/
//加入WS_EX_LAYERED扩展属性
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 fun = NULL;
//取得SetLayeredWindowAttributes函数指针
fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if(fun)
fun(this->GetSafeHwnd(),RGB(255, 128, 0), 0, 1);
FreeLibrary(hInst);
}
/*嵌如桌面,制定鼠标的移动范围------------------*/
HWND hDesktop, hScreen;
hDesktop = ::FindWindow("Progman", NULL);
hScreen = FindWindowEx(hDesktop, 0, "SHELLDLL_DefView", NULL);
CWnd* pWndDesktop = CWnd::FromHandle(hScreen);
this->SetParent(pWndDesktop); //设置当前窗口为桌面子窗口
::GetWindowRect(hScreen, &screen_rect); //获得屏幕范围坐标
/*获得窗口大小--------------------------------*/
//CRect rectClient;
//GetClientRect(rectClient);
//int m_width = rectClient.Width();
//int m_leight = rectClient.Height();
/*添加图标--------------------------------------*/
HICON hIcon;
hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//添加窗口图标
m_pic.Create(NULL, WS_VISIBLE|SS_ICON, CRect(0, 7, 32, 32), this, 0);
m_pic.SetIcon(hIcon);
//添加托盘图标
m_tnid.cbSize=sizeof(NOTIFYICONDATA);
m_tnid.hWnd=this->m_hWnd;
m_tnid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
m_tnid.uCallbackMessage=MYWM_NOTIFYICON;
//用户定义的回调消息
CString szToolTip;
szToolTip=_T("Camel Desktop");
_tcscpy(m_tnid.szTip, szToolTip);
m_tnid.uID=IDR_MAINFRAME;
m_tnid.hIcon=hIcon;
::Shell_NotifyIcon(NIM_ADD,&m_tnid);
if(hIcon)::DestroyIcon(hIcon);
/*显示窗口-------------------------------------*/
i_ifShow = 1;
/*默认钩选-------------------------------------*/
m_soso.SetCheck(1);
//m_google.SetCheck(1);
//m_baidu.SetCheck(1);
//m_yahoo.SetCheck(1);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CCamelDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCamelDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCamelDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
//移动窗口
SendMessage(WM_SYSCOMMAND,SC_MOVE|MOUSE_MOVED,0);
CDialog::OnLButtonDown(nFlags, point);
}
void CCamelDlg::OnSearch()
{
//定义窗口显示方式
int showCmd = 0;
//初始化勾选项
int soso = 0, google = 0, baidu = 0, yahoo = 0;
//检查勾选
int count_select = 0;
if(m_soso.GetCheck()) { soso = 1; count_select++; }
if(m_google.GetCheck()) { google = 1; count_select++; }
if(m_baidu.GetCheck()) { baidu = 1; count_select++; }
if(m_yahoo.GetCheck()) { yahoo = 1; count_select++; }
if(count_select == 0) //没有勾选
{
return;
}
else if(count_select == 1)
{
showCmd = SW_SHOWMAXIMIZED; //只勾选了一个则最大化显示窗口
}
else
{
showCmd = SW_SHOW;
}
//获得输入关键词,保存到str_kw
CString str_kw, str_kw_temp;
m_kw.GetWindowText(str_kw_temp);
str_kw_temp.TrimLeft();
//把汉字转换成URL编码
str_kw = CH_to_URLencode(str_kw_temp);
if(str_kw.GetLength() == 0) //没有输入关键词则打开首页
{
if(soso)
ShellExecute( NULL,NULL,"IEXPLORE", "http://www.soso.com", NULL, showCmd );
if(google)
ShellExecute( NULL,NULL,"IEXPLORE", "http://www.google.cn", NULL, showCmd );
if(baidu)
ShellExecute( NULL,NULL,"IEXPLORE", "http://www.baidu.com", NULL, showCmd );
if(yahoo)
ShellExecute( NULL,NULL,"IEXPLORE", "http://www.yahoo.cn", NULL, showCmd );
}
else
{
if(soso)
OpenURL((CString)"http://www.soso.com/q?sc=web&ch=w.ptl&w=", str_kw, showCmd);
if(google)
OpenURL((CString)"http://www.google.com/search?hl=zh-CN&btnG=Google+%E6%90%9C%E7%B4%A2&lr=&q=", str_kw, showCmd); //google.com
//OpenURL((CString)"http://www.google.cn/search?hl=zh-CN&q=", str_kw, showCmd); //google.cn
if(baidu)
OpenURL((CString)"http://www.baidu.com/s?cl=3&wd=", str_kw, showCmd);
if(yahoo)
OpenURL((CString)"http://search.cn.yahoo.com/search?p=", str_kw, showCmd);
}
return;
}
void CCamelDlg::OpenURL(CString &server_url, CString &str_kw, int showCmd)
{
// 拼凑url
CString url = "\"" + server_url + str_kw + "\"";
//打开url explorer -> IEXPLORE
ShellExecute( NULL,NULL,"IEXPLORE", url, NULL, showCmd );
//清空url, kw, server_url
url.Empty();
server_url.Empty();
return;
}
void CCamelDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menu;
menu.LoadMenu(IDR_MENU1);
CMenu* popMenu = menu.GetSubMenu(0);
popMenu->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);
menu.DestroyMenu();
}
void CCamelDlg::OnMenuAbout()
{
//MB_USERICON = IDR_MAINFRAME;
//MessageBox("Camel Desktop\nVersion: 1.0.0.1\n", "About Camel Desktop", IDR_MAINFRAME); //MB_ICONINFORMATION
AboutDlg aboutDlg;
aboutDlg.DoModal();
}
void CCamelDlg::OnMenuAuto()
{
LPITEMIDLIST pidl;
char tc_source[255], tc_target[255];
//获得启动路径保存到szPath
SHGetSpecialFolderLocation(NULL,CSIDL_STARTUP,&pidl);
SHGetPathFromIDList( pidl, tc_target);
//得到当前执行文件的文件名(包括路径)
::GetModuleFileName(NULL,tc_source,255);
sprintf(tc_target,"%s\\Camel.exe",tc_target);
LPCTSTR str_source(tc_source);
LPCTSTR str_target(tc_target);
if(CopyFile(str_source,str_target,1))
{
MessageBox("Added me to startup successfully.\nI will autorun when system start.\nThank you.", "Camel Success", MB_ICONINFORMATION);
}
else
{
CFileStatus fs;
if(CFile::GetStatus(str_target, fs)) //判断启动中是否存在
{
int ifdel = MessageBox("I'm already in startup list.\nIf you want remove me from sartup?", "Camel Question", MB_ICONQUESTION|MB_OKCANCEL);
if(ifdel == 1)
if(!DeleteFile(str_target)) //从启动中删除文件
{
CString info = "Can not delete me now, because I'm running now\nYou can manual delete from \"" + (CString)str_target + "\" when I'm exit.";
MessageBox(info, "Camel Error", MB_ICONERROR);
}
}
else
{
MessageBox("Failed add me to startup.\nSomething occur unexpected.","Camel Error",MB_ICONERROR);
}
}
}
void CCamelDlg::OnMenuExit()
{
CDialog::OnCancel();
}
void CCamelDlg::OnMenuHelp()
{
MessageBox("Any question?\nPlease E-mail to: wangyangch@gmail.com\nThank you.","Camel Help", MB_ICONINFORMATION);
}
void CCamelDlg::OnDestroy()
{
CDialog::OnDestroy();
::Shell_NotifyIcon(NIM_DELETE,&m_tnid);
}
//int jump;
//DEL void CCamelDlg::OnTimer(UINT nIDEvent)
//DEL {
//DEL if(jump%2 == 0)
//DEL m_pic.MoveWindow(0,7,32,32,1);
//DEL else
//DEL m_pic.MoveWindow(0,10,32,32,1);
//DEL
//DEL jump++;
//DEL
//DEL if(jump > 3)
//DEL KillTimer(0);
//DEL
//DEL CDialog::OnTimer(nIDEvent);
//DEL }
LRESULT CCamelDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message){
case MYWM_NOTIFYICON:
//如果是用户定义的消息
if(lParam==WM_LBUTTONDOWN)
{
//显示 Camel 窗口
ShowWindow(1);
i_ifShow = 1;
//显示桌面
keybd_event(VK_LWIN,0,0,0);
keybd_event('M',0,0,0);
keybd_event(VK_LWIN,0,KEYEVENTF_KEYUP,0);
keybd_event('M',0,KEYEVENTF_KEYUP,0);
//设置焦点
/*
GetDlgItem(IDC_EDIT_KW)->SendMessage(WM_LBUTTONDOWN);
GetDlgItem(IDC_EDIT_KW)->SendMessage(WM_LBUTTONUP);
m_kw.SetFocus();
SetActiveWindow();
AfxGetApp()->m_pMainWnd->SendMessage(WM_LBUTTONDBLCLK);
m_kw.SetFocus();
m_hWnd->SendMessage(WM_LBUTTONDBLCLK);
m_kw.SendMessage(WM_LBUTTONDOWN);
m_kw.SendMessage(WM_LBUTTONUP);
*/
//Let Camel jump
//jump = 0;
//SetTimer(0,150,NULL);
}
else if(lParam==WM_RBUTTONDOWN){
//鼠标右键单击弹出菜单
CMenu menu;
menu.LoadMenu(IDR_MENU1);
//载入事先定义的菜单
CMenu* pMenu=menu.GetSubMenu(0);
CPoint pos;
GetCursorPos(&pos);
SetForegroundWindow(); //鼠标点其他位置菜单消失
pMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,pos.x,pos.y,AfxGetMainWnd());
}
break;
//case WM_SYSCOMMAND:
//如果是系统消息
// if(wParam==SC_MINIMIZE){
//接收到最小化消息时主窗口隐藏
// AfxGetApp()->m_pMainWnd->ShowWindow(SW_HIDE);
// return 0;
// }
// break;
}
return CDialog::WindowProc(message, wParam, lParam);
}
void CCamelDlg::OnMenuHide()
{
//m_kw.SendMessage(WM_LBUTTONDOWN);
i_ifShow = (i_ifShow == 0)? 1 : 0;
ShowWindow(i_ifShow);
}
LPTSTR CCamelDlg::coverChinesetoANSI(LPWSTR szSource, int nEncoding)
{
return 0;
}
CString CCamelDlg::CH_to_URLencode(CString keywords)
{
CString urlstr;
BOOL fRaw = 1; //决定"+"是否被编码
for(int i = 0; i < keywords.GetLength(); i++)
{
unsigned char c = keywords[i];
if(fRaw && c == '+')
urlstr += "%2B";
else if(c > 0x20 && c < 0x7f && c != '&')
urlstr += c;
else if(c == 0x20)
urlstr += fRaw ? ' ' : '+';
else
{
CString tmp;
tmp.Format("%%%02X", c);
urlstr += tmp;
}
}
return urlstr;
}