程序VC ++ Win32应用程序出错?

时间:2011-02-02 18:57:37

标签: c++ c visual-studio visual-c++

我已经在VC ++ Win32中开发了一个鼠标事件程序,该程序运行成功并且成功但没有显示任何输出可以让任何人告诉我它中的错误或程序中遗漏的任何内容

希望得到快速积极的回应

// ttt.cpp : Defines the entry point for the application.
//  TO Demonstrate the Mouse Events

#include "windows.h"
#include "stdafx.h"
#include "stdio.h"




LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    int x=0 , y=0;
    LPCWSTR msgdown = (LPCWSTR)"Left Mouse Button Down" ;
    LPCWSTR msgup = (LPCWSTR)"Left Mouse Button UP" ;
    LPCWSTR msgdblclk = (LPCWSTR)"Left Mouse Button Dbl clk" ;
    LPCWSTR mouse = (LPCWSTR)"Mouse" ;
    switch (msg)
    {
        case WM_CLOSE:
        DestroyWindow(hWnd);
        break;

        case WM_DESTROY:
        PostQuitMessage(0);
        break;

        case WM_LBUTTONDOWN:
        MessageBox(hWnd,msgdown,mouse,MB_OK);

        break;

        case WM_LBUTTONUP:
            MessageBox(hWnd,msgup,mouse,MB_OK);
        break;

        case WM_LBUTTONDBLCLK:
        MessageBox(hWnd,msgdblclk,mouse,MB_OK);
        break;      

        x=LOWORD(lParam);
        y=HIWORD(lParam);

        char text[50];
        sprintf(text,"Mouse Position: X=%d, Y=%d",x,y);
        LPCWSTR textmsg = (LPCWSTR)text;
        SetWindowText(hWnd,textmsg);
        break;
    }
        return DefWindowProc(hWnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
    LPCTSTR className=(LPCTSTR)"Mouse Test";
    WNDCLASSEX wc;

    wc.cbSize =sizeof(WNDCLASSEX);
    wc.style =CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
    wc.lpfnWndProc =WndProc;
    wc.cbClsExtra =0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW +1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = className;
    wc.hIconSm = LoadIcon(NULL,IDI_WINLOGO);


    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL,(LPCWSTR)"Error Registering Class",(LPCWSTR)"Error RegisterClassEx",MB_OK | MB_ICONERROR);
        return 1;
    }

    HWND hwmd = CreateWindowEx(0,className,(LPCWSTR)"Mouse Test",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,400,300,NULL,NULL,hInstance,NULL);

    if(!hwmd)
    {
        MessageBox(NULL,(LPCWSTR)"Error Creating Window",(LPCWSTR)"Error CreateWindowEx",MB_OK | MB_ICONERROR);
        return 1;
    }

        MSG msg;

    while(GetMessage(&msg,NULL,0,0)>0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

2 个答案:

答案 0 :(得分:4)

把这些行

ShowWindow(hwmd, SW_SHOW);
UpdateWindow(hwmd);
HWND hwmd = CreateWindowEx(0,...

之后

答案 1 :(得分:3)

您尝试将char用于需要wchar_t的功能。不要将所有字符串都转换为LPCWSTR,而是在每个字符串前加上L,例如

LPCWSTR msgdown = L"Left Mouse Button Down" ;