回 帖 发 新 帖 刷新版面

主题:关于GetWindowText()函数的问题

我先用hwndButton = CreateWindow(TEXT("button"), TEXT("显示"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,x,y, bWidth, bHeight, hWnd, 0,hInst, NULL);
创建了一个命令按钮,然后在WM_SIZE中用
n = GetWindowText(hwndButton, (LPWSTR)txtButton, nMaxlen);         // 获取指定窗口的标题
但为什么在WM_CREATE里创建的句柄,在WM_SIZE里却说没有初始化呢?
这种子窗口的句柄应该如何传递呢?请高手指点一下

回复列表 (共16个回复)

11 楼

#include "stdafx.h"
#include "ch7.h"

#include <windows.h>
#include <string.h>

#define MAX_LOADSTRING 100
#define nMaxlen        80

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                    // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

     // TODO: Place code here.
    MSG msg;
    HACCEL hAccelTable;
    hInst = hInstance;                       // 获得当前程序实例的句柄

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_CH7, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CH7));

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CH7));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = MAKEINTRESOURCE(IDC_CH7);
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, TEXT("命令按钮设计"), WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND    - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY    - post a quit message and return
//
//

12 楼

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    HWND hwndButton;               // 命令按钮的句柄
    static int bWidth, bHeight;    // 定义命令按钮的宽与高
    static int x,y;                // 定义命令按钮的位置
    RECT    wRect;                 // 定义窗口区域
    char    txtButton[nMaxlen];     // 标题
    int n;

    bWidth = 100;                  // 设置命令按钮的宽与高
    bHeight = 30;

    switch (message)
    {
    case WM_CREATE:
        GetClientRect(hWnd, &wRect);           // 获取程序窗口的长与高
        x = (wRect.right - bWidth)/2;             // 命令按钮在x轴的位置
        y = (wRect.bottom - bHeight)*4/5;         // 命令按钮在y轴的位置
        hwndButton = CreateWindow(TEXT("button"), TEXT("显示"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            x,y, bWidth, bHeight, hWnd, NULL,
            hInst, NULL);
        return 0;

    case WM_SIZE:
        GetClientRect(hWnd, &wRect);           // 获取程序窗口的长与高
        x = (wRect.right - bWidth)/2;             // 命令按钮在x轴的位置
        y = (wRect.bottom - bHeight)*4/5;         // 命令按钮在y轴的位置
        MoveWindow(hwndButton, x, y, bWidth, bHeight, TRUE);          // 重绘指定的子窗口,现为命令按钮
        return 0;

    case WM_COMMAND:
        n = GetWindowText(hwndButton, (LPWSTR)txtButton, nMaxlen);         // 获取指定窗口的标题
        if (!strcmp(txtButton, "显示"))
        {
            SetWindowText(hwndButton, TEXT("删除"));                  // 设置指定窗口的标题
            hdc = GetDC(hWnd);
            TextOut(hdc, 0,0, TEXT("Hi! Button"), 10);
            ReleaseDC(hWnd, hdc);
        }
        else
        {
            SetWindowText(hwndButton, TEXT("显示"));
            InvalidateRect(hWnd, NULL, TRUE);                   // 刷新屏幕,显示字符串
        }

        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

13 楼

char    txtButton[nMaxlen];
你用的是char而你的程序是宽字符版的,
n = GetWindowText(hwndButton, (LPWSTR)txtButton, nMaxlen);
这里获得的标题就会有误,

你最好是用TCHAR,系统会跟据是否定义了UNICODE自动为你选择宽字符或ASCII等,

你没有说这次有什么问题,所以只能说这么多.

14 楼

使用TCHAR txtButton[]时,后面在strcmp中程序会报错,错误为:
.\ch7.cpp(175) : error C2664: 'strcmp' : cannot convert parameter 1 from 'TCHAR [80]' to 'const char *'
不好意思,这个程序原来的问题是提示
 MoveWindow(hwndButton, x, y, bWidth, bHeight, TRUE);  // 重绘指定的子窗口,现为命令按钮
这一句中的hwndButton没有初始化,因此,改变窗口大小时,而没有重绘按钮,
第二个问题就是,strcmp语句没有执行,好象是参数的数据类型不对。

15 楼

HWND hwndButton改为static HWND hwndButton;  
strcmp换成lstrcmp

16 楼

干吗贴那么多代码啊,细节性的问题一般自己慢慢解决就行了啊。问个程序结构,怎么设计运行更快了什么的。

我来回复

您尚未登录,请登录后再回复。点此登录或注册