鱼C论坛

 找回密码
 立即注册
查看: 2749|回复: 2

句柄问题

[复制链接]
发表于 2012-2-7 19:43:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我利用了FindWindow找到了一个窗口句柄

却没有办法利用句柄使用SendMessage来操作控件

原因是什麽呢?:(
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-8 22:21:18 | 显示全部楼层
  1. #include "..\CommonFiles\CmnHdr.h"     /* This is my head file. */
  2. #include <Windowsx.h>
  3. #include <tchar.h>
  4. #include "Resource.h"


  5. ///////////////////////////////////////////////////////////////////////////////


  6. #define ESM_POKECODEANDLOOKUP    (WM_USER + 100)
  7. const TCHAR g_szAppName[] = TEXT("Error Show");


  8. ///////////////////////////////////////////////////////////////////////////////


  9. BOOL Dlg_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) {

  10.    chSETDLGICONS(hwnd, IDI_ERRORSHOW);

  11.    // Don't accept error codes more than 5 digits long
  12.    Edit_LimitText(GetDlgItem(hwnd, IDC_ERRORCODE), 5);

  13.    // Look up the command-line passed error number
  14.    SendMessage(hwnd, ESM_POKECODEANDLOOKUP, lParam, 0);
  15.    return(TRUE);
  16. }


  17. ///////////////////////////////////////////////////////////////////////////////


  18. void Dlg_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) {

  19.    switch (id) {

  20.    case IDCANCEL:
  21.       EndDialog(hwnd, id);
  22.       break;

  23.    case IDC_ALWAYSONTOP:
  24.       SetWindowPos(hwnd, IsDlgButtonChecked(hwnd, IDC_ALWAYSONTOP)
  25.          ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  26.       break;

  27.    case IDC_ERRORCODE:
  28.       EnableWindow(GetDlgItem(hwnd, IDOK), Edit_GetTextLength(hwndCtl) > 0);
  29.       break;

  30.    case IDOK:
  31.       // Get the error code
  32.       DWORD dwError = GetDlgItemInt(hwnd, IDC_ERRORCODE, NULL, FALSE);

  33.       HLOCAL hlocal = NULL;   // Buffer that gets the error message string

  34.       // Use the default system locale since we look for Windows messages.
  35.       // Note: this MAKELANGID combination has 0 as value
  36.       DWORD systemLocale = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);

  37.       // Get the error code's textual description
  38.       BOOL fOk = FormatMessage(
  39.          FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
  40.          FORMAT_MESSAGE_ALLOCATE_BUFFER,
  41.          NULL, dwError, systemLocale,
  42.          (PTSTR) &hlocal, 0, NULL);

  43.       if (!fOk) {
  44.          // Is it a network-related error?
  45.          HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL,
  46.             DONT_RESOLVE_DLL_REFERENCES);

  47.          if (hDll != NULL) {
  48.             fOk = FormatMessage(
  49.                FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS |
  50.                FORMAT_MESSAGE_ALLOCATE_BUFFER,
  51.                hDll, dwError, systemLocale,
  52.                (PTSTR) &hlocal, 0, NULL);
  53.             FreeLibrary(hDll);
  54.          }
  55.       }

  56.       if (fOk && (hlocal != NULL)) {
  57.          SetDlgItemText(hwnd, IDC_ERRORTEXT, (PCTSTR) LocalLock(hlocal));
  58.          LocalFree(hlocal);
  59.       } else {
  60.          SetDlgItemText(hwnd, IDC_ERRORTEXT,
  61.             TEXT("No text found for this error number."));
  62.       }

  63.       break;
  64.    }
  65. }


  66. ///////////////////////////////////////////////////////////////////////////////


  67. INT_PTR WINAPI Dlg_Proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

  68.    switch (uMsg) {
  69.       chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Dlg_OnInitDialog);
  70.       chHANDLE_DLGMSG(hwnd, WM_COMMAND,    Dlg_OnCommand);

  71.    case ESM_POKECODEANDLOOKUP:
  72.       SetDlgItemInt(hwnd, IDC_ERRORCODE, (UINT) wParam, FALSE);
  73.       FORWARD_WM_COMMAND(hwnd, IDOK, GetDlgItem(hwnd, IDOK), BN_CLICKED,
  74.          PostMessage);
  75.       SetForegroundWindow(hwnd);
  76.       break;
  77.    }

  78.    return(FALSE);
  79. }


  80. ///////////////////////////////////////////////////////////////////////////////


  81. int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) {

  82.    HWND hwnd = FindWindow(TEXT("#32770"), TEXT("Error Show"));
  83.    if (IsWindow(hwnd)) {
  84.       // An instance is already running, activate it and send it the new #
  85.       SendMessage(hwnd, ESM_POKECODEANDLOOKUP, _ttoi(pszCmdLine), 0);
  86.    } else {
  87.       DialogBoxParam(hinstExe, MAKEINTRESOURCE(IDD_ERRORSHOW),
  88.          NULL, Dlg_Proc, _ttoi(pszCmdLine));
  89.    }
  90.    return(0);
  91. }


  92. //////////////////////////////// End of File //////////////////////////////////
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-8 22:22:37 | 显示全部楼层
以上例子的WinMain函数供你参考
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-11 03:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表