I have recently begun learning the Win32 API using this tutorial:
http://www.winprog.org/tutorial/ (though I'm using C++, not C as in the tutorial) I'm currently experimenting with the "edit box"-function where I'm trying to compare the text written in the edit box with another line of characters. Code:
#define IDC_MAIN_EDIT 101
Code:
case WM_CREATE:
{
HFONT hfDefault;
HWND hEdit;
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
if(hEdit == NULL)
MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
hfDefault = GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
}
break;
case WM_SIZE:
{
HWND hEdit;
RECT rcClient;
GetClientRect(hwnd, &rcClient);
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
}
break;
Code:
bool comparison (HWND hEdit) {
LPWSTR pszText;
DWORD dwTextLength;
DWORD dwBufferSize;
dwTextLe开发者_开发百科ngth = GetWindowTextLength(hEdit);
dwBufferSize = dwTextLength + 1;
GetWindowText(hEdit, pszText, dwBufferSize);
if(pszText == TEXT("3")) {
return true;
}
else {
return false;
}
}
The problem when I call the "comparison"-function is that pszText and hEdit aren't initialized. I get why pszText isn't and I've tried using the new/delete to fix it, but I don't get it to work. I have no clue about hEdit. Am I perhaps using the GetWindowText-function wrong? Warnings Code:
warning C4700: uninitialized local variable 'pszText' used warning C4700: uninitialized local variable 'hEdit' used
Run-Time Check Failure (appear when I'm using the function, and this is just one of them) Code:
Run-Time Check Failure #3 - The variable 'hEdit' is being used without being initializ
pszText
is a pointer type. So you'need to allocate memory to it before you use it.
Do this:
wchar_t *pszText = new wchar_t[size]; //calculate or guess `size`
Yes, you must allocate a buffer for GetWindowText() to store its data inside. The "LP" in LPWSTR means that the variable is actually a pointer and not an allocated object.
This style is called "Hungarian notation" and in my own personal opinion, its only use is to hide away fundamental C syntax from the programmer, in order to create more bugs.
Look into the very important concept of scope. refer this page http://www.cplusplus.com/doc/tutorial/variables/
c++ Code:
case WM_CREATE:
{
HFONT hfDefault;
HWND hEdit;
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
if(hEdit == NULL)
MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
hfDefault = GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
}
The variable hEdit only exists for the duration of the block in which it's declared, which is within the {}s for the WM_CREATE case.
精彩评论