|
Since I don't know the code, I'm not sure if this will work...
I would pull common code out of these functions and just do them once
during initialization (when the button is created:
CFont *SimpleFont = new CFont;<br />
SimpleFont->CreatePointFont(90, "Arial");<br />
m_button->SetFont(SimpleFont);<br />
m_button->SetFlat(FALSE);<br />
m_button->SetColor(CButtonST::BTNST_COLOR_FG_IN, WHITE);<br />
m_button->DrawTransparent();<br />
Then maybe try forcing a redraw:
void ColorYes(CXPStyleButtonST *m_button)<br />
{<br />
m_button->SetBitmaps(IDB_GREEN_CHECK, WHITE, IDB_GREEN_CHECK, WHITE);<br />
m_button->SetWindowText("Yes");<br />
<br /> m_button->Invalidate();
m_button->UpdateWindow();
}<br />
<br />
void ColorNo(CXPStyleButtonST *m_button)<br />
{<br />
m_button->SetBitmaps(IDB_RED_NO, WHITE, IDB_RED_NO, WHITE);<br />
m_button->SetWindowText("No");<br />
<br /> m_button->Invalidate();
m_button->UpdateWindow();
}<br />
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I'm not familiar with the CXPStyleButtonST class either, but the call to DrawTransparent() to redraw the button immediately raises my suspicions that it may not be erasing the background.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
with reference to the previous post clickety[^]
Cedric helped me in getting the function pointer definition correct as such:
typedef bool (WorkerClass::*pf)(std::string &, std::string &);
for Method signature:
bool fncSame( std::string &x , std::string &y );
I now have two questions.
1) Why does the following fail?
<br />
std::string a("a");<br />
WorkerClass::pf pfn = &WorkerClass::fncSame;<br />
pfn(&a,&a);<br />
giving me the error: "error C2064: term does not evaluate to a function taking 2 arguments"
2) Where would I use function pointers? (other than academically as I'm currently doing)
Many Thanks
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
"There is no wealth like knowledge, no poverty like ignorance." Ali ibn Abi Talib
|
|
|
|
|
is fncSame a static member function ? (it should be. if you're going to use it with fn pointers)
Mustafa Ismail Mustafa wrote: Where would I use function pointers?
you use them all the time when interacting with C-style interfaces (ex. qsort, EnumChildWindows, EnumFonts, WndProcs, etc). you find them often in C-interfaced libraries, where library authors add hooks to let you override library functionality without having to change any of the library's source code - just set the "errorOutput" function pointer (or whatever) to your own function instead of the built-in function.
C++ reduces their importance, though. since a lot of what they were used for in C is handled by the ability to override functions in base classes, you don't need to use function pointers - just derive from the base class and override a virtual function.
|
|
|
|
|
Chris Losinger wrote: is fncSame a static member function ?
Its not. I thought that both static and instance members could be used, but not interchangeably.
Anyways, I changed the method to static and now it gives me this error:
error C2440: 'initializing' : cannot convert from 'bool (__cdecl *)(std::string &,std::string &)' to 'WorkerClass::pf'
In addition to the previous error:
error C2064: term does not evaluate to a function taking 2 arguments
I'm using VS2005 if that means anything in this case.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
"There is no wealth like knowledge, no poverty like ignorance." Ali ibn Abi Talib
|
|
|
|
|
Chris Losinger wrote: is fncSame a static member function ? (it should be. if you're going to use it with fn pointers)
The pointer type is as follows (I added the underline):
typedef bool (WorkerClass::*pf)(std::string &, std::string &)
The function must be a non-static member function of WorkerClass .
Steve
|
|
|
|
|
Mustafa Ismail Mustafa wrote: I now have two questions.
1) Why does the following fail?
std::string a("a");
WorkerClass::pf pfn = &WorkerClass::fncSame;
pfn(&a,&a);
Because it doesn’t make sense: what instance of the class would the member function be called on? You need something like this:
WorkerClass wc;
std::string a("a");
WorkerClass::pf pfn = &WorkerClass::fncSame;
(wc.*pfn)(&a,&a a, a);
-- modified at 3:39 Wednesday 3rd October, 2007
Steve
|
|
|
|
|
Another you have to take care of (Stephen already answered the main question):
bool fncSame( std::string &x , std::string &y );
This is your function signature: it requires two string parameters that are passed by reference. When you use it, you do that:
pfn(&a,&a);
Which is wrong, because you pass the addresses of the strings (pointers). You should do this instead (and take in account the answer of Stephen of course ):
pfn(a,a);
|
|
|
|
|
Oops. Ta for that correction.
Steve
|
|
|
|
|
So I'm trying to sort each value from a files. A user inputs a string of 8 numerals. then breaks the string down to an array and writes each element from the array to a file on each line.
So if the user inputs: 87654321
Its output in the file will be:
8
7
6
5
4
3
2
1
What I want to do is get the first integer of the file, compare it to the second and write to a new file the numbers in ascending order:
1
2
3
4
5
6
7
8
Now, I am aware of the sort() func, and would like to avoid it as much as possible. I'm trying to use the if/for/while to accomplish this task. Below is my code of what I have so far:
<code>
// fstsreamCompare.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int v;
int loop;
int aA[8];
string ns;
int _tmain(int argc, _TCHAR* argv[])
{
ofstream ouI;
ouI.open("rwB.dat");
if( ouI.fail()){
cout << "Error reading file: " << endl;
exit(1);}
ifstream inI;
inI.open("rwB.dat");
if( inI.fail()){
cout << "Error writing file: " << endl;
exit(1);}
do {
cout << "File(SORT) Input an 8-Digit numeral: "; cin >> ns;
if ( ns.length() < 8 )
{
cout << "Human Error! " << "The value is too short!" << endl;
cout << "The value: " << ns << " is " << ns.length() << " digits long." << endl;
cout << "Press any key to re-enter a valid numeral." << endl;
_getch();
system("CLS");}
else if ( ns.length() == 8)
{
aA[0] = ns[0] - '0';
aA[1] = ns[1] - '0';
aA[2] = ns[2] - '0';
aA[3] = ns[3] - '0';
aA[4] = ns[4] - '0';
aA[5] = ns[5] - '0';
aA[6] = ns[6] - '0';
aA[7] = ns[7] - '0';
for ( v=0; v<ns.length(); v++ )
ouI << aA[v] << endl;
}
} while ( ns.length() < 8 ); system("CLS");
cout << endl << "Values have been written to FILE(SORT)" << endl;
cout << "Press any key to sort the values." << endl << endl;
_getch();
return 0;
}
</code>
And thanks to David Crow I know how to convert strings to arrays .
I know how to get the first integer from the file but don't know how to get the second to begin comparing.
Keep in mind this is not a school project this is for my leisure.
|
|
|
|
|
I'd do something like this -- but I'd use the std::sort function!
-----------------------------------------------------------------
// Console.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <stdexcept>
using namespace std;
inline int CharToNum(char ch)
{
// Check for a bad digit.
if (ch<'0' || ch>'9')
{
throw range_error("Non-digit encountered!");
}
return ch-'0';
}
void main()
{
// Get a string of numbers from the user.
string s;
cout << "Enter a string of chars: ";
cin >> s;
// Create an array of numbers from the string -- each digit is one entry.
vector<int> nums;
try
{
transform(s.begin(), s.end(), back_inserter(nums), CharToNum);
}
catch (const range_error &e)
{
cout << e.what() << endl;
return;
}
// Sort the numbers.
sort(nums.begin(), nums.end());
// Output the results.
copy(nums.begin(), nums.end(), ostream_iterator<int>(cout, "\n"));
}
Steve
|
|
|
|
|
hi
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int v;
int loop;
int aA[8];
string ns;
int _tmain(int argc, _TCHAR* argv[])
{
ofstream ouI;
ouI.open("rwB.dat");
if( ouI.fail()){
cout << "Error reading file: " << endl;
exit(1);}
do {
cout << "File(SORT) Input an 8-Digit numeral: "; cin >> ns;
if ( ns.length() < 8 )
{
cout << "Human Error! " << "The value is too short!" << endl;
cout << "The value: " << ns << " is " << ns.length() << " digits long." << endl;
cout << "Press any key to re-enter a valid numeral." << endl;
_getch();
system("CLS");}
else if ( ns.length() == 8)
{
aA[0] = ns[0] - '0';
aA[1] = ns[1] - '0';
aA[2] = ns[2] - '0';
aA[3] = ns[3] - '0';
aA[4] = ns[4] - '0';
aA[5] = ns[5] - '0';
aA[6] = ns[6] - '0';
aA[7] = ns[7] - '0';
for ( v=0; v<ns.length(); v++="" )
="" oui="" <<="" aa[v]="" endl;=""
="" }
="" }="" while="" (="" ns.length()="" <="" 8="" );="" system("cls");
="" oui.close();="" cout="" endl="" "values="" have="" been="" written="" to="" file(sort)"="" endl;
="" "press="" any="" key="" sort="" the="" values."="" ifstream="" ini;
="" ini.open("rwb.dat");
="" if(="" ini.fail()){
="" "error="" writing="" file:="" "="" exit(1);}
="" char="" buff;
="" int="" aarray[8];
="" for(="" iloop="0" ;="" iloop<8;iloop++)
="" {
="" ini.getline(&buff,2,'\n');
="" aarray[iloop]="buff-'0';
" ini.close();
="" <b=""> print unsorted data but from the file
for( int iLoop=0 ; iLoop<8;iLoop++)
{
cout<
|
|
|
|
|
Herboren wrote: And thanks to David Crow I know how to convert strings to arrays .
Although it was not C++.
It looks as though all you need to do is sort the numbers in ascending order before the for (v=0; ...) loop writes them to the file. That is, unless you actually want the original numbers in a file beforehand.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thats a good point, I'm going to give that a go. gimme a sec...
|
|
|
|
|
hello,
i am using c++ for 2 years but, still dont know what is exacly difference between macro, pragma(directives) and inline?
could someone explain me?
thanks
|
|
|
|
|
|
The va_copy() (function/macro) doesn't seem to be in my Visual Studio 2005; or at least I'm getting an "identifier not found" error. Perhaps I need to point my include directory differently???
Failing that, does anybody know a reliable definition for it? I've searched MSDN and didn't find anything.
David
|
|
|
|
|
I don't know what it does, but maybe you need to implement your own
using va_arg, va_end, va_start[^]
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
If all else fails, you could roll your own, something like:
#define va_copy(dest, src) ((void)((dest) = (src)))
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks for the thought. Unfortunately, it's not that easy. These functions are compiler supplied for a good reason; they're inherently low-level and depend strongly on how the compiler pushes and pops the stack, alignment issues, and perhaps other things. I have seen one implementation where va_copy(dest, src) was a simple assignment of src to dest. But, on the other hand, if you've looked at Microsoft's implementation of va_start() etc. you know that a simple assignment may be WAY to naive. It MIGHT work, but then again, it might not. Or worse yet, it might work SOMETIMES.
If someone out there KNOWS how it should be implemented in Visual Studio, I'll certainly try it, but I hate to just guess.
David
|
|
|
|
|
Having a the processID how to get the application instance (HINSTANCE) ?
- or how in general people get the HINSTANCE for an applicaytion ?
-----
here is what I'm trying to do and have some issues:
If I'm in my current app and call AfxGetInstanceHandle(), then the result pass it to a function that parses this conforming to the PE headers structures, and changes some PE imports it is ok.
If I want to do the same for arbitrary process after I do OpenProcess() for a processID and pass that handle to the same function that will change some PE imports it fails because it doesn't see it as
a correct PE header.
Probably a more correct question will be: how to change the imports for current running notepads proceses ? I want to make them call a custom function instead the "GetSaveFileName()" API.
I'm stuck here.
please help.
|
|
|
|
|
HANDLE WINAPI OpenProcess(
DWORD dwDesiredAccess,
BOOL bInheritHandle,
DWORD dwProcessId
);
Maxwell Chen
|
|
|
|
|
As I've mentioned here in earlier posts, I've been writing a set of custom controls. They're finished for the most part, but there is one control left to do that has me stumped. I need a custom ComboBox.
The top of the combobox is an image. That is to say, the part of the combobox that displays the current selection as well as the down arrow that you click on to make a selection is a PNG image. What I need to figure out is how to provide the rest of the combobox functionality, specifically, the drop-down part.
The drop-down part doesn't have to be skinable. So I was thinking about using a standard list box. When the user clicks on the combobox's down arrow, a list box appears beneath it. Once the user selects an item, the list box is hidden. If the list box loses focus at any time, it is also hidden.
I've yet to try this, but it seems to be an ok solution. However, I'm looking for better ideas, so I thought I would ask here. Thanks.
[EDIT]
I think I found a better solution. I can subclass the CComboBox class making sure to leave out the WS_VISIBLE style. That way the combo box is invisible. I'll provide the top part image as described above. When the user clicks the down arrow on my image, I'll call ShowDropDown to show only the drop-down list. This seems to give me what I want.
[/EDIT]
-- modified at 13:17 Tuesday 2nd October, 2007
|
|
|
|
|
Leslie Sanford wrote: I can subclass the CComboBox class making sure to leave out the WS_VISIBLE style.
Do you still get paint and mouse messages when the window is not visible?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark Salsbery wrote: Do you still get paint and mouse messages when the window is not visible?
Nope. I just found this out.
What I'm currently testing is creating a custom control derived from CWnd. It has as it's member a CComboBox. The combobox is initialized to have the same dimensions as the custom control. Also, it's created without the WS_VISIBLE style. The custom control does the hit detection with the left mouse button to determine when to show the drop-down list.
This is working so far, but I've yet to wire up the combobox to the custom control so that it propogates selection change events. This may or may not be hard. I'm still refamiliarizing myself with MFC controls.
[EDIT]
Yay! The above approach works. I've got it all wired together. Frankly, it feels like a bit of a kludge, but it's the best approach I can think of other than rewriting the CComboBox from scratch.
[/EDIT]
[EDIT2]
Well, this has turned out to be too kludgey for my tastes. I've found a simpler way. I have a pop-up menu appear below the bitmap representing the combobox header. This works nice and smoothly. And I don't feel like I'm trying to shoe-horn a control into doing what I want it to do.
[/EDIT2]
-- modified at 16:37 Tuesday 2nd October, 2007
|
|
|
|