为什么修改了部分代码,连窗口都不能显示!
//TCHAR ch = 'A' + i;//lstrcpy(strings , &ch);
strings[0] = 'A'+i;
将上述代码用注释的代码替换,窗口不显示出来,但是调试的时候还能显示出来,求原因!!
代码如下
程序代码:#include <Windows.h>
#define IDC_STATIC 1
#define IDC_LISTBOX 2
LRESULT CALLBACK wndprog(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
TCHAR szappname[] = TEXT("mywindow");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc = wndprog;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szappname;
if ( !RegisterClass(&wndclass))
{
MessageBox(NULL,L"creat window failed!",szappname,MB_ICONERROR);
return 0 ;
}
hwnd = CreateWindow(szappname,TEXT("This is my window !"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL, NULL,
hInstance, NULL);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
while( GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK wndprog(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
static HWND hlistbox, hstatic;
static int xclinet, yclient;
static int xchar, ychar;
static TCHAR *strings={NULL};
static TCHAR *szbuffer = NULL,*temp = NULL,*save = NULL;
int index, length,priorlength=0;
switch (message)
{
case WM_CREATE:
{
hstatic = CreateWindow(TEXT("static"),TEXT("nothing"),
WS_CHILD|WS_VISIBLE|WS_BORDER|SS_LEFT,
0,0,0,0,hwnd,(HMENU)IDC_STATIC,(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
hlistbox = CreateWindow(TEXT("listbox"),NULL,
WS_CHILD|WS_VISIBLE|LBS_STANDARD,
0,0,0,0,hwnd,(HMENU)IDC_LISTBOX,(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
for ( int i = 0; i < 26; i++)
{
strings = (TCHAR *)malloc(sizeof(TCHAR) * 2); //保存一个字符
//TCHAR ch = 'A' + i;
//lstrcpy(strings , &ch);
strings[0] = 'A'+i;
strings[1] = '\0';
SendMessage(hlistbox,LB_ADDSTRING,0,(LPARAM)strings);
}
SendMessage(hlistbox,LB_SETCURSEL,2,0);
}
return 0;
case WM_SIZE:
{
xchar = LOWORD(GetDialogBaseUnits());
ychar = HIWORD(GetDialogBaseUnits());
xclinet = LOWORD(lparam);
yclient = HIWORD(lparam);
MoveWindow(hstatic,xclinet/4,yclient/8,xclinet/2,ychar,TRUE);
MoveWindow(hlistbox,xclinet/4,yclient/4,xclinet/2,ychar*10,TRUE);
}
return 0 ;
case WM_COMMAND:
{
if ( LOWORD(wparam) == IDC_LISTBOX && HIWORD(wparam) == LBN_SELCHANGE )
{
index = SendMessage(hlistbox, LB_GETCURSEL,0,0);
length = SendMessage(hlistbox, LB_GETTEXTLEN,index,0)+1 ;
save = (TCHAR *)malloc(sizeof(TCHAR) * length);
SendMessage(hlistbox,LB_GETTEXT,index,(LPARAM)save);
// 原来的静态我文本还有字符
if ( szbuffer)
{
length += lstrlen(szbuffer);
temp = (TCHAR *)malloc(sizeof(TCHAR) * (lstrlen(szbuffer) + 1));
lstrcpy(temp,szbuffer);
free(szbuffer);
}
szbuffer = (TCHAR *)malloc( sizeof(TCHAR) * length);
if ( temp)
{
lstrcpy(szbuffer,temp);
free(temp);
}
else //使用lstrcat 或则 strcat ,若接收器为空,则将第一位设成'\0',
szbuffer[0] = '\0'; //这两个函数总是寻找'\0'作为起始位置
lstrcat(szbuffer,save);
szbuffer[length-1] = '\0';
free(save);
SetWindowText(hstatic,szbuffer);
}
}
return 0;
case WM_SETFOCUS:
SetFocus(hlistbox);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wparam,lparam);
}








