|
This is my post accidently put in the lounge...
>>>>
This should be pretty easy, right? I'm feeling pretty brain dead right now, though...
In CTreeCtrl the nodes have a DWORD data item and I want to make use of that as a 24 bit value and an 8 bit value. What would be a nice way of getting and setting the DWORD from two other variables?
Thanks,
Ben.
<<<<
Anyway I'm thinking something like (in pseudocode...)
getting...
8bitVal = DWordVal mod 2**24
32bitVal = DWordVal / 2**24
setting...
DWordVal = 24bitVal * 2**24 + 8bitVal
I haven't tried this so is probably wrong and I have a feeling that that there is a nicer way of doing it?
|
|
|
|
|
How about
DWORD dwData = <value>;
DWORD dwEightBit = (dwData & 0x000000FF);
DWORD dwTwentyFourBit = (dwData & 0xFFFFFF00) >> 8;
and
DWORD dwEightBit = <value1>;
DWORD dwTwentyFourBit = <value2>;
DWORD dwData = (dwTwentyFourBit << 8) | dwEightBit;
This has just been typed in, not compiled or tested, you understand. The second code snippet assumes value1 <= 255 and value2 <= 16777215.
|
|
|
|
|
Thanks very much for this. I will have a good look at this later. I'm out of time now. My programming shift is 8am to 8.30am. Have to get myself to the checkouts at the supermarket for my day job now!
|
|
|
|
|
This has been very helpful to me. Thanks very much. Somehow the world of bitwise operations was eluding me that morning!
|
|
|
|
|
Oh god, I know I'm confused. Loads of mistakes. I know. Rather than edit my previous post here are my own ammendments...
If the 24 bit value are the high end bits and the 8 bit value are the low end bits...
getting...
8bitVal = 32bitVal mod 2**8
24bitVal = 32bitVal / 2**8
setting...
32bitVal = 24bitVal * 2**8 + 8bitVal
Still think there may be an easier way of doing this...
|
|
|
|
|
Ben Aldhouse wrote: Still think there may be an easier way of doing this.
Yes, don't go in for complex mathematical expressions, even though the compiler will optimise them. Reread the previous answer and adjust for whichever part is the 8-bit and whichever is the 24-bit. In either case just use simple shifts AND and OR operators thus:
DWORD dwValue = (bits8 << 24) | bits24;
DWORD dwValue = (bits24 << 8) | bits8;
And to split
bits8 = dwValue & 0xFF;
bits24 = dwValue >> 8;
bits24 = dwValue & 0xFFFFFF;
bits8 = dwValue >> 24;
It's time for a new signature.
|
|
|
|
|
Oops - I accidentally gave you a 1 vote instead of a 5. I can't see how to undo a vote!
Sorry.
(OK, looks like I fixed it. I just voted again, giving 5 - that seems to have incremented the vote count but replaced my previous vote. Good.)
|
|
|
|
|
Yep, looks OK now; many thanks for taking the trouble to vote.
It's time for a new signature.
|
|
|
|
|
Thanks for this! Your reply is very much appreciated!
|
|
|
|
|
You're welcome, good luck with your programs.
It's time for a new signature.
|
|
|
|
|
Another option (provided you don't want to transmit your packed 32 bit values to another machine) is to use a union with a set of bit-fields:
union tree_ctrl_item
{
DWORD packed_;
struct
{
DWORD bits_24_ : 24;
DWORD bits_8_ : 8;
}
unpacked_;
};
Then you can use the packed and unpacked forms as ordinary structure members and add member functions for construction and extraction of values if you feel so inclined.
There are numerous "here be dragons" caveats with this but provided you're consistent in the usage it'll be portable if not binary compatible.
Cheers,
Ash
|
|
|
|
|
hi guys, i have problem on my GDI drawing (car) when i try to add a button control or any other control, i lose the focus of my drawing.
here is the scenario,
when i press SPACE my car moves forward frm left to right.
but when i tried adding a button for colorPicker purpose, mar car won't move when i try to press SPACE on my ketborad.. tried to remove the button it worked fine.
how do i set the focus to my GDI car drawing after selecting a color of the car?
or should i say what code to use?
i tried to use
CWnd* pWnd = myCar.rec;
pWnd->SetFocus();
obviously it's error
\lg.manuel\daily quests\gdicarsample\gdicarsample\gdicarsampledlg.cpp(115) : error C2440: 'initializing' : cannot convert from 'CRect' to 'CWnd *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
since car is Crect i can't pass it to CWnd to set the focus,
thanks ahead guys,
|
|
|
|
|
How did you handle SPACE key press?
The focus may be on button and abviously it will take the SPACe key input. In OnButtonClick handler, To kill focus from button, call SetFocus() on main parent window. You may use like this->SetFocus().
|
|
|
|
|
here is my code for SPACE bar hanlder
CGDICarSampleDlg.cpp
void CGDICarSampleDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == 8) {
myCar.Backward();
Invalidate();
}
else if (nChar == 32) {
myCar.Forward();
Invalidate();
}
else if (nChar == 80){
CColorDialog dlg;
if (dlg.DoModal() == IDOK)
{
color = dlg.GetColor();
}
red = GetRValue(color);
green = GetGValue(color);
blue = GetBValue(color);
char rr[32];
itoa(red, rr, 10);
r = rr;
char gg[32];
itoa(green, gg, 10);
g = gg;
char bb[32];
itoa(blue, bb, 10);
b = bb;
myCar.m_brCar.Detach();
myCar.m_brCar.CreateSolidBrush(RGB(red, green, blue));
WritePrivateProfileString("car", "red", r, "car.ini");
WritePrivateProfileString("car", "green", g, "car.ini");
WritePrivateProfileString("car", "blue", b, "car.ini");
myCar.PickColor();
Invalidate();
}
else {
TRACE( "others" );
}
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
and here is the command triggered
Car.cpp
void CCar::Forward()
{
m_nCarX += 5;
angleFlag = angleFlag + 1;
}
void CCar::Backward()
{
m_nCarX -= 3;
angleFlag = angleFlag - 1;
}
if i SetFocus on the Main Dlg Window, will it kill the focus of the button?
|
|
|
|
|
One option is to allow the user to trigger the colour pick button by mouse click only. On Key down events, process the movements of your car. Override PreTranslateMessage() of your window to handle all key down events.
void CGDICarSampleDlg::OnBnClickedButtonColour()
{
}
BOOL CGDICarSampleDlg::PreTranslateMessage(MSG* pMsg)
{
if(WM_KEYDOWN == pMsg->message)
{
KeyDownHandler(pMsg->wParam);
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
void CGDICarSampleDlg::KeyDownHandler(UINT nChar)
{
if (nChar == 8)
{
}
else if (nChar == 32)
{
}
}
|
|
|
|
|
i just tried this code:
BOOL CGDICarSampleDlg::PreTranslateMessage(MSG* pMsg)
{
if(WM_KEYDOWN == pMsg->message)
{
OnKeyDown(pMsg->wParam);
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
got an error saying
error C2660: 'CGDICarSampleDlg::OnKeyDown' : function does not take 1 arguments
maybe because OnKeyDown needs three not just one, on my code below notice that the OnKeyDown event has 3 return values,.
CGDICarSampleDlg.cpp
void CGDICarSampleDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == 8) {
myCar.Backward();
Invalidate();
}
else if (nChar == 32) {
myCar.Forward();
Invalidate();
}
else {
TRACE( "others" );
}
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
declaration of OnKeyDown ...
CGDICarSampleDlg.h
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
|
|
|
|
|
hey that is not the WM_KEYDOWN message handler. You don't need it anymore.
BOOL CGDICarSampleDlg::PreTranslateMessage(MSG* pMsg)
{
if(WM_KEYDOWN == pMsg->message)
{
KeyDownHandler(pMsg->wParam);
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
void CGDICarSampleDlg::KeyDownHandler(UINT nChar)
{
if (nChar == 8)
{
}
else if (nChar == 32)
{
}
}
|
|
|
|
|
yup i did that already but it seems that i still get the same error..
KeyDownHandler does not take 1 argument...
you might want to check the sample project, here's the link and take a look... i did not include the button yet.. run and see..
http://hotfile.com/dl/68100441/675ae33/GDICarSample.rar.html[^]
meybe am just too dumb to get your point, haha, sory..
take a peek at my code to understand what i want do do.. thanks for help man, appreciate it.
modified on Monday, September 13, 2010 11:56 PM
|
|
|
|
|
|
thanks man, i got alot of hints to get my car going on track. it's working fine man. your code works well..
|
|
|
|
|
Hi everyone. I have declared an fstream object that I would want it to either support unicode characters or not. I am using a preprocessor to do this as shown in the code below. Please I am not an advanced programmer in C++, that's why I am consulting experts here to know if it is right.
#if defined (_UNICODE) || defined (UNICODE)
#define fstream basic_fstream<wchar_t,char_traits<wchar_t>>
#else
#define fstream basic_fstream<char, char_traits<char>>
#endif
I have limited knowledge in this C++ template. If it is not right, how should I write it. Thanks in advance.
|
|
|
|
|
I think you should use typedef instead:
#if defined (_UNICODE) || defined (UNICODE)
#typedef basic_fstream<wchar_t,char_traits<wchar_t>> fstream
#else
#typedef basic_fstream<char, char_traits<char>> fstream
#endif
|
|
|
|
|
Good idea, however you should check the syntax of the posted code.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
You don't want to use fstrean as something you typedef or define. ESPECIALLY define...
Use a typedef and don't use a name that's already in use by the standard library - unless you understand namespace overloading rules you'll have a few surprises, even if you do you might still end up with surprises. Another way you could do it for MS compilers is to rely on that old chestnut TCHAR being defined correctly for the build you're doing and then just use a single typedef:
typedef std::basic_fstream<TCHAR> file_stream;
The final point to make is... On windows, why bother? Unless you've got some rubbish old Windows on DOS computers kicking around you can just use wchar_t and wfstream throughout your code.
Cheers,
Ash
|
|
|
|
|
What I want to do is that I will compile the same application as either unicode or non-unicode without modifying the source code. I also do not want to assume the use of Windows. Maybe I have to use the typedef rather if that is better or the best way.
|
|
|
|