QPainter painter object outside paintevent - visual-c++

I am trying to draw a simple two dimensional figure in a QWidget window.
There is a paintEvent defined and a painter object of the QPainter class is also defined.
My drawing contains elements which I need to draw more than once at various locations, such as lines, text etc. For this purpose, I am using functions to draw these lines with varying positions. Similarly for text. In order to make the program shorter, also modular.
The paintEvent function is calling functions which are used to calculate and draw.
How do I pass the QPainter painter object defined in the paintEvent into the functions.
for e.g.
void Classname::drawText(QString text, int PosX, int PosY, QPainter painter)
{
QSize size=this->size();
QFont times("Helvetica [Cronyx]", 10);
QFontMetrics box(times);
int boxWidth = box.width(text);
int boxHeight = box.height();
painter.setFont(times);
painter.setPen(Qt::white);
painter.drawText(PosX,PosY,text);
}
then I get an error where the vc++ environment is telling me that the typename is not allowed for the painter object of QPainter class.
If I define QPainter painter1 object as shown below:
void Classname::drawText(QString text, int PosX, int PosY, QPainter painter)
{
QPainter painter1;
QSize size=this->size();
QFont times("Helvetica [Cronyx]", 10);
QFontMetrics box(times);
int boxWidth = box.width(text);
int boxHeight = box.height();
painter.setFont(times);
painter.setPen(Qt::white);
painter.drawText(PosX,PosY,text);
}
the program compiles but there is no output.
This is a part of the code, I am defining objects of the QPainter class in all the functions.
I read this thread, but the instructions are not clear. Must the begin() and end() function be called at all instances of drawing or just once in the paintEvent function?

As you mentioned you shall implement those functions in your class.
In your header:
class Class
{
// ...
protected:
virtual void paintEvent( QPaintEvent* aEvent ) override;
private:
void drawText( QPainter* aPainter, const QString& aText, int aPosX, int aPosY );
// void drawLine( ... );
};
In your source:
void Class::paintEvent( QPaintEvent* aEvent )
{
QPainter painter( this );
// ...
drawText( &painter/*, ... */ );
drawLine( &painter/*, ... */ );
}
void Class::drawText( QPainter* aPainter, const QString& aText, int aPosX, int aPosY )
{
// Your drawing ...
}

Related

Unable to change the color of my dialog boxes in my application

