主题:关于GetWindowText()函数的问题
sczzxjy
[专家分:360] 发布于 2007-01-23 17:43:00
我先用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个回复)
沙发
zy1121 [专家分:7950] 发布于 2007-01-24 02:23:00
将hwndButton声明定义为
static HWND hwndButton;
板凳
sczzxjy [专家分:360] 发布于 2007-01-24 09:57:00
是申明为static HWND hwndButton的;
3 楼
zy1121 [专家分:7950] 发布于 2007-01-24 12:25:00
是编译器说没初始化吗?
应该不会的呀~~
你是如何做的?
4 楼
sczzxjy [专家分:360] 发布于 2007-01-24 14:11:00
我把程序贴出来给你看看吧:
case WM_CREATE:
GetClientRect(hWnd, &wRect); // 将程序窗口区域保存到结构变量wRect
// 建立复选框
hButtonCi = CreateWindow(TEXT("BUTTON"), TEXT("圆"),
BS_CHECKBOX | WS_CHILD | WS_VISIBLE,
wRect.left + 50,
wRect.bottom - 80,
75,
25,
hWnd,
(HMENU)0,
hInst,
NULL);
hButtonSq = CreateWindow(TEXT("BUTTON"), TEXT("矩形"),
BS_CHECKBOX | WS_CHILD | WS_VISIBLE,
wRect.left + 50,
wRect.bottom - 40,
75,
25,
hWnd,
(HMENU)1,
hInst,
NULL);
break;
case WM_COMMAND:
if(LOWORD(lParam) == (long)hButtonCi)
{
// 读取复选框状态
nCheck = (WORD)SendMessage(hButtonCi, BM_GETCHECK, 0, 0L);
if(nCheck == TRUE)
SendMessage(hButtonCi, BM_SETCHECK, 0, 0L);
else
SendMessage(hButtonCi, BM_SETCHECK, 1, 0L);
}
else if(LOWORD(lParam) == (long)hButtonSq)
{
// 读取复选框状态
nCheck = (WORD)SendMessage(hButtonSq, BM_GETCHECK, 0, 0L);
if(nCheck == TRUE)
SendMessage(hButtonCi, BM_SETCHECK, 0, 0L);
else
SendMessage(hButtonSq, BM_SETCHECK, 1, 0L);
}
InvalidateRect(hWnd, NULL, TRUE);
break;
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_SIZE:
xc = LOWORD(lParam)/2;
yc = HIWORD(lParam)/2;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
//检查复选框以决定是否绘图
nCheck = (WORD)SendMessage(hButtonCi, BM_GETCHECK, 0, 0L);
if(nCheck == TRUE)
Ellipse(hdc, xc - 100, yc - 100, xc + 100 , yc + 100);
nCheck = (WORD)SendMessage(hButtonSq, BM_GETCHECK, 0, 0L);
if(nCheck == TRUE)
{
MoveToEx(hdc, xc - 100, yc - 100 ,(LPPOINT)NULL);
LineTo(hdc, xc + 100, yc - 100);
LineTo(hdc, xc + 100, yc + 100);
LineTo(hdc, xc - 100, yc + 100);
LineTo(hdc, xc - 100, yc - 100);
}
EndPaint(hWnd, &ps);
break;
在后面的WM_SIZE中,控件句柄的值根本没有(与前面得到的句柄不一样),不知道是为什么?
5 楼
zy1121 [专家分:7950] 发布于 2007-01-24 15:23:00
An application should not call BeginPaint except in response to a WM_PAINT message
你不应该在WM_SIZE消息时调用BeginPaint
但应该不是致使原因~~
是编译器说没初始化吗?
你干脆再发完整点的代码,我有时间时给你运行一下看看~~
6 楼
sczzxjy [专家分:360] 发布于 2007-01-24 15:45:00
好呢:完整的程序如下:
// ch8-1.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "ch8-1.h"
#define MAX_LOADSTRING 100
#define mLen 100
// 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_CH81, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CH81));
// 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_CH81));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_CH81);
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
//
//
7 楼
sczzxjy [专家分:360] 发布于 2007-01-24 15:46:00
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
// 定义函数变量
static HWND hButtonExt; // 结束按钮句柄
static HWND hButtonCls; // 清除按钮句柄
static HWND hEditSou; // 编辑控件窗口句柄
static HWND hEditDst;
RECT wRect; // 窗口区域
static int x, y, bWidth, bHeight;
wchar_t txtBuffer[mLen];
// 初始化变量
bWidth = 50;
bHeight = 30;
switch (message)
{
case WM_CREATE:
GetClientRect(hWnd, &wRect);
x = (wRect.right - bWidth)*2/5; //按钮的x位置
y = (wRect.bottom - bHeight)*5/6; //按钮的y位置
// 建立结束按钮 窗口ID为0
hButtonExt = CreateWindow(TEXT("button"), TEXT("结束"), WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
x, y, bWidth, bHeight, hWnd, (HMENU)0,
hInst, NULL);
x = (wRect.right - bWidth)*3/5; //按钮的x位置
y = (wRect.bottom - bHeight)*5/6; //按钮的y位置
// 建立清除按钮 窗口ID为1
hButtonCls = CreateWindow(TEXT("BUTTON"), TEXT("清除"), WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
x, y, bWidth, bHeight, hWnd, (HMENU)1,
hInst, NULL);
// 建立源编辑控件 窗口ID为2
hEditSou = CreateWindow(TEXT("edit"), NULL,
WS_CHILD | WS_VISIBLE |ES_MULTILINE | WS_BORDER| WS_VSCROLL | ES_AUTOVSCROLL,
20, 20, wRect.right - 40, 50,
hWnd, (HMENU)2,
hInst, NULL);
8 楼
sczzxjy [专家分:360] 发布于 2007-01-24 15:46:00
// 建立目的编辑控件 窗口ID为3
hEditDst = CreateWindow(TEXT("edit"), NULL,
WS_CHILD | WS_VISIBLE |ES_MULTILINE | WS_BORDER| WS_VSCROLL | ES_AUTOVSCROLL,
20, 120, wRect.right - 40,50,
hWnd, (HMENU)3,
hInst, NULL);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
if(LOWORD(lParam) == (LPARAM)hButtonExt) // 看是否按结束按钮
PostQuitMessage(0); //程序结束
else if (LOWORD(lParam) == (LPARAM)hButtonCls)
{
GetWindowText(hEditSou, (LPWSTR)txtBuffer, mLen); //得到源编辑窗口中的字符
//SetWindowText(hEditDst, (LPWSTR)txtBuffer); // 将字符数组的数据写入目的编辑窗口
hdc = GetDC(hWnd);
// 在主窗口输出字符串
TextOut(hdc, 100, 250, txtBuffer, lstrlen(txtBuffer));
ReleaseDC(hWnd, hdc);
// 如果是密码检测,程序代码为:
// hdc = GetDC(hWnd);
// GetWindowText(hEditSou, (LPWSTR)txtBuffer, mLen); // 获取编辑框中的字符
// if (!strcmp(txtBuffer, "qouling") // 与预定义的密码比较
//TextOut(hdc, 100, 250, "欢迎使用本系统", 14);
// else
// TextOut(hdc, 100, 250, "密码错误", 8);
ReleaseDC(hWnd, hdc);
}
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;
}
9 楼
zy1121 [专家分:7950] 发布于 2007-01-25 01:11:00
[quote]
if(LOWORD(lParam) == (LPARAM)hButtonExt) // 看是否按结束按钮
PostQuitMessage(0); //程序结束
else if (LOWORD(lParam) == (LPARAM)hButtonCls)
[/quote]
改成这样就行了,并没有你说的 "没有初始化"
if((HWND)lParam == hButtonExt)
PostQuitMessage(0);
else if ((HWND)lParam == hButtonCls)
如果WM_COMMAND来自控件,则lParam是控件句柄,LOWORD(lParam)什么都不是
10 楼
sczzxjy [专家分:360] 发布于 2007-01-25 09:03:00
非常感谢,你帮我解决一个制约我学习的大问题,还有一个程序,不知哥们可不可以帮我再看一看?
我来回复