C++ 添加文本框问题

toptrown 2010-05-29 07:11:27
//在一个窗体上添加文本框怎么不行啊,请帮我看看那出了问题,工具为DVE C++

#include <windows.h>
#define IDC_EDITBOX 110 ///<Identifier For Edit Control
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);

/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}

/* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
CreateWindowEx(Null,"Edit",NULL,WS_CHILD or WS_VISIBLE or WS_BORDER or WS_GROUP or WS_TABSTOP,700,70,180,40,
IDC_EDITBOX,1,hThisInstance,NULL);

break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}
...全文
957 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForestDB 2010-06-08
  • 打赏
  • 举报
回复
建议LZ多看看MSDN先,还有Programming Windows。
sugar65 2010-06-07
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 forestdb 的回复:]
帮顶。
[/Quote]

谢谢, 假如阁下能够解决楼主提的问题就更好了...21楼这问题太神奇了
ForestDB 2010-06-07
  • 打赏
  • 举报
回复
帮顶。
sugar65 2010-06-04
  • 打赏
  • 举报
回复
不可能啊!
Dev-C++4.9.9.2??
XP系统??
IntelCPU??

即使把SetWindowPos(...)放到WM_SIZE消息下, 也应该能居中啊, 我试的时候就行啊..
sugar65 2010-06-04
  • 打赏
  • 举报
回复
可为什么我的机器上运行, 结果和你说的不一样呢?
假如怀疑SetFocus(...)没有正常工作的话, 可以看看它的返回值.
HWND SetFocus(
HWND hWnd // handle of window to receive focus
);

Parameters
hWnd
Identifies the window that will receive the keyboard input. If this parameter is NULL, keystrokes are ignored.

Return Values
If the function succeeds, the return value is the handle of the window that previously had the keyboard focus. If the hWnd parameter is invalid or the window is not associated with the calling thread's message queue, the return value is NULL.


// 处理的窗口接收到的焦点
HWND hReturn = SetFocus(
GetDlgItem(hwnd, IDC_MAIN_TEXT)
);
{
char szText[100];
wsprintf (szText, "hReturn = %p, hItemWnd = %p, hWnd = %p", hReturn, GetDlgItem(hwnd, IDC_MAIN_TEXT), hwnd);
SetWindowText (hwnd, szText);
}
}


使用如上的语句可以将返回值, 文本框句柄, 主窗口句柄同时显示到标题栏
toptrown 2010-06-04
  • 打赏
  • 举报
回复
怎么新建文本框不能让它居中啊
sugar65 2010-06-03
  • 打赏
  • 举报
回复
WM_SIZE消息的来源至少有两个, 一个是主窗口刚建立时, 一个是用户或其他程序改变了主窗口的大小时.
把能够令文本框居中的SetWindowPos (...)加到WM_SIZE消息下, 就可以让文本框居中, 即使用户调整主窗体的大小, 文本框也能自动重新居中.

SetFocus(...)不要加在WM_SIZE消息下, 而要加在WM_CREATE消息中创建文本框之后.
toptrown 2010-06-03
  • 打赏
  • 举报
回复
//我现在的代码请您过目

#include <windows.h>
#define IDC_MAIN_TEXT 1001
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
static HINSTANCE g_hInst = NULL;

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */

g_hInst = hThisInstance;

/* The Window structure */
wincl.hInstance = g_hInst;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);//COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
CW_USEDEFAULT, /* The programs width */
CW_USEDEFAULT, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
g_hInst, /* Program Instance handler */
NULL /* No Window Creation data */
);

/* 使窗口显示在屏幕上并最大化 */
ShowWindow (hwnd, SW_SHOWMAXIMIZED); //SW_SHOWMAXIMIZED最大化//nFunsterStil);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}

