Bitmap Caret无法运行WinApi

时间:2014-05-28 08:48:01

标签: c++ winapi

修改 现在这很奇怪,似乎:: CreateCaret以某种方式解释位图的颜色错误,当我使用RGB(255,255,255)时,得到的插入符号为黑色,当我使用RGB(53,53,52)时结果插入符号如果是RGB(253,253,252),如果我使用RGB(0,0,0)作为位图,那么插入符号就不会显示出来。


我正在尝试为应用程序创建基于位图的自定义插入符号。 :: CreateCaret创建正确大小的插入符号,即位图的大小,但它不显示位图,而是获得位图大小的黑色插入符号。我已经使用ResourceHacker检查过,位图正在加载到exe中。 可能有什么不对?我在微软网站上关注article

我在Windows 7中工作。

这是一个screeshot:

enter image description here

这是我的代码:

RESOURCE.RC

#include <windows.h>
#include "resource.h"


IDB_CARET  BITMAP  DISCARDABLE  "caret.bmp"

RESOURCE.H

#define  IDB_CARET  201

的main.cpp

#include <windows.h>
#include "resource.h"


HINSTANCE hInstance;
HBITMAP caret;

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */



    hInstance = hThisInstance;



    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:

            ::DestroyCaret (  );

            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */

            break;
       case WM_CREATE:
             {
                 caret = ::LoadBitmap (  hInstance, MAKEINTRESOURCE ( IDB_CARET )  );
                 if ( caret == NULL )   ::MessageBox ( 0, "LoadBitmap", "LoadBitmap", 0 );   
             }
             break;
        case WM_SETFOCUS:
             {
                 if ( ::CreateCaret (   hwnd, caret, 0, 0   ) == 0 ) ::MessageBox ( 0, "CreateCaret", "CreateCaret", 0 );

                 if ( ::SetCaretPos ( 100, 100 ) == 0 ) ::MessageBox ( 0, "SetCaretPos", "SetCaretPos", 0 );

                 if ( ::ShowCaret ( hwnd ) == 0 ) ::MessageBox ( 0, "ShowCaret", "ShowCaret", 0 );
             }
             break;
        case WM_KILLFOCUS:
             {
                 if ( ::DestroyCaret (  ) == 0 ) ::MessageBox ( 0, "DestroyCaret", "DestroyCaret", 0 );
             }
             break;
       default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

2 个答案:

答案 0 :(得分:0)

好吧,问题是窗户如何处理插入符颜色。

基本上,如果位图颜色与相关窗口的客户区域的背景颜色相同,则生成的插入符号为黑色,如果位图颜色与相关客户区域的背景颜色相反窗口然后得到的插入符号为白色。我们可以使用Photoshop的反转选项来创建白色插图或者可以在内存中创建位图。

用其他黑色或白色颜色创建插入符号更复杂,但这个想法是相同的。

如果相关窗口或插入符号的背景不是纯色,那么它就会变得更加复杂。


取自:Microsoft website

要创建插入符号,请首先使用所需的模式创建位图。要显示插入符,Windows异或(XOR)并使用与客户端窗口背景相对应的位图结果(结果的NOT)。因此,要创建白色插入符,请创建一个位图,当与窗口背景进行异或时将具有相反的值以创建白色。当插入符号闪烁时,Windows使用位图的反转XOR&#39; d和背景绘制插入符号;这会在屏幕上显示白色闪烁。

答案 1 :(得分:-2)

在CreateCaret(hwnd,caret,0,0)中,指定0宽度和0大小,在那里指定非零大小。此外,确保在您的窗口区域内设置插入位置,然后只有您能够看到它。 检查一下:http://msdn.microsoft.com/en-us/library/windows/desktop/ms648398(v=vs.85).aspx 对于位图,使用类似于下面的代码:

// Load the application-defined caret resource. 
    caret = LoadBitmap(hinst, MAKEINTRESOURCE(120));

// Create a bitmap caret. 

    CreateCaret(hwnd, caret, 0, 0);