I am attempting to create a "dark mode" for my Windows C++ app partially for fun, partially to try and fully comprehend the message passing in MFC, but I'm running into some really odd issues that I can't find explained anywhere.
I've spent the better part of today attempting to figure this out and will try my best to cite the many sources I've looked at and attempted to implement.
I believe I've successfully written message handlers for both WM_CTLCOLOR and WM_ERASEBKGND based on example code from this answer, but they don't seem to have any effect on my dialogs. I've cut the code down here, but am hoping that I've provided enough to expose my issue. If that still isn't enough, I can (reluctantly) share the entire repo.
SoftwareDlg.h
#ifndef _SOFTWAREDLG_H_INCLUDED_
#define _SOFTWAREDLG_H_INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class SoftwareDlg : public CDialog
{
// Construction
public:
SoftwareDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(SoftwareDlg)
enum { IDD = IDD_SOFTWARE_DIALOG };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(SoftwareDlg)
public:
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
BOOL PreTranslateMessage(MSG* pMsg);
CFont m_font;
CRichEditCtrl m_richEditCtrl;
// Generated message map functions
//{{AFX_MSG(SoftwareDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnDestroy();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
public:
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
/////////////////////////////////////////////////////////////////////////////
#endif
/////////////////////////////////////////////////////////////////////////////
SoftwareDlg.cpp
#include "stdafx.h"
#include <Windows.h>
#include "AboutDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//Windows Dialog inherited function overrides
/////////////////////////////////////////////////////////////////////////////
// SoftwareDlg dialog
/////////////////////////////////////////////////////////////////////////////
SoftwareDlg::SoftwareDlg(CWnd* pParent /*=NULL*/)
: CDialog(SoftwareDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(SoftwareDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void SoftwareDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(SoftwareDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(SoftwareDlg, CDialog)
//{{AFX_MSG_MAP(SoftwareDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// SoftwareDlg message handlers
/////////////////////////////////////////////////////////////////////////////
BOOL SoftwareDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
CWnd* pPlaceholder = GetDlgItem(IDC_PLACEHOLDER);
if (pPlaceholder)
{
CRect rect;
pPlaceholder->GetClientRect(&rect);
if (!m_richEditCtrl.Create(WS_VISIBLE | ES_READONLY | ES_MULTILINE | ES_AUTOHSCROLL | WS_HSCROLL | ES_AUTOVSCROLL | WS_VSCROLL, rect, this, 0))
return FALSE;
m_font.CreateFont(-11, 0, 0, 0, FW_REGULAR, 0, 0, 0, BALTIC_CHARSET, 0, 0, 0, 0, "Courier New");
m_richEditCtrl.SetFont(&m_font);
}
m_nTimerID = SetTimer(0x1234, 1000, NULL); //Used by OnTimer function to refresh dialog box & OSD
return TRUE; // return TRUE unless you set the focus to a control
}
void SoftwareDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
/////////////////////////////////////////////////////////////////////////////
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
/////////////////////////////////////////////////////////////////////////////
void SoftwareDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM)dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR SoftwareDlg::OnQueryDragIcon()
{
return (HCURSOR)m_hIcon;
}
void SoftwareDlg::OnTimer(UINT nIDEvent)
{
CDialog::OnTimer(nIDEvent);
}
void SoftwareDlg::OnDestroy()
{
if (m_nTimerID)
KillTimer(m_nTimerID);
m_nTimerID = NULL;
MSG msg;
while (PeekMessage(&msg, m_hWnd, WM_TIMER, WM_TIMER, PM_REMOVE));
CDialog::OnDestroy();
}
BOOL SoftwareDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
switch (pMsg->wParam)
{
case ' ':
Sleep(1000);
}
}
return CDialog::PreTranslateMessage(pMsg);
}
BOOL SoftwareDlg::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetClientRect(&rect);
CBrush myBrush(RGB(255, 0, 0)); // dialog background color
CBrush* pOld = pDC->SelectObject(&myBrush);
BOOL bRes = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
pDC->SelectObject(pOld); // restore old brush
return bRes; // CDialog::OnEraseBkgnd(pDC);
}
HBRUSH SoftwareDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// Are we painting the IDC_MYSTATIC control? We can use
m_brush.CreateSolidBrush(RGB(136, 217, 242));
//if (pWnd->GetDlgCtrlID() == IDD_SOFTWARE_DIALOG)
// Set the text color to red
pDC->SetTextColor(RGB(255, 0, 0));
// Set the background mode for text to transparent so background will show thru.
pDC->SetBkMode(TRANSPARENT);
// Return handle to our CBrush object
hbr = m_brush;
return hbr;
}
HBRUSH SoftwareDlg::CtlColor(CDC* pDC, UINT nCtlColor)
{
HBRUSH myBrush = CreateSolidBrush(RGB(136, 217, 242));
return myBrush;
}
Resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Software.rc
//
#define IDM_ABOUTBOX 0x0010
#define IDD_ABOUTBOX 100
#define IDS_ABOUTBOX 101
#define IDD_SOFTWARE_DIALOG 102
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 141
#define _APS_NEXT_COMMAND_VALUE 32792
#define _APS_NEXT_CONTROL_VALUE 1026
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
Another question, posted by the same user ~6 months earlier, was answered with somewhat similar code, but using the type of framework that contains a "WinMain" function(sorry, I can't differentiate the 2+ types yet). My program does not contain a WinMain function so I wasn't able to use the sample code directly... but another difference in this answer was that David was told to catch a WM_CTLCOLORDLG message type instead the WM_CTLCOLOR message type. I tried to catch this new message type, but IntelliSense told me it was undefined and that particular message property was completely absent from the resource view of the dialog box:
I've also attempted defining "WM_CTLCOLORDLG" myself as described on the Microsoft Docs page, but continued to get error messages when I tried handling it through "ON_MESSAGE".
My code was not an original project, but was taken from an open source sample provided with RTSS. As such, it doesn't use the standard(?) "pch.h", but "stdafx.h"(which is older I guess?). I'm not certain if that's relevant, but I feel like it may be.
I think this issue may also be causing me a lot of other growing pains as well, so any help is GREATLY appreciated.
Main problem with OP's code is that the brush is re-created every time in OnCtlColor and will keep leaking GDI handles (the debug build of MFC raises an ASSERT about this). Complete step-by-step fix below.
Declare the color and brush as members of the dialog.
class SoftwareDlg : public CDialog
{
//...
protected:
COLORREF m_color;
CBrush m_brush;
//...
Initialize the color and brush in OnInitDialog.
BOOL SoftwareDlg::OnInitDialog()
{
m_color = RGB(136, 217, 242);
m_brush.CreateSolidBrush(m_color);
CDialog::OnInitDialog();
//...
Return the brush from OnCtlColor.
HBRUSH SoftwareDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
//...
return m_brush;
}
Use the color in OnEraseBkgnd to repaint any visible area.
afx_msg BOOL SoftwareDlg::OnEraseBkgnd(CDC* pDC)
{
CRect rc;
pDC->GetClipBox(&rc);
pDC->FillSolidRect(rc, m_color);
return TRUE;
}
Set the background color to the rich edit control in OnInitDialog explicitly, since rich edit controls do not use the WM_CTLCOLOR*** messages.
BOOL SoftwareDlg::OnInitDialog()
{
//...
if (pPlaceholder)
{
//...
if (!m_richEditCtrl.Create(WS_VISIBLE | ES_READONLY | ES_MULTILINE | ES_AUTOHSCROLL | WS_HSCROLL | ES_AUTOVSCROLL | WS_VSCROLL, rect, this, 0))
return FALSE;
m_richEditCtrl.SetBackgroundColor(FALSE, m_color);
//...
Note: if using the CDialogEx::SetBackgroundColor approach proposed in the other answer, the OnCtlColor and OnEraseBkgnd parts are covered by the CDialogEx implementation. The last step is still necessary because the WM_CTLCOLOR mechanism only covers the basic controls (static, edit, button etc) and the dialog itself. Controls other than those (rich edit control, list-view control etc) need to be handled each separately.
Simply use the CDialogEx class it supports CDialogEx::SetBackgroundColor and it does all the stuff for static and button controls.

Qt uploading texture thread

I am using a QGLWidget for displaying a live Image Stream. Since uploading textures is an expensive operation I would like to avoid doing this on the GUI Thread. I have read here http://blog.qt.digia.com/blog/2011/06/03/threaded-opengl-in-4-8/ that it is possible to do this in from a different thread. Could anyone provide a short example how to do so? I am uncertain how to share the GL Context and how to make sure that there is no race condition.
This is what i currently have:
.h:
class ImageDisplay : public QGLWidget
{
Q_OBJECT
public:
ImageDisplay(QWidget* parent = 0);
void paintGL();
void resizeGL(int w, int h);
private:
QRectF plane;
GLuint texID;
public slots:
void sl_update(spImageHolder_t _myImageShPtr);
};
.cpp:
ImageDisplay::ImageDisplay(QWidget *parent)
: QGLWidget(parent)
, plane(QPointF(0,1),QSizeF(1,1))
{
QImage initImg(400,300,QImage::Format_Indexed8);
resize(initImg.size());
makeCurrent();
texID = bindTexture ( initImg, GL_TEXTURE_2D, GL_LUMINANCE, QGLContext::NoBindOption );
}
void ImageDisplay::paintGL()
{
makeCurrent();
drawTexture ( plane, texID, GL_TEXTURE_2D );
}
void ImageDisplay::resizeGL(int w, int h)
{
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1,0,1,-1,1);
glMatrixMode (GL_MODELVIEW);
}
void ImageDisplay::sl_update(spImageHolder_t _myImageShPtr) //slot
{
QImage * imgPtr = _myImageShPtr->getImagePtr();
if (NULLPTR != imgPtr)
{
deleteTexture(texID);
texID = bindTexture( *imgPtr, GL_TEXTURE_2D, GL_LUMINANCE, QGLContext::NoBindOption );
}
updateGL();
}

Why aren't these arguments valid?

//Block.h
#pragma once
class Block
{
public:
CRect pos;
int num;
public:
Block(void);
~Block(void);
};
//view class
public:
Block currentState[5]; // stores the current state of the blocks
void CpuzzleView::OnDraw(CDC* pDC)
{
CpuzzleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
//draw the 4 blocks and put text into them
for(int i=0;i<4;i++)
{
pDC->Rectangle(currentState[i].pos);
// i'm getting an error for this line:
pDC->TextOut(currentState[i].pos.CenterPoint(), currentState[i].num);
}
pDC->TextOut(currentState[i].pos.CenterPoint(), currentState[i].num);
The error says that no instance of overloaded function CDC::TextOutW() matches the argument list . But the prototype for the function is:
CDC::TextOutW(int x, int y, const CString &str )
all i've done is that instead of the 2 points i've directly given the point object returned by CenterPoint() ... shouldn't it work?
That's because you didn't supplied arguments list correctly. Please read compiler error message carefully, it's usually helps to solve the problem.
TextOut(currentState[i].pos.CenterPoint(), currentState[i].num);
In this call you passed CPoint object and int. This is not correct, you need to pass int, int and CString (or const char* and int length).
To fix this you shall do something like this:
CString strState;
strState.Format("%d", currentState[i].num); // Or use atoi()/wtoi() functions
TextOut(currentState[i].pos.CenterPoint().x, currentState[i].pos.CenterPoint().x, strState);

VC++ does not show output

I am new to VC++ and its been a few times now, and this is the third program that does not give output even after it is build succesfully.
#include <AFXWIN.H>
#include <math.h>
#define PI 3.1415926
#define SEGMENTS 500
class CMyApp : public CWinApp {
public:
virtual BOOL InitInstance();
};
class CMainWindow : public CFrameWnd
{
public:
CMainWindow();
protected:
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT, CPoint);
DECLARE_MESSAGE_MAP();
};
CMyApp myAPP;
BOOL CMyApp::InitInstance() {
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow(SW_MAXIMIZE);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_PAINT ()
END_MESSAGE_MAP ()
CMainWindow::CMainWindow() {
Create(NULL,"The Hello Application",WS_OVERLAPPEDWINDOW);
}
void CMainWindow::OnPaint() {
CRect rect;
int nWidth = rect.Width();
int nHeight = rect.Height();
CPaintDC dc (this);
CPoint aPoint[SEGMENTS];
for (int i =0; i < SEGMENTS; i++){
aPoint[i].x = ((i*nWidth)/SEGMENTS );
aPoint[i].y= (int)((nHeight/2)* (1-(sin((2*PI*i)/SEGMENTS))));
}
dc.Polyline(aPoint, SEGMENTS);
UpdateData(false);
}
The above program should give Sine curve as the output, except that I get a blank window. And I don't know why does it happen. If it helps, I am using VC++ 6.0
The problem is probably that the rectangle you use to get the width and height is not initialized. You have to get the rectangle from somewhere, see e.g. CWnd::GetClientRect.

Why my custom CStatic derived control does not receive WM_SIZE message?

I'm currently developing a custom control that derives from CStatic MFC class (Smart Device C++ project).
I have created the control class using VC++ MFC class wizard, selecting CStatic class as its base class. I have used Class View to add OnSize event handler for my control class (I have selected WM_SIZE message from messages list), and new OnSize method has been created by Visual Studio along with ON_WM_SIZE() statement between BEGIN_MESSAGE_MAP(...) and END_MESSAGE_MAP().
The problem is that my control does not receive WM_SIZE thus OnSize method is never called - I used MoveWindow to change size of my control - its size changes as I have seen on dialog window but WM_SIZE message is never being sent. When I send WM_SIZE through SendMessage or PostMessage function - the control OnSize method is called normally. What do I wrong? I've read MSDN docs about CStatic control and there is no information that WM_SIZE message is never sent to a static control window.
Sorry for my bad English.
For a Windows dialog based project I've tested what you describe. I receive WM_SIZE messages in the custom control after a call of MoveWindow. Can you post a few pieces of your source code, especially for the dialog class where you use your custom Static control for your test?
Update after you posted your code
Did you run this in a debugger? I'm wondering why you don't get immediately an exception when the dialog is opened because CThreatSelection::OnSize is fired as one of the first events, even before the window handle threatGrid.m_hWnd of your control exists at all. So calling threatGrid.MoveWindow in your OnSize dialog event should cause an exception when the dialog opens.
I'm not sure what you are trying to achieve but it looks that you want to resize your custom Static according to the dialog size as soon as the dialog opens:
For this a possible alternative could be: Remove CThreatSelection::OnSize and place in CThreatSelection::OnInitDialog instead:
CThreatSelection::OnInitDialog()
{
CDialog::OnInitDialog();
// ... perhaps other Init-Stuff...
CRect rect;
GetClientRect(&rect);
threatGrid.MoveWindow(0,0, rect.Width(), rect.Height(), FALSE);
return TRUE;
}
Here you can call threatGrid.MoveWindow because the Window Handle threatGrid.m_hWnd is already created in OnInitDialog.
Below is a header of my CDialog based window generated by MFC wizard
#pragma once
#include "threatgrid.h"
#define COLUMN_COUNT 4
// CThreatSelection dialog
class CThreatSelection : public CDialog
{
DECLARE_DYNAMIC(CThreatSelection)
public:
CThreatSelection(CWnd* pParent = NULL); // standard constructor
virtual ~CThreatSelection();
// Dialog Data
enum { IDD = IDD_THSELECT };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
private:
public:
// My custom cotrol field
CThreatGrid threatGrid;
afx_msg void OnSize(UINT nType, int cx, int cy);
};
Here is te body:
// ThreatSelection.cpp : implementation file
//
#include "stdafx.h"
#include "SpeedNotifyNative.h"
#include "ThreatSelection.h"
// CThreatSelection dialog
IMPLEMENT_DYNAMIC(CThreatSelection, CDialog)
CThreatSelection::CThreatSelection(CWnd* pParent /*=NULL*/)
: CDialog(CThreatSelection::IDD, pParent)
, threatGrid(theApp.imaging, 3)
{
}
CThreatSelection::~CThreatSelection()
{
}
void CThreatSelection::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_THGRID, threatGrid);
}
BEGIN_MESSAGE_MAP(CThreatSelection, CDialog)
ON_WM_SIZE()
END_MESSAGE_MAP()
// CThreatSelection message handlers
BOOL CThreatSelection::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CThreatSelection::OnSize(UINT nType, int cx, int cy)
{
threatGrid.MoveWindow(0,0, cx, cy, FALSE);
//threatGrid.SizeChanged(cx,cy); I use it normally because no WM_SIZE is sent to threatGrid
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
}
...And my custom control header:
#pragma once
#include "atltypes.h"
#include "GridIcon.h"
// CThreatGrid
class ImagingSystem;
class CThreatGrid : public CStatic
{
DECLARE_DYNAMIC(CThreatGrid)
public:
CThreatGrid(ImagingSystem* imaging, int cols);
virtual ~CThreatGrid();
protected:
DECLARE_MESSAGE_MAP()
private:
// Obiekt podsystemu obrazowania
ImagingSystem* imaging;
// Ilość kolumn w siatce
int columns;
// Spacing elementów
int spacing;
public:
// Informuje kontrolkę o zmianie rozmiaru - dotychczas nie udało mi się znaleźć rozwiązania dlaczego WM_SIZE nie ejst wysyłane
void SizeChanged(int cx, int cy);
private:
// Aktualny rozmiar - śledzony niezależnie aby uniknąć niepotrzebnych przeładowań obrazków
CSize currSize;
// Lista ikon
std::vector icons;
public:
afx_msg void OnSize(UINT nType, int cx, int cy);
};
...and my custom control body:
// ThreatGrid.cpp : implementation file
//
#include "stdafx.h"
#include "SpeedNotifyNative.h"
#include "ThreatGrid.h"
// CThreatGrid
IMPLEMENT_DYNAMIC(CThreatGrid, CStatic)
CThreatGrid::CThreatGrid(ImagingSystem* imaging, int cols)
: imaging(imaging)
, columns(cols)
{
}
CThreatGrid::~CThreatGrid()
{
}
BEGIN_MESSAGE_MAP(CThreatGrid, CStatic)
ON_WM_SIZE()
END_MESSAGE_MAP()
// Informuje kontrolkę o zmianie rozmiaru - dotychczas nie udało mi się znaleźć rozwiązania dlaczego WM_SIZE nie ejst wysyłane
void CThreatGrid::SizeChanged(int cx, int cy)
{
CSize nSize(cx,cy);
if(nSize != currSize)
{
currSize = nSize;
int wspc = (int)(0.015 * cx);
int hspc = (int)(0.015 * cy);
spacing = (wspc 0 )
{
int rows = (icons.size() + columns - 1) / columns;
int width = (currSize.cx - spacing * (2 + columns - 1)) / columns;
int height = (currSize.cy - spacing * (2 + rows - 1)) / rows;
CSize size;
if ( width Calculate(i / columns, i % columns, abspoint, size, spacing);
}
}
}
}
void CThreatGrid::OnSize(UINT nType, int cx, int cy)
{
CStatic::OnSize(nType, cx, cy);
// NEVER CALLED BY SYSTEM
}

Resources