遇到个VC++问题,窗口程序在不同电脑上,有的可以执行然后弹出窗口,有的不行
我最近写一个窗口程序,获取手柄消息,结果窗口在不同的电脑上有的可以弹框,有的不可以,很奇怪,求大神指导…… vc++ --------------------编程问答-------------------- 句柄没获取到吧把你代码贴出来看看 --------------------编程问答-------------------- // test3.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <stdlib.h>
/*
JoyToMouse.c
Copyright (C) 2011, coreBugZJ, all rights reserved.
版本:
1.0.0
功能:
将游戏手柄操作映射为鼠标操作。
原理:
接收手柄消息,然后产生相应鼠标消息。
使用:
方向键移动(会自动加速);
键 1 <==> 左键;
键 2 <==> 右键;
键 3 <==> 调整灵敏度,增加;
键 4 <==> 调整灵敏度,减小。
*/
//#using "..\Debug\ClassLibrary2.dll"
#include <Windows.h>
#include <MMSystem.h>
#include <stdio.h>
#pragma comment( lib, "winmm.lib" )
/* 每 ELAPSE 毫秒检测手柄输入 */
#import "./Debug/MyCOM_Shake.tlb"
using namespace MyCOM_Shake;
TCHAR gClassName[] = TEXT("Joy");
TCHAR gWndName[] = TEXT("Joy -- coreBugZJ");
MyCOM_Shake::MyCom_Inte易做图ce *s;
VOID msgOut( HWND hWnd, TCHAR msg[], UINT msglen ) {
HDC hdc = GetDC( hWnd );
TextOut( hdc, 50, 20, msg, msglen );
ReleaseDC( hWnd, hdc );
/* MessageBox( NULL, msg, msg, MB_OK ); */
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
static JOYINFO ji1;
static DWORD id1 = 0xFFFF;
static JOYCAPS jc1;
static DWORD numDev;
static DWORD jxm1, jxl1, jxr1, jym1, jyt1, jyb1, x1, y1;
static DWORD dirMask = 0;
static INT vx = 0, vy = 0;
INT dx, dy, zPos,dz;
DWORD bf;
LPJOYCAPS pjc;
pjc = (LPJOYCAPS)malloc(sizeof(JOYCAPS));
LPJOYINFO pji;
pji = (LPJOYINFO)malloc(sizeof(LPJOYINFO));
char ss[10] = {0};
LPUINT puThreshold = (LPUINT)malloc(sizeof(LPUINT));
/* if (uMsg != 928&&uMsg != 930)
{
dx = uMsg;
sprintf(ss,"%d",dx);
msgOut( hWnd, ss, 12 );
}*/
char bb[12] = {0};
sprintf(bb,"%d",hWnd);
long int xx=atol(bb);
switch ( uMsg ) {
case WM_CREATE :
//SetTimer( hWnd, 1, 17, NULL );
joySetCapture(hWnd, JOYSTICKID1, 1, false);
/* if ( 0 == (numDev = joyGetNumDevs()) ) {
MessageBox( hWnd, TEXT("0 == joyGetNumDevs()\n\n请检查手柄是否插好"), TEXT("Error"), MB_OK | MB_ICONERROR );
SendMessage( hWnd, WM_DESTROY, 0, 0 );
return 0;
}
// 手柄 1
if ( JOYERR_NOERROR != joyGetPos( JOYSTICKID1, &ji1 ) ) {
MessageBox( hWnd, TEXT("JOYERR_NOERROR != joyGetPos()\n\n请检查手柄是否插好"), TEXT("Error"), MB_OK | MB_ICONERROR );
SendMessage( hWnd, WM_DESTROY, 0, 0 );
return 0;
}
if ( JOYERR_NOERROR != joyGetDevCaps( JOYSTICKID1, &jc1, sizeof(jc1) ) ) {
MessageBox( hWnd, TEXT("JOYERR_NOERROR != joyGetDevCaps()\n\n请检查手柄是否插好"), TEXT("Error"), MB_OK | MB_ICONERROR );
SendMessage( hWnd, WM_DESTROY, 0, 0 );
return 0;
}
id1 = JOYSTICKID1;
jxm1 = ( jc1.wXmin + jc1.wXmax ) / 2;
jxl1 = ( jc1.wXmin + jxm1 ) / 2;
jxr1 = ( jc1.wXmax + jxm1 ) / 2;
jym1 = ( jc1.wYmin + jc1.wYmax ) / 2;
jyt1 = ( jc1.wYmin + jym1 ) / 2;
jyb1 = ( jc1.wYmax + jym1 ) / 2;
x1 = jxm1;
y1 = jym1;
joySetCapture( hWnd, id1, 17, TRUE );*/
return 0;
/* case WM_TIMER :
if ( 0 == dirMask ) {
return 0;
}
dx = dy = 0;
if ( dirMask & MASK_LEFT ) {
vx += ACCEL_VX;
dx = -vx;
}
if ( dirMask & MASK_RIGHT ) {
vx += ACCEL_VX;
dx = vx;
}
if ( dirMask & MASK_UP ) {
vy += ACCEL_VY;
dy = -vy;
}
if ( dirMask & MASK_DOWN ) {
vy += ACCEL_VY;
dy = vy;
}
msgOut( hWnd, TEXT("MOVE "), 12 );
mouse_event( MOUSEEVENTF_MOVE, dx, dy, 0, 0 );
return 0;*/
case MM_JOY1MOVE :
//p->Initdirect();
dx = wParam;
sprintf(ss,"%d",dx);
msgOut( hWnd, ss, 12 );
if (dx != 0)
{
//joyGetDevCaps(JOYSTICKID1,pjc,sizeof(JOYCAPS));
// joyGetPos(JOYSTICKID1,pji);
// dx = pjc->wMid;
joyGetThreshold(JOYSTICKID1, puThreshold);
}
//msgOut( hWnd, "OK", 12 );
dirMask = 0;
dy = lParam;
/*sprintf(ss,"%d",dy);
msgOut( hWnd, ss, 12 );*/
x1 = LOWORD(lParam)/2048;
y1 = HIWORD(lParam)/2048;//如果游戏杆在中心位置的左边,移动光标到左边。反之亦然
sprintf(ss,"%d,%d",x1,y1);
msgOut( hWnd, ss, 12 );
if(x1 <= 12)
{
s->SetParent(xx);
s->Initdirect();
msgOut( hWnd, TEXT("左转 "), 12 );
}else if(x1 >= 20)
{
//Sleep(5);
msgOut( hWnd, TEXT("右转 "), 12 );
}else if(y1 <= 12)//如果游戏杆在中心位置的下边,移动光标到下边。反之亦然
{
msgOut( hWnd, TEXT("前 "), 12 );
}
else if(y1 >= 25)
{
msgOut( hWnd, TEXT("后 "), 12 );
}
else if (wParam==256)
{
msgOut( hWnd, TEXT("bbt1 "), 12 );
//SetDlgItemText(IDC_STATIC1,"select");
}else if(wParam&JOY_BUTTON2CHG)
{
//SetDlgItemText(IDC_STATIC1,"start");
}else if(wParam&JOY_BUTTON5)
{
//SetDlgItemText(IDC_STATIC1,"5");
}
else if(wParam&JOY_BUTTON6)
{
//SetDlgItemText(IDC_STATIC1,"6");
}else if(wParam&JOY_BUTTON7)//select
{
//SetDlgItemText(IDC_STATIC1,"7");
}else if(wParam&JOY_BUTTON8)//select
{
//SetDlgItemText(IDC_STATIC1,"8");
}
return 0;
case MM_JOY1BUTTONDOWN:
dx = wParam;
sprintf(ss,"%d",dx);
msgOut( hWnd, ss, 12 );
// msgOut( hWnd, "易做图", 12 );
/*if(wParam & JOY_BUTTON1)
{
Sleep(5);
//SetDlgItemText(IDC_STATIC1,"1");
msgOut( hWnd, TEXT("1 "), 12 );
}else if (wParam & JOY_BUTTON2)
{
Sleep(5);
msgOut( hWnd, TEXT("2 "), 12 );
//SetDlgItemText(IDC_STATIC1,"2");
}else if(wParam & JOY_BUTTON3)
{
Sleep(5);
msgOut( hWnd, TEXT("3 "), 12 );
//SetDlgItemText(IDC_STATIC1,"3");
}
else if(wParam & JOY_BUTTON4)
{
Sleep(5);
msgOut( hWnd, TEXT("4 "), 12 );
//SetDlgItemText(IDC_STATIC1,"4");
} */
return 0;
case MM_JOY1ZMOVE :
zPos=lParam;
dz = HIWORD(lParam);
sprintf(ss,"%d,%d",zPos,dz);
// msgOut( hWnd, ss, 12 );
dx = wParam;
// sprintf(ss,"%d",dx);
// msgOut( hWnd, ss, 12 );
/* if(wParam & JOY_BUTTON1)
{
Sleep(5);
//SetDlgItemText(IDC_STATIC1,"1");
msgOut( hWnd, TEXT("1 "), 12 );
}else if (wParam & JOY_BUTTON2)
{
Sleep(5);
msgOut( hWnd, TEXT("2 "), 12 );
//SetDlgItemText(IDC_STATIC1,"2");
}else if(wParam & JOY_BUTTON3)
{
Sleep(5);
msgOut( hWnd, TEXT("3 "), 12 );
//SetDlgItemText(IDC_STATIC1,"3");
}
else if(wParam & JOY_BUTTON4)
{
Sleep(5);
msgOut( hWnd, TEXT("4 "), 12 );
//SetDlgItemText(IDC_STATIC1,"4");
} */
return 0;
/*case MM_JOY1BUTTONUP:
dx = wParam;
sprintf(ss,"%d",dx);
msgOut( hWnd, ss, 12 );
return 0;
case WM_DESTROY :
KillTimer( hWnd, 1 );
if ( JOYSTICKID1 == id1 ) {
joyReleaseCapture( id1 );
}
PostQuitMessage( 0 );
return 0;
case WM_KEYDOWN:
dx = wParam;
sprintf(ss,"%d",dx);
msgOut( hWnd, ss, 12 );
}
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
--------------------编程问答-------------------- INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmd, INT nShow ) {
WNDCLASSEX wc;
HWND hWnd;
MSG msg;
wc.cbClsExtra = 0;
wc.cbSize = sizeof(wc);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
wc.hInstance = hInst;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = gClassName;
wc.lpszMenuName = NULL;
wc.style = 0;
if ( 0 == RegisterClassEx( &wc ) ) {
MessageBox( NULL, TEXT("RegisterClassEx Failed!"), TEXT("Error"), MB_OK | MB_ICONERROR );
return 0;
}
hWnd = CreateWindowEx( 0,
gClassName, gWndName,
WS_OVERLAPPEDWINDOW,
200, 200, 400, 100,
NULL, NULL,
hInst, NULL );
if ( NULL == hWnd ) {
MessageBox( NULL, TEXT("CreateWindowEx Failed!"), TEXT("Error"), MB_OK | MB_ICONERROR );
return 0;
}
ShowWindow( hWnd, nShow );
UpdateWindow( hWnd );
CoInitialize(NULL); //注意初始化
MyCOM_Shake::MyCom_Inte易做图cePtr p((__uuidof(MyCOM_Shake::Class1))); //创建智能指针
*s = p;
while ( GetMessage( &msg, NULL, 0, 0 ) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
}
--------------------编程问答-------------------- 代码太长,没细看,
注意以下几点
1、似乎你用到了.net的组件,确保出错的计算机都装有.net
2、你用到了com组件,确保每台计算机都正确注册,并能使用哪个组件
。。。
补充:.NET技术 , VC.NET