向记事本发送字符消息

OnSend,OnSave是对Button控件的响应函数。

 

void CSendNotepadMsgDlg::OnSend() //发送字符串
{
 // TODO: Add your control notification handler code here
 HWND hWnd = ::FindWindow("Notepad", NULL);
 if(hWnd)
 {
  HWND hEdit = FindWindowEx(hWnd, NULL, "Edit", NULL);
  if(hEdit)
  ::PostMessage(hEdit, WM_CHAR, 0x48, 0);//发送‘H’到notepad
  ::PostMessage(hEdit, WM_CHAR, 0x45, 0);//发送‘E’到notepad
  ::PostMessage(hEdit, WM_CHAR, 0x4C, 0);//发送‘L’到notepad
  ::PostMessage(hEdit, WM_CHAR, 0x4C, 0);//发送‘L’到notepad
  ::PostMessage(hEdit, WM_CHAR, 0x4F, 0);//发送‘O’到notepad
 }
 else
 {
  AfxMessageBox("请打开记事本文件!");
  return;
 } 
}

void CSendNotepadMsgDlg::OnSave() //保存文件
{
 // TODO: Add your control notification handler code here
 
 CWnd* pWnd =FindWindow("Notepad", NULL);
 if (pWnd->GetSafeHwnd())
 {
  pWnd->ShowWindow(SW_NORMAL);
  pWnd->SetForegroundWindow();
  keybd_event(VK_CONTROL, 0, 0, 0);//按下Ctrl键
  keybd_event('S', 0, 0, 0);//按下S键
  keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);//释放Ctrl键
  keybd_event('S', 0, KEYEVENTF_KEYUP, 0);//释放S键
 }
 else
 {
  AfxMessageBox("未找到打开的记事本文件!");
  return;
 } 
}

你可能感兴趣的:(记事本)