如何从c ++更改窗口闪烁光标形状? [英] How to change windows blink cursor shape from c++?

查看:782
本文介绍了如何从c ++更改窗口闪烁光标形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将窗口眨眼光标的形状从默认(|)的垂直变为水平,就像dos(_)中使用的那样。




实际上称为光标,而不是光标。这可能是混乱来自的地方,为什么搜索解决方案没有产生非常多的使用。 NullPonyPointer的评论也反映了这种常见的混乱。 SetCursor 函数确实是您想要更改鼠标光标,但它不会改变插入符号。



幸运的是,有一组Windows函数可以处理插入符号: CreateCaret ShowCaret HideCaret SetCaretPos DestroyCaret 。还有一些其他的操作闪烁时间,但我建议坚持用户的当前设置(这将是默认值)。



首先,一点背景。我强烈建议您阅读两篇介绍MSDN文章的关于插入符号使用插入点。但是这里有一个简短的总结:插入符由一个窗口拥有;特别是当前具有焦点的窗口。此窗口可能类似于文本框控件。当窗口接收到焦点时,它创建一个使用的插入符号,然后当它失去焦点时,它会破坏它的插入符号。显然,如果您不手动执行任何操作,您将收到默认实现。



现在,示例代码。因为我喜欢糖果机接口,我将它包装在一个函数:

  bool CreateCustomCaret(HWND hWnd,int width,int height,int x,int y)
{
//为接收焦点的控件创建插入符号。
if(!CreateCaret(hWnd,/ * handle to the window that will own the caret * /
NULL,/ *使用指定的大小创建一个实体插入符号* /
width,/ * width ,以逻辑单位* /
height))/ *插入符号的高度,以逻辑单位* /
return false;

//在窗口中设置插入符的位置。
if(!SetCaretPos(x,y))
return false;

//显示插入符号。它将开始自动闪烁。
if(!ShowCaret(hWnd))
return false;

return true;
}

然后,响应 WM_SETFOCUS EN_SETFOCUS 或类似的通知,我会调用 CreateCustomCaret 函数。并响应 WM_KILLFOCUS EN_KILLFOCUS 或另一个类似的通知,我会呼叫 DestroyCaret()



或者, CreateCustomCaret 可以从位图创建插入符号。我可能提供以下重载:

  bool CreateCustomCaret(HWND hWnd,HBITMAP hbmp,int x,int y)
{
//为接收焦点的控件创建插入符号。
if(!CreateCaret(hWnd,/ * handle to the window that will own the caret * /
hBmp,/ * create a caret using specified bitmap * /
0,0) *为位图忽略宽度和高度参数* /
return false;

//在窗口中设置插入符的位置。
if(!SetCaretPos(x,y))
return false;

//显示插入符号。它将开始自动闪烁。
if(!ShowCaret(hWnd))
return false;

return true;
}


How to change windows blink cursor shape from vertical which is by default ( | ) to horizontal like that used in dos ( _ ).

Is there some good function that take care about that?

OS: win7

解决方案

This is actually called a caret, rather than a cursor. That's probably where the confusion comes from, and why searching for a solution didn't yield very much of use. NullPonyPointer's comment reflects this common confusion as well. The SetCursor function is indeed what you would want to change the mouse cursor, but it won't work to change the caret.

Fortunately, there is a whole group of Windows functions that work with carets: CreateCaret, ShowCaret, HideCaret, SetCaretPos, and DestroyCaret. There are some others for manipulating blink time, but I recommend sticking to the user's current settings (which will be the default).

First, a bit of background. I strongly recommend reading the two introductory MSDN articles about carets and on using carets. But here's a quick summary: The caret is owned by a window; in particular, the window that currently has the focus. This window will likely be something like a text box control. When the window receives the focus, it creates a caret to use, and then when it loses the focus, it destroys its caret. Obviously, if you don't do any of this manually, you will receive the default implementation.

Now, the sample code. Because I like candy machine interfaces, I'd wrap it in a function:

bool CreateCustomCaret(HWND hWnd, int width, int height, int x, int y)
{
    // Create the caret for the control receiving the focus.
    if (!CreateCaret(hWnd,    /* handle to the window that will own the caret */
                     NULL,    /* create a solid caret using specified size    */
                     width,   /* width of caret, in logical units             */
                     height)) /* height of caret, in logical units            */
        return false;

    // Set the position of the caret in the window.
    if (!SetCaretPos(x, y))
        return false;

    // Show the caret. It will begin flashing automatically.
    if (!ShowCaret(hWnd))
        return false;

    return true;
}

Then, in response to WM_SETFOCUS, EN_SETFOCUS, or a similar notification, I would call the CreateCustomCaret function. And in response to WM_KILLFOCUS, EN_KILLFOCUS, or another similar notification, I would call DestroyCaret().

Alternatively, CreateCustomCaret could have created the caret from a bitmap. I might provide the following overload:

bool CreateCustomCaret(HWND hWnd, HBITMAP hbmp, int x, int y)
{
    // Create the caret for the control receiving the focus.
    if (!CreateCaret(hWnd,   /* handle to the window that will own the caret   */
                     hBmp,   /* create a caret using specified bitmap          */
                     0, 0))  /* width and height parameters ignored for bitmap */
        return false;

    // Set the position of the caret in the window.
    if (!SetCaretPos(x, y))
        return false;

    // Show the caret. It will begin flashing automatically.
    if (!ShowCaret(hWnd))
        return false;

    return true;
}

这篇关于如何从c ++更改窗口闪烁光标形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