标题:messagebox的错误和textout错误
只看楼主
gwcome
Rank: 2
来 自:尼玛星球
等 级:论坛游民
帖 子:43
专家分:33
注 册:2013-1-22
结帖率:100%
 问题点数:0 回复次数:0 
messagebox的错误和textout错误
在处理键盘消息是调用messagebox,却出现卡死现象!!!。并且textout输出的信息中有未定义的字符!!!
程序代码:
#include <windows.h>
#include <string>
#include <vector>
#pragma comment(lib, "winmm") 
#define  NUMLINES 50

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
    TCHAR    szAppName[] = TEXT ("IconDemo") ;
    HWND     hwnd ;
    MSG      msg ;
    WNDCLASS wndclass ;

    wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
    wndclass.lpfnWndProc   = WndProc ;
    wndclass.cbClsExtra    = 0 ;
    wndclass.cbWndExtra    = 0 ;
    wndclass.hInstance     = hInstance ;
    wndclass.hIcon         = LoadIcon (hInstance, 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, TEXT ("This program requires Windows NT!"),
            szAppName, MB_ICONERROR) ;
        return 0 ;
    }

    hwnd = CreateWindow (szAppName, TEXT ("窗口"),
        WS_OVERLAPPEDWINDOW|WS_VSCROLL,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hInstance, NULL) ;

    ShowWindow (hwnd, iCmdShow) ;
    UpdateWindow (hwnd) ;

    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage (&msg) ;
        DispatchMessage (&msg) ;
    }
    return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    HDC  hdc ;
    static int i=0 ,cxChar,cxCaps,cyChar,iMaxWidth,cxClient,cyClient,iVscrollPos;
    int j;
    static int nummessage=0;
    static TCHAR szBuffer[NUMLINES][50];
    static TCHAR *pmessage[]=
    {
        TEXT("WM_KEYDOWN"),TEXT("WM_KEYUP"),TEXT("WM_CHAR"),TEXT("WM_DEADCHAR"),
        TEXT("WM_SYSKEYDOWN"),TEXT("WM_SYSKEYUP"),TEXT("WM_SYSCHAR"),TEXT("WM_SYSDEADCHAR")
    };
    CreateWindow (TEXT("button"), TEXT ("窗口"),
        WS_CHILD|WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, NULL, NULL) ;
    TEXTMETRIC tm;
    static SCROLLINFO si;

    switch (message)
    {
    case WM_CREATE:

        hdc = GetDC (hwnd) ;
        GetTextMetrics (hdc, &tm) ;
        cxChar = tm.tmAveCharWidth ;
        cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
        cyChar = tm.tmHeight + tm.tmExternalLeading ;
        ReleaseDC (hwnd, hdc) ;
        return 0 ;
    case WM_SIZE:
        cxClient = LOWORD (lParam) ;
        cyClient = HIWORD (lParam) ;
        si.cbSize = sizeof (si) ;
        si.fMask = SIF_RANGE | SIF_PAGE ;
        si.nMin   = 0 ;
        si.nMax   = NUMLINES - 1 ;
        si.nPage = cyClient / cyChar ;
        SetScrollInfo (hwnd, SB_VERT, &si, TRUE);
        return 0 ;

    case WM_VSCROLL:
        si.cbSize=sizeof(si);
        si.fMask=SIF_ALL;
        GetScrollInfo(hwnd,SB_VERT,&si);
        iVscrollPos=si.nPos;
        switch (LOWORD (wParam))
        {
        case SB_TOP:
            iVscrollPos = si.nMin ;
            break ;
        case SB_BOTTOM:
            iVscrollPos = si.nMax ;
            break ;
        case SB_LINEUP:
            iVscrollPos -= 1 ;
            break ;
        case SB_LINEDOWN:
            iVscrollPos += 1 ;
            break ;
        case SB_PAGEUP:
            iVscrollPos-= si.nPage ;
            break ;
        case SB_PAGEDOWN:
            iVscrollPos += si.nPage ;
            break ;
        case SB_THUMBTRACK:
            iVscrollPos = si.nTrackPos ;
            break ;
        default:
            break ;         
        }
        iVscrollPos=max(0,min(iVscrollPos,NUMLINES-1));

        GetScrollInfo(hwnd,SB_VERT,&si);
        if (iVscrollPos !=si.nPos)
        { 
            si.nPos=iVscrollPos;
            SetScrollInfo(hwnd,SB_VERT,&si,TRUE);
            InvalidateRect(hwnd,NULL,TRUE);
        }
        return 0 ;


    case WM_PAINT :
        {
            SetScrollInfo(hwnd,SB_VERT,&si,TRUE);
            hdc=GetDC(hwnd);
            j=0;
            for (int i=iVscrollPos;i<nummessage;i++)
            {
                TextOut(hdc,0,j*cyChar,szBuffer[i],50);//输出的内容包括很多,未定义的字符串
                j++;
            }
            ReleaseDC(hwnd,hdc);
        }
        return 0 ;

    case WM_DESTROY :

        PostQuitMessage (0) ;
        return 0 ;

    case WM_KEYDOWN:
    case WM_KEYUP:
    case WM_CHAR:
    case WM_DEADCHAR:
    case WM_SYSKEYDOWN:
    case WM_SYSKEYUP:
    case WM_SYSCHAR:
    case WM_SYSDEADCHAR:
        hdc=GetDC(hwnd);
        if (nummessage>NUMLINES-1)
        {
            MessageBox(hwnd,TEXT("您输入的信息过多,请从新运行程序\n"),0,MB_OK);//error 程序在此处卡死,为什么?????
            PostQuitMessage (0) ;
            return 0 ;
        }
        wsprintf(szBuffer[nummessage],TEXT("%s---%d----%d"),pmessage[message-WM_KEYDOWN],wParam,lParam);
        nummessage++;
        ReleaseDC(hwnd,hdc);
        return 0;

    }
    return DefWindowProc (hwnd, message, wParam, lParam) ;
}
搜索更多相关主题的帖子: vector comment include 
2013-04-07 16:57



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




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

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