你好我有以下win32程序,并且我有一个EDITTEXT
控件没有显示在屏幕上。意图在主窗口上绘制两个EDITTEXT
控件,但只有一个显示,为什么会这样?
完整代码
#include <windows.h>
#include <iostream>
#include "resource.h"
using namespace std;
const int BUFFERMAX = 512;
char server_ip[BUFFERMAX];
int port;
const char windclassname[] = "windowClass";
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
INT_PTR CALLBACK AboutDialog(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
INT_PTR CALLBACK ConnectWin(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
WNDCLASSEX parent_window;
HWND hwnd;
MSG msg;
parent_window.cbSize = sizeof(WNDCLASSEX);
parent_window.style = 0;
parent_window.lpfnWndProc = WndProc;
parent_window.cbClsExtra = 0;
parent_window.cbWndExtra = 0;
parent_window.hInstance = hInstance;
parent_window.hIcon = LoadIcon(NULL,IDI_APPLICATION);
parent_window.hCursor = LoadCursor(NULL,IDC_ARROW);
parent_window.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
parent_window.lpszMenuName = MAKEINTRESOURCE(ID_MENU);
parent_window.lpszClassName = windclassname;
parent_window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
//Registering the Class for use.
if ( !RegisterClassEx(&parent_window)){
MessageBox(NULL,"Could not Register WindowClass","Error",MB_OK);
return 0;
}
//Creating the window
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,windclassname,"UI test",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,250,350,NULL,NULL,hInstance,NULL);
if ( hwnd == NULL ){
MessageBox(NULL,"Could not create window","Error",MB_OK);
return 0;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while ( GetMessage(&msg,NULL,0,0) > 0 ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
switch (msg){
case WM_CREATE:
HWND chatbox_cntrl;
HWND message_text;
chatbox_cntrl = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0,0,240,260,hwnd,(HMENU)CHATBOX,GetModuleHandle(NULL),NULL);
message_text = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",
WS_CHILD,0,270,240,10,hwnd,(HMENU)MESSAGETEXT,GetModuleHandle(NULL),NULL);
break;
case WM_COMMAND:
switch(LOWORD(wParam)){
case ID_MABOUT_INFO:
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_MABOUT_INFO), hwnd,AboutDialog);
break;
case ID_MCONTROLS_CONNECT:
int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_MCONTROLS_CONNECT), hwnd,ConnectWin);
break;
case ID_MCONTROLS_EXIT:
DestroyWindow(hwnd);
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
INT_PTR CALLBACK AboutDialog(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
switch (msg){
case WM_CLOSE:
EndDialog(hwnd,WM_CLOSE);
break;
default:
return false;
}
return true;
}
INT_PTR CALLBACK ConnectWin(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
switch(msg){
case WM_COMMAND:
switch(wParam){
case CONNECT_BUTTON:
int len = GetWindowTextLength(GetDlgItem(hwnd,EDITTEXT_CONNECT));
int recv = GetDlgItemText(hwnd,EDITTEXT_CONNECT,server_ip,len+1);
port = GetDlgItemInt(hwnd,EDITTEXT_PORT,NULL,false);
EndDialog(hwnd,CONNECT_BUTTON);
break;
}
break;
case WM_CLOSE:
EndDialog(hwnd,WM_CLOSE);
break;
default:
return false;
}
return true;
}
这是两个控件。
case WM_CREATE:
HWND chatbox_cntrl;
HWND message_text;
chatbox_cntrl = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0,0,240,260,hwnd,(HMENU)CHATBOX,GetModuleHandle(NULL),NULL);
message_text = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",
WS_CHILD,0,270,240,10,hwnd,(HMENU)MESSAGETEXT,GetModuleHandle(NULL),NULL);
break;
答案 0 :(得分:4)
消息文本窗口与第一个没有相同的样式标志。具体来说,它缺少WS_VISIBLE
。这可能是你的问题。
答案 1 :(得分:0)
在创建编辑窗口之前,您是否致电InitCommonControlsEx()
了? CreateWindow()
会返回有效的HWND
吗?
INITCOMMONCONTROLSEX icc;
icc.dwSize = sizeof(icc);
// Load one of the intrinsic User32 control classes.
// The user controls include button, edit, static,
// listbox, combobox, and scrollbar.
dwICC = ICC_STANDARD_CLASSES;
::InitCommonControlsEx(&icc);
答案 2 :(得分:0)
在使用InitCommonControlsEx加载控件之前是否已初始化控件?试试这个代码吧。
int InitControls() {
INITCOMMONCONTROLSEX iccx;
iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccx.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&iccx);
}
请记住包含头文件commctrl.h和库comctl32.dll
祝你好运!