简体   繁体   中英

Error in Program VC++ Win32 application?

I have developed a program for mouse event in VC++ Win32 the program is running successfully and building up with success but not showing any output can anybody tell me what is error in it or anything i missed in program

hoping for quick and positive response

// 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;
}

Put these lines

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

after HWND hwmd = CreateWindowEx(0,...

You're trying to use char for functions that require wchar_t . Instead of casting all your strings to LPCWSTR , put an L in front of each string, eg

LPCWSTR msgdown = L"Left Mouse Button Down" ; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM