|
I see many code projects that enhance the CListBox control, but none that lets each item be another control, like fx CButton. How would one implement that?
|
|
|
|
|
|
The link draws radio buttons, but doesn't really use MFC buttons. I am working on a solution that drops the need for CListBox.
|
|
|
|
|
Create some button controls as listbox's children window, put its on the foreground of the listbox, you have to process some messages to change the control's data, coordinates, etc.
|
|
|
|
|
Hello everybody
I have an SDI solution in MFC. I have created a class with the name of "CLASS A"
and
class CXXView : public CView, public CLASS A
How can I get access to CXXView variables from CLASS A in MFC?
Indeed; DOSE NOT WORK ==> CXXView * pCurrentView = static_cast<cxxview*>(GetActiveView());
Because of this error:
GetActiveView() is undifined (XXView.h is included in CLASS A)
Best Regads
|
|
|
|
|
Member 15033704 wrote: ndeed; DOSE NOT WORK ==> CXXView * pCurrentView = static_cast<cxxview*>(GetActiveView());
Because of this error:
GetActiveView() is undifined
Of course it does not work! GetActiveView is a method of CFrameWnd class, but your "CLASS A" has nothing to do with the CFrameWnd!
BTW, why do you think you need to "get access to CXXView variables from CLASS A"?
|
|
|
|
|
Many thanks for your help.
In fact, I do some calculations in CLASS A, then I want to show the results of those calculations in
CXXView class by using pDC->Textout(........).
I use InvalidateRect(NULL,NULL,FALSE) to redraw the view, but as you know it is not a good idea.
I have defined a CRect in CXXView class that I know those texts are going to be shown in that rect and I want to Invalidate just that rect from CLASS A.
On the other hand, while I am in CLASS A and using InvalidateRect function, this function needs 2 parameters of CXXView class to be done that are HWND and CRect is defined in class CXXView.
Best Regads
|
|
|
|
|
Why are you trying to do it from the "CLASS A" instance?
Just implement it in the CXXView class. Or, if your "CLASS A" already has a method that does what you need then just call it from within the instance of CXXView class (of course, this method must be declared as public or protected, not as private).
|
|
|
|
|
Dear Victor Nijegorodov
Before of all, many thanks for your advice.
The structure of solution is as follows:
Calculations are to be done in CLASS A;
and class CXXView : public CView, public CALSS A
CXXView dose not know when these Calculations are done;
When CLASS A send a message to CXXView, then CXXView execute other methods;
The message is InvalidateRect;
The problem is that CXXView dose not know when these Calculations are done.
On the ther hand, I don't want to define a flag and check it continuously if Calculations are done or not?
so CXXView has to await receiving InvalidateRect message from CLASS A
I tried to define an instance of CXXView class within the CLASS A:
CXXView* pCurrentView;
CRect rect = pCurrentView->m_Rect_zone;
HWND pWnd = pCurrentView->GetSafeHwnd();
InvalidateRect(NULL, NULL, FALSE);
but I was not succeeded because of this compiler error:
Uninitialized variable pCurrentView
One macro or something like that is missing here (CXXView* pCurrentView;) which I am not familiar with that.
Best Regards
|
|
|
|
|
codeNation fresher batch has a tradition of laser tournaments .in laser tag two team play against each other .a team can have any number of players >=1.the two teams can have unequal team member.
for successful completion of this tournament ipshita want that every person must have played every other person as part of different teams.every laser tag match take 30 minutes to complete .now the gaming arena has allowed codeNation to play for a maxima of X hours after which the arena close . given the number of folks in the batch as N you will have to find out if codeNation will be able to finish the tournament before the complex closes?
|
|
|
|
|
So what have you tried, and where are you stuck? If you are expecting someone to do your work for you I am afraid disappointment awaits you.
|
|
|
|
|
For those who enjoy playing with algoritms ... A two-step problem: The simple part, make something that works. The difficult part: Provomg that your solution is the optimal one.
Disclaimer: I have no clue about the second part.
Problem: We have a keypad lock at our work facilities. You present your card, and then if the last four digits typed are a valid entry code, the door opens. So if a valid code is 2345, and you type 1234, the door doesn't open. If you then add a 5, the door opens.
An algoritm for trying all possible keys is like the first homework assignment in 101 Elementary Programming.
If you have no knowledge of any entry code, but you know that if the last four digits are correct, the door will open, what will be your dialing strategy for making the minimal nunber of keys dialled to get in? What will be the worst case number of digits dialled? Can you prove that this it the theoretically best, that no other algorithm will provide a lower worst case?
What will be the average across all possible entry codes? Can you prove that your algorithm provides the minimal number of total keypresses in the average case?
I do not have any ready-made "right answer" to this problem, just a bunch of implementations of how to make sure I get it if I have forgotten the code with no complexity analysis. I do know the code, so I do get in - don't worry about that! The problem here is not to get in, but the complexity of getting in if you do NOT know the code!
|
|
|
|
|
here is the method to insert a node at the front in the XORed doubly linked list or also known as memory efficient doubly linked list :
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *npx; };
Node *XOR(Node *a, Node *b){
return ((Node *)( (uintptr_t)(a) ^ (uintptr_t)(b) ));
}
void insert(Node **head_ref, int data){
Node *new_node = new Node();
new_node->data = data;
new_node->npx = XOR((*head_ref), nullptr);
if(*head_ref != nullptr){
Node *next = XOR((*head_ref)->npx, nullptr);
(*head_ref)->npx = XOR(new_node, next);
}
*head_ref = new_node;
}
void printList(Node *head){
Node *curr = head, *prev = nullptr, *next;
cout << "Following are the nodes of Linked List: \n";
while(curr != nullptr) {
cout << curr->data << " ";
next = XOR(prev, curr->npx);
prev = curr;
curr = next;
}
}
int main(){
Node *head = nullptr;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
insert(&head, 40);
insert(&head, 50);
insert(&head, 60);
printList(head);
return 0;
}
Here is the code i have tried to put an element at the back of the list:
Node *insert(Node **last, int data){
Node *new_node = new Node();
new_node->data = data;
new_node->npx = XOR(*last, nullptr);
if(*last != nullptr) {
Node *prev = XOR((*last)->npx, nullptr);
(*last)->npx = XOR(prev, new_node);
}
return (new_node);
}
int main(){
Node *head = nullptr;
head = insert(&head, 10);
head = insert(&head, 20);
head = insert(&head, 30);
head = insert(&head, 40);
head = insert(&head, 50);
head = insert(&head, 60);
printList(head);
return 0;
}
But it is not working
output: it is same in both the cases
Thank you
|
|
|
|
|
Tarun Jha wrote: But it is not working Debug it and find out what is happening.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
I am trying to call two identical functions (one is templated and the other one is normal). The templated one compiles successfully, but the non-templated one gives compilation error. Why?
template <typename ParamType>
void Template_universal_ref(ParamType&& param)
{
cout << "Template_universal_ref, param : " << param << endl;
}
void NonTemplate_universal_ref(int&& param)
{
cout << "NonTemplate_universal_ref, param : " << param << endl;
}
int main()
{
int x=5;
Template_universal_ref(x);
return 0
} In my view
NonTemplate_universal_ref(x); is special version of
Template_universal_ref(x); . Why does NonTemplate_universal_ref(x); result in compilation error?
|
|
|
|
|
The double ampersand in the non-template version is illegal. Change it to a single one for a reference parameter, or none at all for a direct value.
|
|
|
|
|
int&& is a rvalue reference. Until C++11 we only had lvalue references. In Nontemplate_universal_ref(x) , x is a lvalue, not an rvalue. Try the following:
template <typename ParamType>
void Template_universal_ref(ParamType&& param)
{
cout << "Template_universal_ref, param : " << param << endl;
}
void NonTemplate_universal_ref(int&& param)
{
cout << "NonTemplate_universal_ref(int&&), param : " << param << endl;
}
void NonTemplate_universal_ref(int& param)
{
cout << "NonTemplate_universal_ref(int&), param : " << param << endl;
}
int main()
{
int x=5;
Template_universal_ref(x);
NonTemplate_universal_ref(x);
NonTemplate_universal_ref(x+3);
return 0;
} Note that in the second call to NonTemplate_universal_ref, the argument x+3 is not an lvalue, that is, it cannot be assigned to.
Now, maybe someone can explain why the call via the template works?
|
|
|
|
|
I am working with embedded programming in standard C and there are 2 microcontrollers communicating with each other using a home-made SPI-protocol. In addition, microcontroller 2 has a CAN-bus that should also (indirectly) be fully accessible from microcontroller 1. The scheduler and task queues are also home-made so no realtime operating system is used. My tasks look like this:
void (*task_t)(uint8_t* taskData, uint16_t sizeOfTaskDataInBytes); My functions to add 1 or 2 tasks look like this:
typedef enum {
TASK_IN_WAIT_PHASE = 0,
TASK_FINISHED = 1
} taskStatus_e;
typedef taskStatus_e (*getTaskStatusCallback_t)(uint8_t* data, uint16_t dataSizeBytes);
Bool_t queueTask(taskQueueSelector_e, task_t, uint8_t* data, uint16_t dataSizeBytes, getTaskStatusCallback_t);
Bool_t queue2Tasks(taskQueueSelector_e, task_t task1, uint8_t* data1, uint16_t data1SizeBytes, getTaskStatusCallback_t getTaskStatusCallback1, task_t task2, uint8_t* data2, uint16_t data2SizeBytes, getTaskStatusCallback_t getTaskStatusCallback2); When microcontroller 1 wants to read from microcontroller 2 over SPI, the following code is used:
struct SPI_secretData_s {
volatile Bool_t accessInProgress;
volatile int8_t numRetriesLeft;
};
#define PLEASE_SEE_NUM_REGS_TO_ACCESS_PARAMETER (1)
struct SPI_readRegsTaskParams_s {
struct SPI_secretData_s secretData;
volatile uint32_t startAddr;
volatile uint16_t numRegsToAccess;
volatile SPI_readStatus_e readStatus;
volatile uint16_t readBuffer[PLEASE_SEE_NUM_REGS_TO_ACCESS_PARAMETER];
};
queue2Tasks(SPI_QUEUE_SELECTOR, SPI_readRegs, (uint8_t*)&SPI_readRegsTaskParams, sizeof(SPI_readRegsTaskParams), SPI_getTaskStatusCallback, SPI_handleReadIsFinished, NULL, 0, NULL);
taskStatus_e SPI_getTaskStatusCallback(uint8_t* data, uint16_t dataSizeBytes) {
struct SPI_readRegsTaskParams_s* SPI_readRegsTaskParams = (struct SPI_readRegsTaskParams_s*)data;
return (SPI_readRegsTaskParams->secretData.accessInProgress) ? TASK_IN_WAIT_PHASE : TASK_FINISHED;
}
uint32_t SPI_handleReadIsFinished(uint8_t* notUsed, uint16_t sizeOfNotUsedInBytes) {
struct SPI_readRegsTaskParams_s* SPI_readRegsTaskParams = (struct SPI_readRegsTaskParams_s*)getPreviousTaskData();
} When microcontroller 2 wants to read from the CAN-bus, the following code is used:
struct CAN_secretData_s {
volatile Bool_t accessInProgress;
};
struct CAN_readRegsTaskParams_s {
struct CAN_secretData_s secretData;
volatile uint32_t startAddr;
volatile uint8_t numRegsToReadMax4;
volatile uint16_t readRegsBuffer[4];
volatile CAN_readStatus_e readStatus;
};
queue2Tasks(CAN_QUEUE_SELECTOR, CAN_readRegs, (uint8_t*)&CAN_readRegsTaskParams, sizeof(CAN_readRegsTaskParams), CAN_getTaskStatusCallback, CAN_handleReadIsFinished, NULL, 0, NULL);
taskStatus_e CAN_getTaskStatusCallback(uint8_t* data, uint16_t dataSizeBytes) {
struct CAN_readRegsTaskParams_s* CAN_readRegsTaskParams = (struct CAN_readRegsTaskParams_s*)data;
return (CAN_readRegsTaskParams->secretData.accessInProgress) ? TASK_IN_WAIT_PHASE : TASK_FINISHED;
uint32_t CAN_handleReadIsFinished(uint8_t* notUsed, uint16_t sizeOfNotUsedInBytes) {
struct CAN_readRegsTaskParams_s* CAN_readRegsTaskParams = (struct CAN_readRegsTaskParams_s*)getPreviousTaskData();
}
Could someone please suggest how I should handle the case when microcontroller 1 wants to read from the CAN-bus, via microcontroller 2?
modified 8-Feb-19 5:20am.
|
|
|
|
|
Just like I have a secretData_s struct as part of the task data, maybe I should also have a postTaskInfo_s section:
#define PLEASE_SEE_SIZE_OF_POST_TASK_DATA_PARAMETER (1)
struct postTaskInfo_s {
task_t postTask;
taskQueueSelector_e postTaskQueueSelector;
uint16_t sizeOfPostTaskData;
uint8_t postTaskData[PLEASE_SEE_SIZE_OF_POST_TASK_DATA_PARAMETER]
}; Then if postTaskInfo->postTask is not null then the CAN_readRegs-task will queue the postTask just before it's finished. Can someone think of something better?
modified 8-Feb-19 5:58am.
|
|
|
|
|
I am working with embedded programming in standard C and there are 2 microcontrollers communication with each other using a home-made protocol. The scheduler and task queues are also home-made so no realtime operating system is used. My tasks look like this:
void (*task_t)(uint8_t* taskData, uint16_t sizeOfTaskDataInBytes); My add-task-to-queue-function looks like this:
Bool_t addTaskToQueue(queueSelector_e, task_t, uint8_t* taskData, uint16_t sizeOfTaskDataInBytes); Please note that the taskData is also inserted into the queue (in an area called task data pool area) so it's not just a pointer to taskData that is being queued. The task data to write data from microcontroller 1 to microcontroller 2 needs to look something like this:
struct writeRegTaskData_s {
uint32_t startAddr;
uint16_t numRegsToWrite;
uint8_t dataToWrite[numRegsToWrite]; or
uint8_t* dataToWrite } The problem is with the dataToWrite parameter. Sometimes a lot of data will be written, for example, during a firmware upgrade, and then it's overkill to put all the dataToWrite into the queue, it's perfectly fine to have one static buffer. At other times, for example when reacting to different push button events, I want to put my dataToWrite on the stack and then add it into my task queue. However, when I do this, I must make sure dataToWrite points to the proper location inside my task queue data pool and not on the original buffer on the stack. What's the recommended way to solve this issue in a clean way?
|
|
|
|
|
arnold_w wrote: I must make sure dataToWrite points to the proper location inside my task queue data pool and not on the original buffer on the stack. Then you need to allocate a block of memory from the pool, and copy the data there. I assume that you have a pool control mechanism that operates similar to malloc to manage the pool. In either case you pass the buffer pointer to the function that handles the data. You just need to make sure that pool buffers are released after they have been used.
|
|
|
|
|
Yes, the task data pools (one for each task queue) are simple FIFO:s, but they're kept separate from the queues where I store the task function pointers. The only thing I can think of is to place the copied data immediately after the task data struct:
typedef enum {
USE_STATIC_NON_QUEUED_WRITE_DATA = 0,
DATA_TO_WRITE_APPEARS_AFTER_STRUCT = 1
} writeBufferLocationOption_e;
struct writeRegTaskData_s {
uint32_t startAddr;
uint16_t numRegsToWrite;
writeBufferLocationOption_e writeBufferLocationOption;
uint8_t* dataToWrite }
Bool_t addTaskToQueue(queueSelector_e, task_t, uint8_t* taskData1, uint16_t sizeOfTaskDataInBytes1, uint8_t* taskData2, uint16_t sizeOfTaskDataInBytes2); When I do want to queue the data, then I'd pass the data as the taskData2 parameter. Can someone think of a cleaner solution?
|
|
|
|
|
What about something like:
struct writeRegTaskData_s {
writeBufferLocationOption_e writeBufferLocationOption;
uint16_t numRegsToWrite;
uint8_t dataToWrite[1]; }
You can then create your fixed buffer with the above structure, followed immediately by the data space. For the dynamic buffers you just need to calculate the total space required (struct plus data), allocate it and fill in the struct values, and copy the data. You then just have a single pointer to pass to your data maniulation routines.
|
|
|
|
|
I don't think I can guarantee that the static non-queued buffers can be put in the end of the structs because most of the time the structs will be created on the stack. Therefore, I think I need pointers to the buffers.
I also have a CAN-bus interface and typically the receive packet task would take a struct that looks like this:
struct packet_s {
uint32_t extID;
uint8_t DLC; uint8_t* data;
}; In my application these packets will always be queued (they never use static buffers) and as soon as they are queued the data pointer becomes worthless. The best thing I can think of is the following (a slight variation of Richard MacCutchan's suggestion):
#define PLEASE_SEE_DLC_FOR_PACKET_SIZE (1)
struct packet_s {
uint32_t extID;
uint8_t DLC; uint8_t data[PLEASE_SEE_DLC_FOR_PACKET_SIZE];
};
modified 4-Feb-19 9:27am.
|
|
|
|
|
When you declare your static buffers you just add the space required for the struct at the beginning. Then you only need one pointer whether it is static or dynamic. Keep things simple as much as possible.
|
|
|
|
|