【发布时间】: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 文件