【问题标题】:Bitmap Caret not working WinApi位图插入符号不起作用 WinApi
【发布时间】:2014-07-17 09:28:22
【问题描述】:

编辑: 现在这很奇怪,似乎 ::CreateCaret 以某种方式解释了位图的颜色错误,当我使用 RGB ( 255,255,255 )时,生成的插入符号是黑色的,当我使用 RGB ( 53,53,52 )时如果是 RGB ( 253,253, 252 ) 的结果插入符号,并且如果我将 RGB ( 0,0,0 ) 用于位图,则插入符号根本不会显示。


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

我在 Windows 7 中工作。

这是截图:

这是我的代码:

资源.rc

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


IDB_CARET  BITMAP  DISCARDABLE  "caret.bmp"

资源.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;
}

【问题讨论】:

  • 检查所有 API 调用的返回值。
  • @AlexFarber 我做到了,一切正常
  • 不,您没有检查任何返回值。
  • @DavidHeffernan 你是怎么得出这个结论的?以下是检查返回值的方法(原始帖子已更新)。
  • 我得出这个结论的原因很简单,即您的原始代码没有检查任何返回值。我想你可以有用地提供一个屏幕截图和链接为资源的 .bmp 文件

标签: c++ winapi


【解决方案1】:

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

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

用黑色或白色以外的颜色创建插入符号更复杂,但想法是一样的。

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


取自:Microsoft website

要创建插入符号,首先创建一个具有所需图案的位图。为了显示插入符号,Windows 异或 (XOR) 并将位图的结果(结果的非)与客户端窗口的背景相反。因此,要创建一个白色插入符号,请创建一个位图,当与窗口背景进行异或运算时,该位图将具有一个相反的值,该值将创建一个白色。当插入符号闪烁时,Windows 使用与背景异或的位图的反向来绘制插入符号;这会在屏幕上产生白色闪烁。

【讨论】:

    【解决方案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); 
    

    【讨论】:

    • 文档非常清楚地指出,如果传递位图,这些参数将被忽略
    • 我可以看到插入符号,但它全是黑色的,并且没有显示应该显示的位图。
    • @DebasishJana 问题中的代码确实调用了LoadBitmap
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多