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

标签 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;
}

最佳答案

把这些行

ShowWindow(hwmd, SW_SHOW);
UpdateWindow(hwmd);

HWND hwmd = CreateWindowEx(0,... 之后

关于c++ - 程序 VC++ Win32 应用程序出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4878597/

相关文章:

c++ - 如何从进程内部确定 CPU 和内存消耗

c - 为什么局部变量没有默认类型而全局变量在 C 中有?

c - scanf("%d/%d/%d) 在 C 中是什么意思?

c# - 是否有用于 Visual Studio 的断点插件?

c++ - 局部变量的代码块自动完成

c++ - 在蛇游戏中打印障碍物时如何解决边框问题?

c++ - glDrawElements 已停止工作

.net - VisualStudio错误?

c++ - 奇怪的行为 printf( ) 每行调用 10-13 次

c++ - 如何有效地查找大型 vector 中的元素