标题:为什么修改了部分代码,连窗口都不能显示!
取消只看楼主
疯狂的小白
Rank: 2
等 级:论坛游民
帖 子:8
专家分:10
注 册:2013-5-29
 问题点数:0 回复次数:4 
为什么修改了部分代码,连窗口都不能显示!
//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);
}

 
搜索更多相关主题的帖子: include 
2013-06-05 12:50
疯狂的小白
Rank: 2
等 级:论坛游民
帖 子:8
专家分:10
注 册:2013-5-29
得分:0 
为什么要这么改呢?这是用C在编程,也没用C++啊,能解释一下原因吗?
我看了一下,我的程序没有包含#include "stdafx.h"
程序中的静态变量是我改程序的时候直接加到后面的...急于测试程序...出了句柄和xchar,ychar,szbuffer之外,其他的都可以改为一般的变量
2013-06-05 14:37
疯狂的小白
Rank: 2
等 级:论坛游民
帖 子:8
专家分:10
注 册:2013-5-29
得分:0 
我刚才测试了一下你改过的程序,依旧行不通,估计是编译器不同吧,我的是vs2010
刚才检查了一下,觉得应该是 lstrcpy(strings , &ch);出错了
这是lstcpy的功能说明:功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间
ch只是一个字符,没有包含'\0',把ch换成ch[2],并且设置ch[1]='\0';程序正常运行。
2013-06-05 15:13
疯狂的小白
Rank: 2
等 级:论坛游民
帖 子:8
专家分:10
注 册:2013-5-29
得分:0 
能否请教另一个问题...
SendMessage(hedit, EM_GETLINE, lenth, (LPARAM)szbuffer); 不能正常运行,求解!
MSDN上的解释是:把szbuffer的第一个word设置为它的长度...这还要我怎么办...各种尝试不成功,有人却成功运行了,纳闷..
case IDC_BUTTON:
                int index = SendMessage(hstatic,EM_LINEINDEX,0,0);
                int lenth = SendMessage(hedit,EM_LINELENGTH,index,0) + 1;
               
                if ( szbuffer )
                    free(szbuffer);
                szbuffer = (TCHAR *)malloc(sizeof(TCHAR) * lenth);
                // szbuffer[0] = lenth; // 这中方式也行不通
                *((LPWORD)szbuffer) = lenth;  //这还是行不通...
                SendMessage(hedit, EM_GETLINE, lenth, (LPARAM)szbuffer); // 读取失败
                szbuffer[lenth-1] = '\0';
                SetWindowText(hstatic,szbuffer);
                break;
            }
2013-06-05 15:17
疯狂的小白
Rank: 2
等 级:论坛游民
帖 子:8
专家分:10
注 册:2013-5-29
得分:0 
好吧。。。大哥你还是帮我看看刚才发的问题,已经跪两天了...郁闷
2013-06-05 16:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-414568-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.270259 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved