Skip to content

Commit

Permalink
New ut::GetCurrModuleHinst() method added.
Browse files Browse the repository at this point in the history
Docs update.
  • Loading branch information
jovibor committed Jan 8, 2025
1 parent be083ab commit f307500
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
21 changes: 11 additions & 10 deletions HexCtrl/src/CHexCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ auto CHexDlgAbout::OnInitDialog(const MSG& msg)->INT_PTR

auto CHexDlgAbout::OnLButtonDown(const MSG& msg)->INT_PTR
{
const POINT pt { .x { ut::GET_X_LPARAM(msg.lParam) }, .y { ut::GET_Y_LPARAM(msg.lParam) } };
const POINT pt { .x { ut::GetXLPARAM(msg.lParam) }, .y { ut::GetYLPARAM(msg.lParam) } };
const auto hWnd = m_Wnd.ChildWindowFromPoint(pt);
if (hWnd != m_WndLink) {
m_fLBDownLink = false;
Expand All @@ -169,7 +169,7 @@ auto CHexDlgAbout::OnLButtonDown(const MSG& msg)->INT_PTR

auto CHexDlgAbout::OnLButtonUp(const MSG& msg) -> INT_PTR
{
const POINT pt { .x { ut::GET_X_LPARAM(msg.lParam) }, .y { ut::GET_Y_LPARAM(msg.lParam) } };
const POINT pt { .x { ut::GetXLPARAM(msg.lParam) }, .y { ut::GetYLPARAM(msg.lParam) } };
const auto hWnd = m_Wnd.ChildWindowFromPoint(pt);
if (hWnd != m_WndLink) {
m_fLBDownLink = false;
Expand All @@ -185,7 +185,7 @@ auto CHexDlgAbout::OnLButtonUp(const MSG& msg) -> INT_PTR

auto CHexDlgAbout::OnMouseMove(const MSG& msg)->INT_PTR
{
const POINT pt { .x { ut::GET_X_LPARAM(msg.lParam) }, .y { ut::GET_Y_LPARAM(msg.lParam) } };
const POINT pt { .x { ut::GetXLPARAM(msg.lParam) }, .y { ut::GetYLPARAM(msg.lParam) } };
const auto hWnd = m_Wnd.ChildWindowFromPoint(pt);
if (hWnd == nullptr)
return FALSE;
Expand Down Expand Up @@ -326,7 +326,7 @@ bool CHexCtrl::Create(const HEXCREATE& hcs)
m_stColors = *hcs.pColors;
}

m_hInstRes = hcs.hInstRes != nullptr ? hcs.hInstRes : ::GetModuleHandleW(nullptr);
m_hInstRes = hcs.hInstRes != nullptr ? hcs.hInstRes : ut::GetCurrModuleHinst();
m_dwCapacity = std::clamp(hcs.dwCapacity, 1UL, 100UL);
m_flScrollRatio = hcs.flScrollRatio;
m_fScrollLines = hcs.fScrollLines;
Expand Down Expand Up @@ -6887,7 +6887,7 @@ auto CHexCtrl::OnCommand(const MSG& msg)->LRESULT

auto CHexCtrl::OnContextMenu(const MSG& msg)->LRESULT
{
const POINT pt { .x { ut::GET_X_LPARAM(msg.lParam) }, .y { ut::GET_Y_LPARAM(msg.lParam) } };
const POINT pt { .x { ut::GetXLPARAM(msg.lParam) }, .y { ut::GetYLPARAM(msg.lParam) } };

//Notify parent that we are about to display a context menu.
const HEXMENUINFO hmi { .hdr { m_Wnd, static_cast<UINT>(m_Wnd.GetDlgCtrlID()), HEXCTRL_MSG_CONTEXTMENU },
Expand All @@ -6910,8 +6910,9 @@ auto CHexCtrl::OnDestroy()->LRESULT
//"If the specified window is a parent or owner window, DestroyWindow automatically destroys the associated
//child or owned windows when it destroys the parent or owner window. The function first destroys child or
//owned windows, and then it destroys the parent or owner window."
//But it doesn't seem to always be the case for owned dialog windows in some environments.
//These DestroyDlg() calls to make sure the dialogs are always properly destroyed.
//But this doesn't seem to always be the case for owned dialog windows in some environments (mainly when
//IHexCtrl is a child of MFC's CView class).
//These DestroyDlg calls to make sure the dialogs are always properly destroyed.

ClearData();
m_pDlgBkmMgr->DestroyDlg();
Expand Down Expand Up @@ -7100,7 +7101,7 @@ auto CHexCtrl::OnKeyUp(const MSG& /*msg*/)->LRESULT

auto CHexCtrl::OnLButtonDblClk(const MSG& msg)->LRESULT
{
const POINT pt { .x { ut::GET_X_LPARAM(msg.lParam) }, .y { ut::GET_Y_LPARAM(msg.lParam) } };
const POINT pt { .x { ut::GetXLPARAM(msg.lParam) }, .y { ut::GetYLPARAM(msg.lParam) } };
const auto nFlags = static_cast<UINT>(msg.wParam);

if ((pt.x + static_cast<long>(m_pScrollH->GetScrollPos())) < m_iSecondVertLinePx) { //DblClick on "Offset" area.
Expand Down Expand Up @@ -7138,7 +7139,7 @@ auto CHexCtrl::OnLButtonDblClk(const MSG& msg)->LRESULT

auto CHexCtrl::OnLButtonDown(const MSG& msg)->LRESULT
{
const POINT pt { .x { ut::GET_X_LPARAM(msg.lParam) }, .y { ut::GET_Y_LPARAM(msg.lParam) } };
const POINT pt { .x { ut::GetXLPARAM(msg.lParam) }, .y { ut::GetYLPARAM(msg.lParam) } };
const auto nFlags = static_cast<UINT>(msg.wParam);

m_Wnd.SetFocus(); //SetFocus is vital to give proper keyboard input to the main HexCtrl window.
Expand Down Expand Up @@ -7198,7 +7199,7 @@ auto CHexCtrl::OnLButtonUp(const MSG& /*msg*/)->LRESULT

auto CHexCtrl::OnMouseMove(const MSG& msg)->LRESULT
{
POINT pt { .x { ut::GET_X_LPARAM(msg.lParam) }, .y { ut::GET_Y_LPARAM(msg.lParam) } };
POINT pt { .x { ut::GetXLPARAM(msg.lParam) }, .y { ut::GetYLPARAM(msg.lParam) } };
const auto optHit = HitTest(pt);

if (m_fLMousePressed) {
Expand Down
2 changes: 1 addition & 1 deletion HexCtrl/src/Dialogs/CHexDlgTemplMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ auto CHexDlgTemplMgr::OnMouseMove(const MSG& msg)->INT_PTR
static constexpr auto iMinTreeWidth = 100; //Tree control minimum allowed width.
static const auto hCurResize = static_cast<HCURSOR>(::LoadImageW(nullptr, IDC_SIZEWE, IMAGE_CURSOR, 0, 0, LR_SHARED));
static const auto hCurArrow = static_cast<HCURSOR>(::LoadImageW(nullptr, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED));
const POINT pt { .x { ut::GET_X_LPARAM(msg.lParam) }, .y { ut::GET_Y_LPARAM(msg.lParam) } };
const POINT pt { .x { ut::GetXLPARAM(msg.lParam) }, .y { ut::GetYLPARAM(msg.lParam) } };

const auto hWndList = wnd::CWnd::FromHandle(m_ListEx.GetHWND());
auto rcList = hWndList.GetWindowRect();
Expand Down
14 changes: 11 additions & 3 deletions HexCtrl/src/HexUtility.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,22 @@ export namespace HEXCTRL::INTERNAL::ut { //Utility methods and stuff.
}

//Replicates GET_X_LPARAM macro from the windowsx.h.
[[nodiscard]] constexpr int GET_X_LPARAM(LPARAM lParam) {
[[nodiscard]] constexpr int GetXLPARAM(LPARAM lParam) {
return (static_cast<int>(static_cast<short>(static_cast<WORD>((static_cast<DWORD_PTR>(lParam)) & 0xFFFF))));
}

[[nodiscard]] constexpr int GET_Y_LPARAM(LPARAM lParam) {
return GET_X_LPARAM(lParam >> 16);
[[nodiscard]] constexpr int GetYLPARAM(LPARAM lParam) {
return GetXLPARAM(lParam >> 16);
}

//Returns hInstance of a current module, whether it is a .exe or .dll.
[[nodiscard]] auto GetCurrModuleHinst() -> HINSTANCE {
HINSTANCE hInst { };
::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(&GetCurrModuleHinst), &hInst);
return hInst;
};

#if defined(DEBUG) || defined(_DEBUG)
void DBG_REPORT(const wchar_t* pMsg, const std::source_location& loc = std::source_location::current()) {
_wassert(pMsg, StrToWstr(loc.file_name()).data(), loc.line());
Expand Down
51 changes: 33 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ First you need to create a **HexCtrl** object:
```cpp
HEXCTRL::IHexCtrlPtr myHex { HEXCTRL::CreateHexCtrl() };
```
Then call the [`IHexCtrl::Create`](#create) method, which takes the [`HEXCREATE`](#hexcreate) struct with the all necessary information for the **HexCtrl** creation. The `HEXCREATE::dwStyle` and `dwExStyle` are [Window](https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles) and [Extended Window](https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles) styles respectively, set these styles according to your needs. For all available options see the [`HEXCREATE`](#hexcreate) struct description.
Then call the [`IHexCtrl::Create`](#create) method, which takes the [`HEXCREATE`](#hexcreate) struct with the all necessary information for the **HexCtrl** creation. The `HEXCREATE::dwStyle` and `dwExStyle` are [window](https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles) and [extended window](https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles) styles respectively, set these styles according to your needs. For all available options see the [`HEXCREATE`](#hexcreate) struct description.

### [](#)In Dialog
To use **HexCtrl** in a Dialog you can create it with the [Classic Approach](#classic-approach): call [`Create`](#create) method and provide all the necessary information.
Expand Down Expand Up @@ -834,32 +834,46 @@ struct HEXCOLORS {
COLORREF clrBkSel { GetSysColor(COLOR_HIGHLIGHT) }; //Background color of the selected Hex/Text.
COLORREF clrBkBkm { RGB(240, 240, 0) }; //Bookmarks background color.
COLORREF clrBkDataInterp { RGB(147, 58, 22) }; //Data Interpreter Bk color.
COLORREF clrBkInfoBar { GetSysColor(COLOR_BTNFACE) }; //Background color of the bottom Info bar.
COLORREF clrBkInfoBar { GetSysColor(COLOR_3DFACE) }; //Background color of the bottom Info bar.
COLORREF clrBkCaret { RGB(0, 0, 255) }; //Caret background color.
COLORREF clrBkCaretSel { RGB(0, 0, 200) }; //Caret background color in selection.
};
using PCHEXCOLORS = const HEXCOLORS*;
```

### [](#)HEXCREATE
The main initialization struct used for the **HexCtrl** creation.
```cpp
struct HEXCREATE {
HWND hWndParent { }; //Parent window handle.
const HEXCOLORS* pColors { }; //HexCtrl colors, nullptr for default.
const LOGFONTW* pLogFont { }; //Monospaced font for HexCtrl, nullptr for default.
RECT rect { }; //Initial window rect.
UINT uID { }; //Control ID if it's a child window.
DWORD dwStyle { }; //Window styles.
DWORD dwExStyle { }; //Extended window styles.
DWORD dwCapacity { 16UL }; //Initial capacity size.
DWORD dwGroupSize { 1UL }; //Initial data grouping size.
float flScrollRatio { 1.0F }; //Either a screen-ratio or lines amount to scroll with Page-scroll (mouse-wheel).
bool fScrollLines { false }; //Treat flScrollRatio as screen-ratio (false) or as amount of lines (true).
bool fInfoBar { true }; //Show bottom Info bar or not.
bool fOffsetHex { true }; //Show offset digits as Hex or Decimal.
bool fCustom { false }; //If it's a custom control in a dialog.
HINSTANCE hInstRes { }; //Hinstance of the HexCtrl resources, nullptr for current module.
HWND hWndParent { }; //Parent window handle.
PCHEXCOLORS pColors { }; //HexCtrl colors, nullptr for default.
const LOGFONTW* pLogFont { }; //Monospaced font for HexCtrl, nullptr for default.
RECT rect { }; //Initial window rect.
UINT uID { }; //Control ID if it's a child window.
DWORD dwStyle { }; //Window styles.
DWORD dwExStyle { }; //Extended window styles.
DWORD dwCapacity { 16UL }; //Initial capacity size.
DWORD dwGroupSize { 1UL }; //Initial data grouping size.
float flScrollRatio { 1.0F }; //Either a screen-ratio or lines amount to scroll with Page-scroll (mouse-wheel).
bool fScrollLines { false }; //Treat flScrollRatio as screen-ratio (false) or as amount of lines (true).
bool fInfoBar { true }; //Show bottom Info bar or not.
bool fOffsetHex { true }; //Show offset digits as Hex or Decimal.
bool fCustom { false }; //If it's a custom control in a dialog.
};
```
#### Members:
**HINSTANCE hInstRes**

The `hInstRes` member allows you to provide an alternative `HINSTANCE` of a module where all **HexCtrl** resources (dialogs, menu, etc...) reside. By default **HexCtrl** uses its current module, whether it's a `.exe` or `.dll`.

**DWORD dwStyle**

Standard [window style](https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles) for the main **HexCtrl** window.

**DWORD dwExStyle**

Standard [extended window style](https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles) for the main **HexCtrl** window.

### [](#)HEXDATA
The main struct to set a data to display in the **HexCtrl**.
Expand All @@ -874,9 +888,10 @@ struct HEXDATA {
bool fHighLatency { false }; //Do not redraw until scroll thumb is released.
};
```
#### Members:
**ULONGLONG ullMaxVirtOffset**

#### [](#)ullMaxVirtOffset
Used to set maximum Virtual data offset. This is needed for the offset digits amount calculation.
Used to set maximum virtual data offset in virtual data mode. This is needed for the offset digits amount calculation.

### [](#)HEXDATAINFO
Struct for a data information used in [`IHexVirtData`](#virtual-data-mode).
Expand Down

0 comments on commit f307500

Please sign in to comment.