|
you should allocate first the size of your button array, either statically (Button *btnShow[10]; ) or dynamically ( Button **ppBtnShow; ppBtnShow = (Button **)calloc(10, sizeof(Button *)); [...] ppBtnShow[i] = new Button; ). In the last case don't forget to free your data to avoid memory leaks!
Fold With Us!
Sie wollen mein Herz am rechten Fleck
Doch seh ich dann nach unten weg
Da schlägt es links
|
|
|
|
|
I think it is better to have an array that contains directly the buttons and not the pointers to the buttons (otherwise you will need to destroy your buttons after use, ...).
So it looks like:
CButton* m_pButtonArray;
And to create for example 10 buttons:
m_pButtonArray = new CButton[10];
Then you will need to 'initialize' them using the Create method and then call ShowWindow(SW_SHOW) for all of them.
Hope this helps
|
|
|
|
|
Hi Picander,
You forgot to attatch your code, and you didn't mention which compiler your are using VC6.0 or DOT net. Well I gave a try to do the same stuff you have tried, with MCF in VC6.0. You might take a look to it.
//---------------------------------------------------
char *strCaption[3]= {"Button A","Button B","Button C"};
char buffer[20];
int butt_width = 90,
butt_height = 30,
butt_pad = 10;
CButton *myButton = new CButton[3];
// Create a push button.
for(int i=1 ; i <= 3 ; i++)
{
myButton[i-1].Create(strCaption[i-1], WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(10, 10 + butt_height * (i-1), 100, butt_height*i), this, 1);
//add code to populate variables
}
//--------------------------------------------------------
Good Day
Sohail
|
|
|
|
|
Thanks for the reply Sohail!
I forgot to mention that I am developing a .NET windows forms application.
Button *myButton = new Button[3];
does not work...
gui\myForm.cpp(87) : error C2691: 'System::Windows::Forms::Button' : invalid type for __gc array element
gui\myForm.cpp(87) : error C3149: 'System::Windows::Forms::Button' : illegal use of managed type 'System::Windows::Forms::Button'; did you forget a '*'?
gui\myForm.cpp(87) : error C2691: 'System::Windows::Forms::Button __gc *' : invalid type for __gc array element
i have tried many other options, as other posted suggested and i still cannot get it working.
I cannot find on the net any similar code sample for c++.
|
|
|
|
|
you need to reserve a range of continuous resource ID for your array of buttons; and manually create a ON_CONTROL_RANGE entry in the message map.
The problem with MFC is that you need to have a predefined ID range; you can't create the buttons with no defined ID and have it registered in the message map ( or I think so ).
ON_CONTROL_RANGEBN_CLICKED, id1, id2, OnButtonsClicked)
void YourMethod::OnButtonsClicked(UINT nCmdID)
{
switch ( nCmdID )
{
case id1:
...
case id2:
break;
default:
break;
}
}
One other way would be to create your own class of button that have callbacks that can be sent to the parent window.
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
Thanks for all the replies! You are great folks!
Here is the combination of your suggestions:
Button* myButtons[];
myButtons= new Button *[20];
for (int i = 0; i < 20; i++)
myButtons[i] = new Button();
|
|
|
|
|
Thanks for all the replies! You are great folks!
For a .net managed c++ windows form application, here is the combination of your suggestions:
Button* myButtons[];
myButtons= new Button *[20];
for (int i = 0; i < 20; i++)
myButtons[i] = new Button();
|
|
|
|
|
hi all there, i want a code that help me to insert any file type in a rtf file, i need to insert by code not drag drop by mouse so plzzzzzzzzzzzzz help me .......
Thanks alot
Metal Man
|
|
|
|
|
Hi. Can anyone help me on this: I need to read data from sound card on line in. All I want is to get the digitized data that represents the signal I apply to line in. If anyone could suggest me some docs, or anything related to this I would be thankful.
PLEASE HELP !!! Thank you.;)
|
|
|
|
|
|
hello,
I have a problem. the call SETWINDOWTEXT truncates the length of the displayed text to 259 characters (dialog based, ansi mode) (somebody else reported 159), i don't see any dots, also a lot of space is left on the bar. A getwindowtext will return the full original string.
I try to set a titlebar of my dialog with 3 strings which are left, center and right justified. I do this by inserting a lot of spaces between the strings
Things i can do, but don't wan't to:
- Change the active title bar font.
- Draw my own titlebar.
The following code will work on a 800x600 mode maximized dialog, but not on a 1600x1200.
<code>
void SetWindowText3(CWnd* TargetWnd, CString LeftText, CString MidText, CString RightText)
{
CRect Rect;
int CaptionWidth;
CFont font;
CDC dc;
NONCLIENTMETRICS ncm;
int LeftWidth;
int MidWidth;
int RightWidth;
int SpaceWidth;
int FillWidth;
int FillLeftWidth;
int FillRightWidth;
int PosCenter;
CString CaptionText;
/****************************************************************************
Figure out the maximum width of the caption text
****************************************************************************/
TargetWnd->GetWindowRect(Rect);
CaptionWidth = Rect.Width(); // get width of whole window
CaptionWidth -= 2*GetSystemMetrics(SM_CXBORDER); // Remove left and right the border width
CaptionWidth -= 2*GetSystemMetrics(SM_CXFRAME); // Remove left and right the frame width
if (TargetWnd->GetStyle() & WS_SYSMENU)
CaptionWidth -= 2*GetSystemMetrics(SM_CXSIZE); // Remove the icon and close button
else
CaptionWidth -= 5; // Remove extra edge if no close button
PosCenter = CaptionWidth/2;
if ( (TargetWnd->GetStyle() & WS_MAXIMIZEBOX)
|| (TargetWnd->GetStyle() & WS_MINIMIZEBOX) )
CaptionWidth -= (2*GetSystemMetrics(SM_CXSIZE)) - 1; // Remove the width of the minimizebutton
/****************************************************************************
Prepare the GetTextExtent function.
****************************************************************************/
ncm.cbSize = sizeof(NONCLIENTMETRICS); // i don't why this is used.
SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
sizeof(NONCLIENTMETRICS), &ncm, 0); // get the pointer to the nonclientmetrics struct
font.CreateFontIndirect(&ncm.lfCaptionFont); // Create a copy of the font used title bar
dc.CreateCompatibleDC(NULL); // Create an DC
CFont* pOldFont = (CFont*)dc.SelectObject(&font); // Make sure that gettextextent uses the correct font.
dc.SetMapMode(MM_TEXT); // Make sure that gettextextent returns PIXELS.
/****************************************************************************
Get various width in pixels of the thext
****************************************************************************/
LeftWidth = dc.GetTextExtent(LeftText).cx; // Get the text width of the left text.
MidWidth = dc.GetTextExtent(MidText).cx; // Get the text width of the mid text.
RightWidth = dc.GetTextExtent(RightText).cx; // Get the text width of the right text.
SpaceWidth = dc.GetTextExtent(_T(" ")).cx; // How many pixels does a space take
FillWidth = (CaptionWidth - LeftWidth -
MidWidth - RightWidth)/SpaceWidth; // How much space is left on the title bar?
/****************************************************************************
Bug Work around. Make sure text is never longer as 259 characters
****************************************************************************/
// if ((LeftText.GetLength()+MidText.GetLength()+RightText.GetLength()+FillWidth)>259)
// FillWidth = 259 - (LeftText.GetLength()+MidText.GetLength()+RightText.GetLength());
/****************************************************************************
Fill in the spaces between the text and write the text to the title bar
****************************************************************************/
FillLeftWidth = (PosCenter-LeftWidth-(MidWidth/2))/SpaceWidth; // Space (in characters) between left and middle
//FillLeftWidth = FillWidth/2; // Same as above, but midtext in middle of empty area
FillRightWidth = FillWidth - FillLeftWidth; // space (in characters) between middle and right
CaptionText = LeftText;
for (int loopy=0;loopy<FillLeftWidth;loopy++) // How many space are needed between left and middle?
CaptionText += _T(" "); // Add a space to the caption text
CaptionText += MidText; // Add the middle text to the caption text
for (int loopy=0;loopy<FillRightWidth;loopy++) // How many space are needed between middle and right?
CaptionText += _T(" "); // Add a space to the caption text
CaptionText += RightText; // Add the right text tot the caption text
TargetWnd->SetWindowText(CaptionText); // now actually set the windows text
/****************************************************************************
Delete temperary objects
****************************************************************************/
dc.SelectObject(pOldFont);
font.DeleteObject(); // Delete the font.
dc.DeleteDC(); // Delete the DC
}
</code>
|
|
|
|
|
|
Hello! I've seen some sample programs on how to use CRectTracker properly, but i only have seen it on ordinary CViews...
I noticed the problem when i used CScrollViews and when I'm scrolling..
When I scroll the view, the CRectTracker also scrolls, thats fine.. But when I scroll all the way to the right, the CRectTracker is supposed to disappear, since its coordinates is out of the view already, and thats fine... But When I scroll back to its position, i expected to see it back, but i didn't.... I want the CRectTracker to be appearing again..
How do i solve this?
|
|
|
|
|
Hello,
i'm programming a multithreaded communication application. Some of the communication operations are being hold in a a shared (for all threads) synchronized queue. the synchronized queue is using c++ 'CRITICAL_SECTION' handle (using the 'EnterCriticalSection' and 'LeaveCriticalSection' functions).
have three questions (actually it's two):
1) Is 'CRITICAL_SECTION' ensure FIFO threads access into the critial sections of code?
2) If not, what can i use to ensure FIFO access?
3) If yes, is there a better (less CPU time, less resources) way to ensure FIFO access to critical sections of code?
thanks
|
|
|
|
|
1. Yes. This first thread to request critical section using EnterCriticalSection acquires it. The others must wait until the critical section is released with LeaveCriticalSection. I have seen some articles, posts, and other submissions here at the CodeProject site arguing that a class wrapper is a good idea in case you experience an exception - the critical section will not remain locked by a thread experiencing the exception. You can also look into TryEnterCriticalSection if you do not want your threads to block for some reason.
2. You can also use Mutex if your data is accessed by multiple threads from multiple processes. IF the data is only acessed by a single process, the critical section is generally the easiest and the quickest, without getting into interlocked element usage.
3. Not really. The critical section has been the recommended element for single-process multiple-thread protection of sections of code.
|
|
|
|
|
Hello,
Are you sure that CRITICAL_SECTION handles the threads waiting in FIFO order?
MSDN does not reffer to it any where
could you point me to an article or something that talks about it?
thanks
|
|
|
|
|
I misunderstood your initial question, and thought you were trying to implement a FIFO stack or similar queue.
This is what MSDN had to say about the critical section:
Platform SDK : DLLs, Processes, and Threads : Critical Section Objects ; Last updated: April 2004
"A critical section object provides synchronization similar to that provided by a mutex object, except that a critical section can be used only by the threads of a single process. Event, mutex, and semaphore objects can also be used in a single-process application, but critical section objects provide a slightly faster, more efficient mechanism for mutual-exclusion synchronization (a processor-specific test and set instruction). Like a mutex object, a critical section object can be owned by only one thread at a time, which makes it useful for protecting a shared resource from simultaneous access. There is no guarantee about the order in which threads will obtain ownership of the critical section, however, the system will be fair to all threads. Unlike a mutex object, there is no way to tell whether a critical section has been abandoned."
|
|
|
|
|
Notice,
MS does not guarantee FIFO order when using CRITICAL_SECTION:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/initializecriticalsection.asp
I've tried to implement a FIFO critical section. But i think it consumes alot of CPU time. If anyone can look into it, it would be helpfull
thanks
H FILE:
****************************
#include <windows.h>
#include <stdlib.h>
#include <list>class uFIFOCriticalSection
{
protected:
CRITICAL_SECTION _EntryCritSection;
std::list<handle> mLst_events;
public:
uFIFOCriticalSection();
void Enter();
void Leave();
virtual ~uFIFOCriticalSection();
private:
// No copies do not implement
uFIFOCriticalSection(const uFIFOCriticalSection &rhs);
uFIFOCriticalSection &operator=(const uFIFOCriticalSection &rhs);
};
****************************
CPP FILE:
**********************
#include "uFIFOCriticalSection.h"
uFIFOCriticalSection::uFIFOCriticalSection()
{
InitializeCriticalSection (&_EntryCritSection);
}
void uFIFOCriticalSection::Enter() {
EnterCriticalSection(&_EntryCritSection);
//creating an event handle for this thread (when it goes out of the critical section
HANDLE my_event = CreateEvent(NULL, false, false, NULL);
HANDLE last_event = NULL;
//checking if there is a thread inside the critical section
if (!mLst_events.empty()) last_event = mLst_events.back();
//pushing this thread's event handle into the queue
mLst_events.push_back(my_event);
LeaveCriticalSection(&_EntryCritSection);
//if there is a thread inside the, (last_event != NULL -> 'mLst_events' IS empty)
//we'll wait for the single event to be signalled.
//NOTE: if the 'last_event' is already signalled, the function will not wait
//and if the last_event is closed (by CloseHandle) the handle is invalid
//and the function will return with 'WAIT_FAILED'. Both cases are good for us.
if (last_event != NULL) WaitForSingleObject(last_event, INFINITE);
}
void uFIFOCriticalSection::Leave() {
EnterCriticalSection(&_EntryCritSection);
//the thread had left the critical section.
//getting the related event handle
HANDLE my_event = mLst_events.front();
mLst_events.pop_front();//removing myself
LeaveCriticalSection(&_EntryCritSection);
//signalling this thread's event.
//if there is a thread wait on this event,
//it's will automatically started
SetEvent(my_event);
//no need for it anymore, we can close the handle to retain resources
CloseHandle(my_event);
}
uFIFOCriticalSection::~uFIFOCriticalSection()
{
//we must release all the threads
//DO WE? let's try not...
/* std::list<handle>::iterator itr = mLst_events.begin();
while(itr != mLst_events.end()) {
SetEvent((*itr));
itr++;
}*/
DeleteCriticalSection (&_EntryCritSection);
}
*****************
|
|
|
|
|
Hi all:
Can anybody gives me a fully explanations or references of FDT(File Directory Table).
Does it has something to do with the order appearing after the DOS command "dir"?
Thanks!
|
|
|
|
|
With FAT16, and possibly FAT32, the order in which files/folders are enumerated is governed by how they were added to the FAT initially. IIRC, files/folders on NTFS are enumerated alphabetically.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Thanks! Then how can I retrieve the FTD for specific folder?
|
|
|
|
|
I'm not familar with FTD in the context of folders. Could you be more specific?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Hi,
I have to create two propertysheets at runtime in dialog box.
when i run the project i get the last property sheet with dialog box and other controls
hidden.
if i run the project commenting one propertypage and viceversa i am getting the property
page and all the controls.
after creating each property sheet i used to call the modify style function.
can you give me any suggestion.
With Regards,
R.Sundaram.
|
|
|
|
|
how to get software package for vc++ project exe
|
|
|
|
|
Are YOu looking for Some Type of Installer.
Grab it from here INNO Setup
[^].
It's FREE
"I Think this Will Help"
<h5
alok gupta="" <br=""> visit me at http://www.thisisalok.tk
|
|
|
|