0

典型的な勝利スタイルのイベントループを使用して、マウス入力で基本的なopenglウィンドウを作成しようとしています. NSMouseMoved問題は、イベントを生成しようとして髪を引っ張っていることです。次のコードは、マウス アップ、マウス ダウン、マウス ドラッグなどに関するデバッグ情報を出力しますが、window に伝えてもマウス移動は出力しませんsetAcceptsMouseMovedEvents:YES。では、次の例でマウスの動きを機能させる方法についてのアイデアはありますか?

明らかに、私がウィンドウを作成している方法は非常にココアに似ていませんが、いくつかのトリッキーなスレッド化を行うメイクファイルベースの Windows C++ コードベースを移植しようとしています。そういうわけで、私は を使用した win32 ループに似たスタイルにこだわっていますGetMsg()

また、ビルドするには、次を使用しています:

gcc -o hellogl hellogl.m -framework Foundation -framework Cocoa -framework OpenGL

助けてくれてありがとう!

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

#include <OpenGL/gl.h>
#include <stdlib.h>

@interface BaseWinDelegate : NSWindow<NSWindowDelegate>

@end

@implementation BaseWinDelegate
- (void) windowWillClose:(NSNotification*)notification
{
    printf("Closing.\n");

    NSEvent * evt = [NSEvent otherEventWithType:NSApplicationDefined
                     location: NSMakePoint(0,0)
                     modifierFlags: 0
                     timestamp: 0.0
                     windowNumber: 0
                     context: nil
                     subtype: 0
                     data1: 0
                     data2: 0];

    [NSApp postEvent:evt atStart:NO];
}
@end

@interface BaseView : NSOpenGLView
- (void) update;
- (void) drawRect:(NSRect)rect;
- (void) reshape;
@end

@implementation BaseView

- (void) drawRect:(NSRect)rect
{
    glClearColor(0.2f,0.2f,0.2f,0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    [[self openGLContext] flushBuffer];
}

- (void) update
{
    printf("Update.\n");
}

- (void) reshape
{
    NSRect rect;

    [[self openGLContext] update];
    rect = [self bounds];

    printf("Reshape - %f, %f\n", rect.size.width,rect.size.height);
}

@end

int main(int argc, const char * argv[])
{
    printf("Starting.\n");

    NSAutoreleasePool * myPool = [[NSAutoreleasePool alloc] init ];
    NSApplicationLoad();

    NSRect rect = NSMakeRect(100,100,640,480);
    NSWindow * win = [[NSWindow alloc] initWithContentRect:rect
                        styleMask:NSTitledWindowMask |     NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask
                        backing: NSBackingStoreBuffered
                        defer: NO];

    NSOpenGLPixelFormatAttribute attributes[] =
    {
        NSOpenGLPFADoubleBuffer,
        0
    };

    NSOpenGLPixelFormat* pf = [[NSOpenGLPixelFormat alloc]     initWithAttributes:attributes];

    BaseView * pView = [[BaseView alloc] initWithFrame:rect pixelFormat:pf];
        BaseWinDelegate * myDelegate = [BaseWinDelegate alloc];

    [win setDelegate:myDelegate];
    [win setContentView: pView];
    [win makeKeyAndOrderFront: NSApp];
    [win setAcceptsMouseMovedEvents:YES];

    do
    {
        NSEvent * evt = [NSApp nextEventMatchingMask : NSAnyEventMask
                               untilDate : [NSDate distantFuture]
                               inMode : NSDefaultRunLoopMode
                               dequeue : YES ];

        NSEventType evtType = [evt type];

        if (evtType == NSApplicationDefined)
        {
            break;
        }

        printf("%d\n",(int)evtType);
        [NSApp sendEvent : evt];
    } while (1);

    [myPool drain];
    return EXIT_SUCCESS;
}
4

1 に答える 1

1

わかりました、それを理解しました。問題は、それがフォアグラウンドプロセスではないということです。したがって、このコードを追加すると修正されます。

ProcessSerialNumber psn;
GetCurrentProcess(&psn);
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetFrontProcess(&psn);

これにより、ウィンドウがキーウィンドウになり、マウス移動イベントが送信されなくなりました。それが他の誰か(つまり、自分のイベントループを処理するのが好きな人の0.1%)に役立つことを願っています。

于 2011-11-29T09:17:29.263 に答える