/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:{
//在窗口中新建一个文本框
CreateWindow("EDIT", "",
WS_CHILD | WS_VISIBLE | ES_WANTRETURN | ES_PASSWORD | ES_CENTER,// | WS_TABSTOP | WS_GROUP | WS_BORDER | ES_MULTILINE,
100, 100, 90, 24, // 文本框的坐标及大小
hwnd, (HMENU)IDC_MAIN_TEXT, g_hInst, NULL);
}
break;
case WM_SIZE:{
int nWidth = LOWORD(lParam); // 客户区的宽度
int nHeight = HIWORD(lParam); // 客户区的高度

SetWindowPos( // 设置文本框居中
GetDlgItem(hwnd, IDC_MAIN_TEXT), // 窗口句柄
HWND_TOP, // 窗口位置处理命令
nWidth / 2 - 45, // 左边界坐标
nHeight / 2 - 12, // 上边界坐标
90, 24, // 宽度 高度
SWP_SHOWWINDOW // 窗口尺寸和定位的标志,显示窗口
);
// 处理的窗口接收到的焦点
SetFocus(
GetDlgItem(hwnd, IDC_MAIN_TEXT)
);
}
break;
case WM_DESTROY:{
PostQuitMessage (0); // 发送一个“WM_QUIT”消息到消息队列
}
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}
toptrown 2010-06-03
  • 打赏
  • 举报
回复
谢谢你一直以来对我的帮助
将SetFocus加到WM_CREATE里后经过WM_SIZE改变窗口大小文本框就失去焦点了
让窗口最大化可用此语句ShowWindow (hwnd, SW_SHOWMAXIMIZED)
但建文本框是在建窗口之前
将SetFocus加到WM_CREATE后就没有文本框了
toptrown 2010-06-03
  • 打赏
  • 举报
回复
错了,应该是
因为我要让窗口启动时文本框居中,所以我把SetFocus()加到了WM_SIZE里。
但为什么创建文本框时不能居中呀
sugar65 2010-06-03
  • 打赏
  • 举报
回复
下面是我整理出来的完整程序, 具体什么函数放在什么地方, 自己看就好了

#include <windows.h>
#define IDC_EDITBOX 110 ///<Identifier For Edit Control
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"密码登陆界面", /* Title Text */
WS_OVERLAPPEDWINDOW | WS_MAXIMIZE, // WS_MAXIMIZE标志使得窗口最大化
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);

/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
if (messages.hwnd == GetDlgItem (hwnd, IDC_EDITBOX) && messages.message == WM_KEYDOWN && (int) messages.wParam == VK_RETURN)
{
//ShowWindow (hwnd, SW_HIDE);
DestroyWindow (hwnd);
}
else
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
}

hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"主界面", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);

/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}

/* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
CreateWindowEx(
NULL,
"Edit",
0,
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_GROUP | WS_TABSTOP | ES_PASSWORD | ES_WANTRETURN,
700,70,180,40,
hwnd,
(HMENU)IDC_EDITBOX,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
SetFocus(GetDlgItem(hwnd,IDC_EDITBOX));
break;

case WM_SIZE:
{ // 为了定义局部变量, 防止局部变量跳跃标签而加的一组大括号
int nWidth = LOWORD(lParam); // width of client area
int nHeight = HIWORD(lParam); // height of client area

SetWindowPos(
GetDlgItem(hwnd,IDC_EDITBOX), // handle of window
HWND_TOP, // placement-order handle
nWidth / 2 - 40, // horizontal position
nHeight / 2 - 10, // vertical position
80, // width
20, // height
SWP_SHOWWINDOW // window-positioning flags
);
}
break;

case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;

default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}
toptrown 2010-06-02
  • 打赏
  • 举报
回复
因为我要让窗口启动时最大化,所以我把它加到了WM_SIZE里。
但为什么创建窗口时不能最大化呀
sugar65 2010-06-01
  • 打赏
  • 举报
回复
忘了一件事, 汗..

当窗口启动时文本框即有输入焦点, 可以在
WM_CREATE消息中, 创建文本框之后, 使用如下的API:
HWND SetFocus(
HWND hWnd // handle of window to receive focus
);

将文本框的句柄, 而不是主窗体的句柄, 作为 HWND hWnd 参数
toptrown 2010-05-31
  • 打赏
  • 举报
回复
如何将文本框设为密码框
toptrown 2010-05-31
  • 打赏
  • 举报
回复
当焦点在文本框时,如何按回车键退出(关闭本窗口)
sugar65 2010-05-31
  • 打赏
  • 举报
回复
不好意思, 那句写错了, 应该是

ShowWindow (hwnd, SW_HIDE);

另外, 当窗口隐藏掉之后, 假如你不处理其他的细节, 你就看不到窗体了, 但程序还在继续走, 所以你需要点:
菜单>运行>重启程序
或者干脆用任务管理器来结束程序
sugar65 2010-05-31
  • 打赏
  • 举报
回复
我想不出别的办法, 我能想得到的就只有改写消息循环了.

如果我没猜错的话, 你是想做个密码登陆界面吧.

把原来的消息循环改成如下:

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
if (messages.hwnd == GetDlgItem (hwnd, 1) && messages.message == WM_KEYDOWN && (int) messages.wParam == VK_RETURN)
{
ShowWindow (hwnd, SH_HIDE);
}
else
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
}


只不过, 当你隐藏窗口之后, 你需要处理一下其他的细节.

我不可能一直帮你(当然其他人也可以给你帮助), 所以你需要看一些关于 Windows 的消息机制以及介绍 Win32API 的资料.
遇到不会的函数, 可以百度一下.
toptrown 2010-05-31
  • 打赏
  • 举报
回复
如何按回车键退出(关闭本窗口),当窗口启动时文本框即有输入焦点
sugar65 2010-05-31
  • 打赏
  • 举报
回复
CreateWindowEx的第四个参数里添上
| ES_PASSWORD
就成密码框了
sugar65 2010-05-31
  • 打赏
  • 举报
回复
The following edit control styles (in the EDIT class) can be specified in the dwStyle parameter:

Style Meaning
ES_AUTOHSCROLL Automatically scrolls text to the right by 10 characters when the user types a character at the end of the line. When the user presses the ENTER key, the control scrolls all text back to position zero.
ES_AUTOVSCROLL Automatically scrolls text up one page when the user presses the ENTER key on the last line.
ES_CENTER Centers text in a multiline edit control.
ES_LEFT Left-aligns text.
ES_LOWERCASE Converts all characters to lowercase as they are typed into the edit control.
ES_MULTILINE Designates a multiline edit control. The default is single-line edit control.
When the multiline edit control is in a dialog box, the default response to pressing the ENTER key is to activate the default button. To use the ENTER key as a carriage return, use the ES_WANTRETURN style.
When the multiline edit control is not in a dialog box and the ES_AUTOVSCROLL style is specified, the edit control shows as many lines as possible and scrolls vertically when the user presses the ENTER key. If you do not specify ES_AUTOVSCROLL, the edit control shows as many lines as possible and beeps if the user presses the ENTER key when no more lines can be displayed.
If you specify the ES_AUTOHSCROLL style, the multiline edit control automatically scrolls horizontally when the caret goes past the right edge of the control. To start a new line, the user must press the ENTER key. If you do not specify ES_AUTOHSCROLL, the control automatically wraps words to the beginning of the next line when necessary. A new line is also started if the user presses the ENTER key. The window size determines the position of the word wrap. If the window size changes, the word wrapping position changes and the text is redisplayed.
Multiline edit controls can have scroll bars. An edit control with scroll bars processes its own scroll bar messages. Note that edit controls without scroll bars scroll as described in the previous paragraphs and process any scroll messages sent by the parent window.
ES_NOHIDESEL Negates the default behavior for an edit control. The default behavior hides the selection when the control loses the input focus and inverts the selection when the control receives the input focus. If you specify ES_NOHIDESEL, the selected text is inverted, even if the control does not have the focus.
ES_NUMBER Allows only digits to be entered into the edit control.
ES_OEMCONVERT Converts text entered in the edit control. The text is converted from the Windows character set to the OEM character set and then back to the Windows set. This ensures proper character conversion when the application calls the CharToOem function to convert a Windows string in the edit control to OEM characters. This style is most useful for edit controls that contain filenames.
ES_PASSWORD Displays an asterisk (*) for each character typed into the edit control. You can use the EM_SETPASSWORDCHAR message to change the character that is displayed.
ES_READONLY Prevents the user from typing or editing text in the edit control.
ES_RIGHT Right-aligns text in a multiline edit control.
ES_UPPERCASE Converts all characters to uppercase as they are typed into the edit control.
ES_WANTRETURN Specifies that a carriage return be inserted when the user presses the ENTER key while entering text into a multiline edit control in a dialog box. If you do not specify this style, pressing the ENTER key has the same effect as pressing the dialog box's default push button. This style has no effect on a single-line edit control.
加载更多回复(8)

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