| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 513 人关注过本帖
标题:输出字符串错误~~
只看楼主 加入收藏
gwcome
Rank: 2
来 自:尼玛星球
等 级:论坛游民
帖 子:43
专家分:33
注 册:2013-1-22
结帖率:100%
收藏
 问题点数:0 回复次数:1 
输出字符串错误~~
小小菜又来请教各位了~~~~
写了个记录键盘消息的代码
首先 在处理键盘消息的分支上记录键盘消息。。信息存储在二维数组szbuffer中。。。代码如下
程序代码:
    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);
        wsprintf(szBuffer[nummessage],TEXT("%s---%d----%d"),pmessage[message-WM_KEYDOWN],wParam,lParam);
        nummessage++;
        if (nummessage>NUMLINES)
        {
            MessageBox(hwnd,0,0,0);
            return 0;
        }
        ReleaseDC(hwnd,hdc);
        return 0;

然后再通过 在wm_paint分支中通过textout函数将数组输出。。代码如下。。
程序代码:
    case WM_PAINT :
        {
            hdc=GetDC(hwnd);
            for (int i=0;i<nummessage;i++)
            {
                TextOut(hdc,0,i*cyChar,szBuffer[i],50);
            }
            ReleaseDC(hwnd,hdc);
        }
        return 0 ;

运行之后字符串后面出现不明字符串!!! 详见截图
图片附件: 游客没有浏览图片的权限,请 登录注册

各位~~~~~这个怎么去除呢!!!TAT TAT
搜索更多相关主题的帖子: 键盘 记录 字符串 
2013-04-05 20:34
gwcome
Rank: 2
来 自:尼玛星球
等 级:论坛游民
帖 子:43
专家分:33
注 册:2013-1-22
收藏
得分:0 
程序代码:
#include <windows.h>
#include <string>
#include <vector>
#include "resource.h"
#pragma comment(lib, "winmm") 
#define  NUMLINES 1000

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, MAKEINTRESOURCE (IDI_ICON1)) ;
    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;
    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(""))创建案件子窗口~~~
    TEXTMETRIC tm;
    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) ;
        // Set vertical scroll bar range and page size
        //Windows会把最大的滚动条位置限制为si.nMax - si.nPage +1
        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);
        SetScrollPos   (hwnd, SB_VERT, iVscrollPos, TRUE) ;
        return 0 ;

    case WM_VSCROLL:
        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));
        if (iVscrollPos !=GetScrollPos(hwnd,SB_VERT))
        {                    
            SetScrollPos(hwnd,SB_VERT,iVscrollPos,TRUE);
            InvalidateRect(hwnd,NULL,TRUE);
        }
        return 0 ;


    case WM_PAINT :
        {
            hdc=GetDC(hwnd);
            for (int i=0;i<nummessage;i++)
            {
                TextOut(hdc,0,i*cyChar,szBuffer[i],50);
            }
            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);
        wsprintf(szBuffer[nummessage],TEXT("%s---%d----%d"),pmessage[message-WM_KEYDOWN],wParam,lParam);
        nummessage++;
        if (nummessage>NUMLINES)
        {
            MessageBox(hwnd,0,0,0);
            return 0;
        }
        ReleaseDC(hwnd,hdc);
        return 0;

    }
    return DefWindowProc (hwnd, message, wParam, lParam) ;
}
完整代码

[ 本帖最后由 gwcome 于 2013-4-5 20:38 编辑 ]

小菜小菜
2013-04-05 20:37
快速回复:输出字符串错误~~
数据加载中...
 
   



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

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