| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 230 人关注过本帖
标题:为什么修改了部分代码,连窗口都不能显示!
只看楼主 加入收藏
疯狂的小白
Rank: 2
等 级:论坛游民
帖 子:8
专家分:10
注 册:2013-5-29
收藏
 问题点数:0 回复次数:7 
为什么修改了部分代码,连窗口都不能显示!
//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
lonmaor
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:郑州
等 级:版主
威 望:75
帖 子:2637
专家分:6423
注 册:2007-11-27
收藏
得分:0 
我把所有的malloc/free改成new/delete就没问题了。很好奇为什么要定义那么多静态变量?

程序代码:
// winapp1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#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,"create 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;
    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); //保存一个字符
            strings = new 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 = new TCHAR [sizeof(TCHAR) * length];
                SendMessage(hlistbox,LB_GETTEXT,index,(LPARAM)save);

                // 原来的静态我文本还有字符
                if ( szbuffer)
                {
                    length += lstrlen(szbuffer);
                    temp = new TCHAR[sizeof(TCHAR) * (lstrlen(szbuffer) + 1)];
                    lstrcpy(temp,szbuffer);
                    //free(szbuffer);
                    delete [] szbuffer;
                }
                szbuffer = new TCHAR[sizeof(TCHAR) * length];
                if ( temp)
                {   
                    lstrcpy(szbuffer,temp);
                    delete [] temp;
                }
                else //使用lstrcat 或则 strcat ,若接收器为空,则将第一位设成'\0',
                    szbuffer[0] = '\0'; //这两个函数总是寻找'\0'作为起始位置
                lstrcat(szbuffer,save);
                szbuffer[length-1] = '\0';
                delete [] 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);
}

从不知道到知道,到知道自己不知道,成长的道路上脚步深深浅浅
2013-06-05 13:08
疯狂的小白
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
lonmaor
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:郑州
等 级:版主
威 望:75
帖 子:2637
专家分:6423
注 册:2007-11-27
收藏
得分:0 
呃,我是用vc6编译通过的。居然提示malloc/free为未标识。改了之后就ok了。

在vs2012下,加L""后编译通过。。至于为啥不知道。

[ 本帖最后由 lonmaor 于 2013-6-5 16:22 编辑 ]

从不知道到知道,到知道自己不知道,成长的道路上脚步深深浅浅
2013-06-05 16:19
疯狂的小白
Rank: 2
等 级:论坛游民
帖 子:8
专家分:10
注 册:2013-5-29
收藏
得分:0 
好吧。。。大哥你还是帮我看看刚才发的问题,已经跪两天了...郁闷
2013-06-05 16:31
codeos
Rank: 2
等 级:论坛游民
帖 子:8
专家分:10
注 册:2011-2-14
收藏
得分:0 
以下是引用疯狂的小白在2013-6-5 15:17:44的发言:

能否请教另一个问题...
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;
            }

LZ没好好看MSDN的解释:
wParam
Specifies the zero-based index of the line to retrieve from a multiline edit control. A value of zero specifies the topmost line. This parameter is ignored by a single-line edit control.

“SendMessage(hedit, EM_GETLINE, lenth, (LPARAM)szbuffer); // 读取失败”
这里的lenth不是要接受字符串的长度,而是要获取目标Edit控件中 “zero-based”的行号,如果目标Edit控件不存在lenth行,调用后应该会失败;


lParam
Pointer to the buffer that receives a copy of the line. Before sending the message, set the first word of this buffer to the size, in TCHARs, of the buffer. For ANSI text, this is the number of bytes; for Unicode text, this is the number of characters. The size in the first word is overwritten by the copied line.

发送EM_GETLINE消息之前需要把szBuffer的长度值放在szBuffer的“first word”中,调用完成后此长度值会被“overwritten”


示例:
TCHAR szDebug[1024];
szDebug[0] = 1024;
SendMessage(hEdit,EM_GETLINE,1,(LPARAM)szDebug);
MessageBox(NULL,szDebug,L"Msg",0);
hEdit是我创建的一个Edit控件句柄,在VS2005中调试OK。
2013-06-25 23:03
快速回复:为什么修改了部分代码,连窗口都不能显示!
数据加载中...
 
   



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

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