Re: Getting windows mousepointer icon = black square
Posted: Fri Apr 16, 2021 3:18 pm
You cans use them but for monochrome cursors, you need to do what wxWidgets attempts to do, i.e., properly blend the two parts of the mask...
Official forum for the wxWidgets Cross-Platform GUI Toolkit
https://forums.wxwidgets.org/
You cans use them but for monochrome cursors, you need to do what wxWidgets attempts to do, i.e., properly blend the two parts of the mask...
Code: Select all
BITMAP bmMask,bmCurs;
if ( !ii.hbmMask ) {
wxLogMessage("NO Cursor Mask???");
return false;
}else{
if( GetObject(ii.hbmMask, sizeof(bmMask), &bmMask) != sizeof(bmMask) ) {
wxLogError("Could not get BITMAP from the mask.");
return false;
}
wxLogMessage("Cursor Mask: size = %dx%d pixels, depth = %d.",
bmMask.bmWidth, bmMask.bmHeight, bmMask.bmBitsPixel);
}
if ( !ii.hbmColor ) {
wxLogMessage("The cursor is monochrome.");
} else {
wxLogMessage("The cursor is in color.");
if( GetObject(ii.hbmColor, sizeof(bmCurs), &bmCurs) != sizeof(bmCurs) ) {
wxLogError("Could not get BITMAP from the mask.");
return false;
}
wxLogMessage("Cursor Mask: size = %dx%d pixels, depth = %d.",
bmCurs.bmWidth, bmCurs.bmHeight, bmCurs.bmBitsPixel);
if (!bitmap.InitFromHBITMAP(ii.hbmColor, bmCurs.bmWidth, bmCurs.bmHeight, bmCurs.bmBitsPixel) ) {
wxLogError("Could not initialize Bitmap from HBITMAP.");
return false;
}
wxMask bmMask(ii.hbmMask);
bitmap.SetMask(&bmMask);
}
I think there is a bug, you are always missing "!" when checking the result of ::GetObject(), this function returns zero on failure, non-zero otherwise. I did not read the rest of the code.
I check his return value against sizeof(BITMAP): whatever result (0 or not) not equal to the size of that BITMAP means a failure.
Sorry for having missing that check, I did not look past the end of the function closing parentheses.
IIf the function succeeds, and lpvObject is a valid pointer, the return value is the number of bytes stored into the buffer.
Code: Select all
::DrawIconEx(dc.GetHDC(), x, y, hCursor0, 0, 0, nullptr, DI_NORMAL);
Code: Select all
#include <wx/wx.h>
#include <wx/msw/wrapwin.h>
bool GetFromCurrentCursor(wxIcon& icon, wxBitmap& bitmap)
{
CURSORINFO ci{0};
wxIcon tmpIcon;
wxBitmap tmpBitmap;
ci.cbSize = sizeof(ci);
if ( !::GetCursorInfo(&ci) || (ci.flags & CURSOR_SHOWING) == 0 )
{
wxLogDebug("Could not get cursor info.");
return false;
}
if ( !tmpIcon.CreateFromHICON(WXHICON(ci.hCursor)) )
{
wxLogDebug("Could not create wxIcon from HCURSOR.");
return false;
}
if ( !tmpBitmap.CopyFromIcon(tmpIcon) )
{
wxLogDebug("Could not create wxBitmap from wxIcon.");
return false;
}
icon = tmpIcon;
bitmap = tmpBitmap;
return true;
}
class MyFrame: public wxFrame
{
public:
MyFrame() : wxFrame (nullptr, wxID_ANY, "Test")
{
SetBackgroundColour(*wxRED);
SetClientSize(200, 100);
m_timer.Bind(wxEVT_TIMER,
[this](wxTimerEvent&) { GetFromCurrentCursor(m_icon, m_bitmap); Refresh(); Update(); });
m_timer.Start(100);
Bind(wxEVT_PAINT, &MyFrame::OnPaint, this);
}
private:
wxTimer m_timer;
wxIcon m_icon;
wxBitmap m_bitmap;
void OnPaint(wxPaintEvent&)
{
wxPaintDC dc(this);
if ( !m_icon.IsOk() || !m_bitmap.IsOk() )
return;
dc.DrawIcon(m_icon, 20, 20);
dc.DrawBitmap(m_bitmap, 100, 20, true);
}
};
class MyApp : public wxApp
{
public:
bool OnInit() override
{
(new MyFrame())->Show();
return true;
}
}; wxIMPLEMENT_APP(MyApp);
Great!.PB wrote: ↑Sat Apr 17, 2021 8:13 am The issue just has been fixed in wxWidgets master: https://github.com/wxWidgets/wxWidgets/ ... 6347fef477
Actually, looking at the code, I think it should be inside the block guarded by statement
Code: Select all
if ( ::GetObject(hbmp, sizeof(BITMAP), (LPSTR) &bm) )