| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2349 人关注过本帖
标题:Window Creation Failed
只看楼主 加入收藏
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
结帖率:100%
收藏
 问题点数:0 回复次数:4 
Window Creation Failed
创建了一个为win32应用程序。编译通过。运行时不出现主窗体,出来一个Window Creation Failed的对话框。多方面排查之后,发现问题出在一个取模运算当中。
原本是
short span;
span=((unsigned short)(time(0)))%(upper-lower);
出现上述问题。后改为
short span;
if(upper-lower==10)
    span=((unsigned short)(time(0)))%10;
就可以完全正常运行。
为什么就不能对表达式(upper-lower)直接取模呢?请问有没有遇到过类似问题的朋友。我的编译器是VC++6.0
搜索更多相关主题的帖子: 应用程序 Failed 编译器 对话框 表达式 
2016-08-22 22:13
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
首先,这两段代码不一样,前者在一切情况下都执行模除;而后者只在等于10的条件下才模除。

上传整个工程吧
2016-08-22 23:52
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
这里是我的源代码,请各位斧正。只因电脑不在手边,回贴晚了

#include <windows.h>
#include<time.h>
#define IDB_OK 101

/* This is where all the input to the window goes to */
HWND txtFrame,txtUpper,txtKey,lblUpper,btnOK,lblNum,txtNum,lblLower,txtLower;
char str_upper[3],str_lower[3];short upper,lower;
class Question
{
private:
    short add_one;
    short add_two;
    short sum;
    short add;//运算符标记。标识做加法1还是减法0
    short blank;//标识第几个数作为问题,10,20,30
    short answer;
public:
    short GetAnswer(){return answer;}
    char str[37];
    char str1[12];
    char str2[12];
    char str3[12];
    Question()
    {
        while(!Reset());
    }
    int Reset()
    {
        strcpy(str1,"");strcpy(str2,"");strcpy(str3,"");
        GetWindowText(txtUpper,str_upper,3);upper=atoi(str_upper);
        GetWindowText(txtLower,str_lower,3);lower=atoi(str_lower);
        short span;
        /****************以下两行为问题代码**********************/
        //if(upper-lower==10) span=10;else span=40; //若执行这一行可以对span取余
        span=upper-lower;                           //若执行这一行不能对span取余
                /********************************************************/
        sum=(((unsigned short)rand())%span)+lower+1;//为什么不能对span取模???
        add_one=((unsigned short)rand())%(sum-1)+1;
        add_two=sum-add_one;
        add=((unsigned short)rand())%2;
        blank=(((unsigned short)rand())%3+1)*10;
        switch(blank+add)
        {
        case 11:
            answer=add_one;
            strcpy(str1,TEXT("(     ) + "));
            itoa(add_two,str2,10);
            strcat(str2," = ");
            itoa(sum,str3,10);
            break;
        case 10:
            answer=sum;
            strcpy(str1,TEXT("(     ) - "));
            itoa(add_one,str2,10);
            strcat(str2," = ");
            itoa(add_two,str3,10);
            break;
        case 21:
            answer=add_two;
            itoa(add_one,str1,10);
            strcat(str1," + ");
            strcpy(str2,TEXT("(     ) = "));
            itoa(sum,str3,10);
            break;
        case 20:
            answer=add_one;
            itoa(sum,str1,10);
            strcat(str1," - ");
            strcpy(str2,"(     ) = ");
            itoa(add_two,str3,10);
            break;
        case 31:
            answer=sum;
            itoa(add_one,str1,10);
            strcat(str1," + ");
            itoa(add_two,str2,10);
            strcat(str2," = ");
            strcpy(str3,TEXT("(     )"));
            break;
        case 30:
            answer=add_two;
            itoa(sum,str1,10);
            strcat(str1," - ");
            itoa(add_one,str2,10);
            strcat(str2," = ");
            strcpy(str3,TEXT("(     )"));
            break;
        default:return 0;
        }
        memset(str,0,30);
        strcat(str,str1);
        strcat(str,str2);
        strcat(str,str3);
        return 1;
    }
};
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
    Question q;short num;char strnum[3];char strkey[2];short i;char oldq[5000];char oldk[256];
    switch(Message) {
        
        /* Upon destruction, tell the main thread to stop */
    case WM_CREATE:
        txtFrame=CreateWindow(TEXT("EDIT"),TEXT(""),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_VSCROLL|WS_BORDER|SS_LEFT,20,20,580,360,hwnd,NULL,NULL,NULL);
        txtKey=CreateWindow(TEXT("EDIT"),TEXT(""),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_BORDER|SS_LEFT,20,400,580,40,hwnd,NULL,NULL,NULL);
        txtUpper=CreateWindow(TEXT("EDIT"),TEXT("20"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|WS_BORDER|SS_LEFT,100,470,22,22,hwnd,NULL,NULL,NULL);
        lblUpper=CreateWindow(TEXT("STATIC"),TEXT("数字上限"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|SS_CENTER,20,470,80,40,hwnd,NULL,NULL,NULL);
        lblLower=CreateWindow(TEXT("STATIC"),TEXT("数字下限"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|SS_CENTER,140,470,80,40,hwnd,NULL,NULL,NULL);
        txtLower=CreateWindow(TEXT("EDIT"),TEXT("10"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|WS_BORDER|SS_CENTER,220,470,22,22,hwnd,NULL,NULL,NULL);
        lblNum=CreateWindow(TEXT("STATIC"),TEXT("题目数"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|SS_CENTER,300,470,69,40,hwnd,NULL,NULL,NULL);
        txtNum=CreateWindow(TEXT("EDIT"),TEXT("10"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|WS_BORDER|SS_LEFT,370,470,40,22,hwnd,NULL,NULL,NULL);
        btnOK=CreateWindow(TEXT("BUTTON"),TEXT("确定"),WS_OVERLAPPED|WS_CHILD|WS_VISIBLE|WS_BORDER|SS_CENTER|BS_PUSHBUTTON,500,470,80,40,hwnd,(HMENU)IDB_OK,NULL,NULL);
        break;
    case WM_COMMAND:
        switch(HIWORD(wParam))
        {
        case BN_CLICKED:
            switch(LOWORD(wParam))
            {
            case IDB_OK:
                srand((unsigned int)time(0));
                SetWindowText(txtFrame,"");SetWindowText(txtKey,"");
                GetWindowText(txtNum,strnum,3);num=atoi(strnum);
                for(i=0;i<num;i++)
                {
                    GetWindowText(txtFrame,oldq,5000);
                    strcat(oldq,q.str);
                    if(i%3!=2)
                        strcat(oldq,"\t\t");
                    else
                    {
                        strcat(oldq,"\r\n");
                    }
                    SetWindowText(txtFrame,oldq);
                    GetWindowText(txtKey,oldk,256);
                    itoa(q.GetAnswer(),strkey,10);
                    strcat(oldk,strkey);strcat(oldk,"  ");
                    SetWindowText(txtKey,oldk);
                    q.Reset();
                }
                char siz[4];itoa(i,siz,10);SetWindowText(hwnd,siz);
                break;
            }
            break;
        }
        break;
        case WM_DESTROY: {
            PostQuitMessage(0);
            break;
        }
        
        /* All other messages (a lot of them) are processed using default procedures */
        default:
            return DefWindowProc(hwnd, Message, wParam, lParam);
    }
    return 0;
}

/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    WNDCLASSEX wc; /* A properties struct of our window */
    HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
    MSG Msg; /* A temporary location for all messages */

    /* zero out the struct and set the stuff we want to modify */
    memset(&wc,0,sizeof(wc));
    wc.cbSize         = sizeof(WNDCLASSEX);
    wc.lpfnWndProc     = WndProc; /* This is where we will send messages to */
    wc.hInstance     = hInstance;
    wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
   
    /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszClassName = "WindowClass";
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
    wc.hIconSm         = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */

    if(!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","幼儿数学",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, /* x */
        CW_USEDEFAULT, /* y */
        640, /* width */
        580, /* height */
        NULL,NULL,hInstance,NULL);

    if(hwnd == NULL) {
        MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }

    /*
        This is the heart of our program where all input is processed and
        sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
        this loop will not produce unreasonably high CPU usage
    */
    while(GetMessage(&Msg, NULL, 0, 0) > 0) { /* If no error is received... */
        TranslateMessage(&Msg); /* Translate key codes to chars if present */
        DispatchMessage(&Msg); /* Send it to WndProc */
    }
    return Msg.wParam;
}
2016-09-02 09:10
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
调试时发现:
GetWindowTextA(txtUpper,str_upper,3);upper=atoi(str_upper); 此时txtUpper是NULL,str_upper是"",upper是0;
下一句同样lower是0。
span=upper-lower; 中 span 也就是 0 了
sum=(((unsigned short)rand())%span)+lower+1; 就肯定会发生除零错误了呀。
你的Reset函数执行于CreateWindow创建这txtUpper/txtLower两个控件之前了。

顺便说一下,
char str_upper[3];
GetWindowText(txtUpper,str_upper,3);
是错误的
要么是
char str_upper[3];
GetWindowTextA(txtUpper,str_upper,3);
要么是
wchar_t str_upper[3];
GetWindowTextW(txtUpper,str_upper,3);
要么是
TCHAR str_upper[3];
GetWindowText(txtUpper,str_upper,3);

同样,
char str1[12];
strcpy(str1,TEXT("(     ) + "));
也是错误的
要么是
char str1[12];
strcpy(str1,"(     ) + ");
要么是
wchar_t str1[12];
wcscpy(str1,L"(     ) + ");
要么是
TCHAR str1[12];
_tcscpy(str1,TEXT("(     ) + "));
2016-09-02 10:32
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
哦。好的。谢谢。看来是执行时间顺序的问题。我自己再去看一看那几种数据类型的区别。
2016-09-02 12:21
快速回复:Window Creation Failed
数据加载中...
 
   



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

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