Added support for custom cursor/icon

This commit is contained in:
Lynix
2012-06-24 13:28:24 +02:00
parent d851e35a56
commit bcf36e7a14
14 changed files with 406 additions and 7 deletions

View File

@@ -0,0 +1,52 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Win32/CursorImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Image.hpp>
#include <Nazara/Utility/Debug.hpp>
bool NzCursorImpl::Create(const NzImage& cursor, int hotSpotX, int hotSpotY)
{
NzImage windowsCursor(cursor);
if (!windowsCursor.Convert(nzPixelFormat_BGRA8))
{
NazaraError("Failed to convert cursor to BGRA8");
return false;
}
HBITMAP bitmap = CreateBitmap(windowsCursor.GetWidth(), windowsCursor.GetHeight(), 1, 32, windowsCursor.GetConstPixels());
HBITMAP monoBitmap = CreateBitmap(windowsCursor.GetWidth(), windowsCursor.GetHeight(), 1, 1, nullptr);
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms648052(v=vs.85).aspx
ICONINFO iconInfo;
iconInfo.fIcon = FALSE;
iconInfo.xHotspot = hotSpotX;
iconInfo.yHotspot = hotSpotY;
iconInfo.hbmMask = monoBitmap;
iconInfo.hbmColor = bitmap;
m_cursor = CreateIconIndirect(&iconInfo);
DeleteObject(bitmap);
DeleteObject(monoBitmap);
if (!m_cursor)
{
NazaraError("Failed to create cursor: " + NzGetLastSystemError());
return false;
}
return true;
}
void NzCursorImpl::Destroy()
{
DestroyIcon(m_cursor);
}
HCURSOR NzCursorImpl::GetCursor()
{
return m_cursor;
}

View File

@@ -0,0 +1,26 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CURSORIMPL_HPP
#define NAZARA_CURSORIMPL_HPP
#include <windows.h>
class NzImage;
class NzCursorImpl
{
public:
bool Create(const NzImage& image, int hotSpotX, int hotSpotY);
void Destroy();
HCURSOR GetCursor();
private:
HICON m_cursor = nullptr;
};
#endif // NAZARA_CURSORIMPL_HPP

View File

@@ -0,0 +1,49 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Win32/IconImpl.hpp>
#include <Nazara/Utility/Image.hpp>
#include <Nazara/Utility/Debug.hpp>
bool NzIconImpl::Create(const NzImage& icon)
{
NzImage windowsIcon(icon);
if (!windowsIcon.Convert(nzPixelFormat_BGRA8))
{
NazaraError("Failed to convert icon to BGRA8");
return false;
}
HBITMAP bitmap = CreateBitmap(windowsIcon.GetWidth(), windowsIcon.GetHeight(), 1, 32, windowsIcon.GetConstPixels());
HBITMAP monoBitmap = CreateBitmap(windowsIcon.GetWidth(), windowsIcon.GetHeight(), 1, 1, nullptr);
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms648052(v=vs.85).aspx
ICONINFO iconInfo;
iconInfo.fIcon = TRUE;
iconInfo.hbmMask = monoBitmap;
iconInfo.hbmColor = bitmap;
m_icon = CreateIconIndirect(&iconInfo);
DeleteObject(bitmap);
DeleteObject(monoBitmap);
if (!m_icon)
{
NazaraError("Failed to create icon: " + NzGetLastSystemError());
return false;
}
return true;
}
void NzIconImpl::Destroy()
{
DestroyIcon(m_icon);
}
HICON NzIconImpl::GetIcon()
{
return m_icon;
}

View File

@@ -0,0 +1,26 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_ICONIMPL_HPP
#define NAZARA_ICONIMPL_HPP
#include <windows.h>
class NzImage;
class NzIconImpl
{
public:
bool Create(const NzImage& image);
void Destroy();
HICON GetIcon();
private:
HICON m_icon = nullptr;
};
#endif // NAZARA_ICONIMPL_HPP

View File

@@ -12,6 +12,11 @@
#include <Nazara/Core/Thread.hpp>
#include <Nazara/Core/ThreadCondition.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Cursor.hpp>
#include <Nazara/Utility/Image.hpp>
#include <Nazara/Utility/Icon.hpp>
#include <Nazara/Utility/Win32/CursorImpl.hpp>
#include <Nazara/Utility/Win32/IconImpl.hpp>
#include <cstdio>
#include <windowsx.h>
#include <Nazara/Utility/Debug.hpp>
@@ -274,9 +279,6 @@ bool NzWindowImpl::IsVisible() const
void NzWindowImpl::SetCursor(nzWindowCursor cursor)
{
// Pas besoin de libérer le curseur par la suite s'il est partagé
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms648045(v=vs.85).aspx
switch (cursor)
{
case nzWindowCursor_Crosshair:
@@ -337,9 +339,16 @@ void NzWindowImpl::SetCursor(nzWindowCursor cursor)
break;
}
// Pas besoin de libérer le curseur par la suite s'il est partagé
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms648045(v=vs.85).aspx
::SetCursor(m_cursor);
}
void NzWindowImpl::SetCursor(const NzCursor& cursor)
{
m_cursor = cursor.m_impl->GetCursor();
}
void NzWindowImpl::SetEventListener(bool listener)
{
if (m_ownsWindow)
@@ -365,6 +374,14 @@ void NzWindowImpl::SetFocus()
SetForegroundWindow(m_handle);
}
void NzWindowImpl::SetIcon(const NzIcon& icon)
{
HICON iconHandle = icon.m_impl->GetIcon();
SendMessage(m_handle, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(iconHandle));
SendMessage(m_handle, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(iconHandle));
}
void NzWindowImpl::SetMaximumSize(int width, int height)
{
RECT rect = {0, 0, width, height};

View File

@@ -57,8 +57,10 @@ class NzWindowImpl : NzNonCopyable
bool IsVisible() const;
void SetCursor(nzWindowCursor cursor);
void SetCursor(const NzCursor& cursor);
void SetEventListener(bool listener);
void SetFocus();
void SetIcon(const NzIcon& icon);
void SetMaximumSize(int width, int height);
void SetMinimumSize(int width, int height);
void SetPosition(int x, int y);