|
David is correct in saying that developing a device driver is not a trivial task. If you have to ask such a question then you are -- at this stage -- almost certainly not the man for the job.
Steve
|
|
|
|
|
Hi
I have downloaded Ultimate Toolbox( http://www.codeproject.com/KB/MFC/UltimateToolbox.aspx[^] source code. And i see that for drawing controls, some of controls source code overrode OnDrawItem and put drawing code in there, some of them in OnPaint message handler, some of them both.
How does a control developer make decision when only Onpaint or only Drawitem or both of them must be used? Is this relative to control(for example drawing a custom buton is different combobox or etc.) or relative to drawing style?
Could you please give me some trick?
Thanks...
|
|
|
|
|
For questions related a particular article you should use the comments section at the bottom of the article.
-Saurabh
|
|
|
|
|
This is a general MFC question (could be completly unrelated to Dundas toolbox), so I think this is a valid place to ask.
This signature was proudly tested on animals.
|
|
|
|
|
Some controls do not have the concept of an "item." For example, an edit control does not have a DrawItem message (IIRC), while a ComboBox does.
You may have to handle both because of what you are allowed to touch in each message handler, and because of what "phase" in the drawing process you are at. For example, you may not be able to successfully custom-draw the drop-down button on a combobox during the DrawItem phase -- maybe it gets overwritten/redrawn after that phase completes.
Peace!
-=- James Please rate this message - let me know if I helped or not!<hr></hr> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
James R. Twine wrote: Some controls do not have the concept of an "item." For example, an edit control does not have a DrawItem message (IIRC), while a ComboBox does.
I confused here. Yes some control have concept of an item. Like combobox, Listbox...We can add items(strings, color) to that controls so they have DrawItem virtual method.
But why does CButton class have virtual DrawItem method? What can we add such a control so it has a concept of an "item"?
Thanks for answer.
|
|
|
|
|
Yes, some of the controls do not make sense, Button and Static controls have an item draw message, even though it might not make much sense. Also, lots of virtual functions exist on the CWnd base class that may not apply to all of its derives classes, like CWnd::OnDrawItem .
Peace!
-=- James Please rate this message - let me know if I helped or not!<hr></hr> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
OK.
If i turn back my question in my firstpost: How does a control developer make decision when only Onpaint or only Drawitem or both of them must be used? Is this relative to control(for example drawing a custom buton is different combobox or etc.) or relative to drawing style?
Can we say that:
If a control has owner draw property we must paint items of control in DrawItem method (like this : http://msdn.microsoft.com/en-us/visualc/bb838680.aspx[^]
but if we want draw combobox itself, we must handle that in OnPaint Handler. We mustn't draw items in onpaint if it support ownerdraw ??
I asked this because i am not sure about my logic. Because for example in that application(custom button) http://www.codeproject.com/KB/buttons/roundbuttons.aspx[^]
he made all the drawing in the DrawItem method.
Thanks for help...
|
|
|
|
|
sawerr wrote: some of controls source code overrode OnDrawItem and put drawing code in there
Well OnDrawItem is mainly provdided to facilitate owner drawing controls without sub classing them. An e.g. would be a tab control, we can owner draw a tab control without sub classing it using this function.
Signature of OnDrawItem is as follows, nIDCtl points to the ID of the controls that's meant to owner drawn. So here you will get the id of your tab control and you can owner draw it here.
afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
There is another option, that's by subclassing and overriding DrawItem virtual function. Note that DrawItem get's called by OnDrawItem .
Stack trace to DrawItem virtual member function is as follows...
CListBoxEx::DrawItem(tagDRAWITEMSTRUCT * 0x0012f6d4) line 20
CListBox::OnChildNotify(unsigned int 43, unsigned int 1000, long 1242836, long * 0x00000000) line 125
CWnd::SendChildNotifyLastMsg(long * 0x00000000) line 2683
CWnd::ReflectLastMsg(HWND__ * 0x0004089a, long * 0x00000000) line 2721
CWnd::OnDrawItem(int 1000, tagDRAWITEMSTRUCT * 0x0012f6d4) line 1156 + 14 bytes
CLearnDrawingDlg::OnDrawItem(int 1000, tagDRAWITEMSTRUCT * 0x0012f6d4) line 46
CWnd::OnWndMsg(unsigned int 43, unsigned int 1000, long 1242836, long * 0x0012f4f0) line 1930
sawerr wrote: How does a control developer make decision when only Onpaint or only Drawitem or both of them must be used?
DrawItem messages makes things easier for developers, we get OD flags that tells which action requires painting.
For e.g.
ODS_CHECKED
ODS_FOCUS
ODS_DISABLED.
We just have to do the painting stuff. But if we are going via OnPaint then we are on our own, we've got to find out when an item is selected, when it's in focus, when it's disabled based on every paint event that we get. Maybe even scrolling too. So that's for the tough guys.
So basically for quick and easy painting things we got via OnDrawItem , or DrawItem else for full control we go via OnPaint .
This is my understanding on this topic!
sawerr wrote: downloaded Ultimate Toolbox
I did have the privilege of working on this project with Nishant! Didn't do much development though. My first impression was that it's well written and it works well. It was a great learning experience.
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
OK. So Can we say:
"Use OwnerDraw Method when we can, and OnPaint when we have to"?
I think DrawItem MEthod is not only for "items". We can paint/make entire control. Right?
For example we can make elliptic buttons in ownerdraw():
CRect rect = lpDrawItemStruct->rcItem;
dc.Ellipse(rect);
Also we can change the combobox's items drawing style. But can we change style of combobox itself, not only items with only ownerdraw method?Or, If we want this, must we use onpaint handler? I tried this code for listbox:
void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CRect rect = lpDrawItemStruct->rcItem;
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
dc.Ellipse(&rect);
}
it draws a circle in the listbox, not draw elliptic listbox. But same code makes elliptic button.
IS that a right scenario to see that when we need onpaint and can't be done such a thing with DrawItem Method? We can draw entire button with ownerdraw method, but we can't draw Listbox or combobox with ownerdraw, only it is items?
Thanks for all answers...
|
|
|
|
|
sawerr wrote: I think DrawItem MEthod is not only for "items". We can paint/make entire control. Right?
You are limited in DrawItem function. The point is that it's just a helper function. The master of these functions is OnPaint or WM_PAINT message.
sawerr wrote: For example we can make elliptic buttons in ownerdraw():
CRect rect = lpDrawItemStruct->rcItem;
dc.Ellipse(rect);
You are not free to draw anything anytime in DrawItem functions. There is a rule. You draw based on the flag's that's passed in. So if the flag tells you draw entire client area (ODA_DRAWENTIRE)then go around splashing colors, else if it's just to paint a focus rect (ODA_FOCUS) then you can't do that.
Now you may ask how to know if have to draw a focus rect or remove a focus rect, that's why we have the state flag ODS_FOCUS.
Do the same in OnPaint in and you will what I meant.
sawerr wrote: IS that a right scenario to see that when we need onpaint and can't be done such a thing with DrawItem Method? We can draw entire button with ownerdraw method, but we can't draw Listbox or combobox with ownerdraw, only it is items?
Can you rephrase this question? My brain just fell apart!
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
OK.
DRAWITEMSTRUCT has got a hDC member which identifies a device context. Is that Device Context for drawing shapes or etc. things to the surface of the control or redraw control itself. For example:
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect;
GetClientRect(&rect);
rect.DeflateRect(20,50);
pDC->Ellipse(rect);
}
That code draws an ellipse to the surface of the button, not elliptic button. Because when i clicked out of ellipse buttonclicked handler invoked.
I tried same thing for OnPaint() -->> CPaintDC dc(this);
It gave me same result.
1-) Is there a way to get DC for drawing control itself, not its surface?
2-)If yes, how can i understand which DC is for surface of control and which DC is for draw a control?
Thanks for all helps...
|
|
|
|
|
sawerr wrote: 1-) Is there a way to get DC for drawing control itself, not its surface?
So you want to change the shape of the control itself! For that you've got to call SetWindowRgn . You can use functions in CRgn class to create a region your window.
To create an elliptical dialog you just have to call SetWindowRgn with a region parameter from OnInitDialog .
Read this for more information.[^]
sawerr wrote: 2-)If yes, how can i understand which DC is for surface of control and which DC is for draw a control?
There is nothing like surface and a control dc. It's just one control dc which is just used for painting on top of a control.
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
Hi
FONT this are Decimal NCRs ....How to get ASCII Character from this values....
|
|
|
|
|
int i = 0x32;
char c = i;
|
|
|
|
|
VB experiences, I suppose.
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
|
|
|
|
|
Since you mention FONT, I wonder If you are talking about getting ASCII values from printed/scanned material, you will need something that can do OCR on the scanned image that can recognize the characters.
Peace!
-=- James Please rate this message - let me know if I helped or not!<hr></hr> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Hello,
I have a property sheet with pages in it in my WinCE application devleoped in eVC++4.
The sheet has page as members & are added to sheet. Sheet is a modal & is called directly on menu click. It doesn't have any parent. I have couple of factors to look at :
1) On the device I don't see the Ok, Cancel, Apply buttons. To close it, I have to press Esc.
2) I have OnCommand() in each page, with SetModified(true), which updates the variables of the page. I want to update the changes in a object present in sheet class. How do I achieve that?
Any help is highly appreciated.
Thanks
Terry
|
|
|
|
|
Could someone please explain to me how to use getaddrinfo in its most simple form. The example given by MSDN is beyond my understand as well as every site i've found so far.
All i want is to get my internal ip address and store it in a variable.
Thank you.
|
|
|
|
|
smedowns77 wrote: All i want is to get my internal ip address and store it in a variable.
Do you have the name of your machine? If so, you can use
ADDRINFO hints = {0},
*sa;
hints.ai_family = AF_INET;
hints.ai_flags = AI_CANONNAME;
int nResult = getaddrinfo("machine_name", NULL, &hints, &sa);
char *addr = inet_ntoa(((LPSOCKADDR_IN) sa->ai_addr)->sin_addr); An easier way would be to use gethostbyname() .
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
For getting your ip address, call GetAdaptersInfo() and the IP_ADAPTER_INFO.CurrentIpAddress will give you the current ip address of that adapter.
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
A given host can have more than one address.
Here's a simple IP v4 example:
char hostname[256];
if (0 == ::gethostname(hostname, sizeof(hostname)))
{
addrinfo aihints;
addrinfo *ailist = NULL;
memset(&aihints, 0, sizeof(aihints));
aihints.ai_family = AF_INET;
aihints.ai_socktype = SOCK_STREAM;
aihints.ai_protocol = IPPROTO_TCP;
if (::getaddrinfo(hostname, NULL, &aihints, &ailist) == 0)
{
addrinfo *paddrinfo = ailist;
while (paddrinfo)
{
sockaddr_in *paddr_in = (sockaddr_in *)paddrinfo->ai_addr;
char *pipstring = inet_ntoa(paddr_in->sin_addr);
paddrinfo = paddrinfo->ai_next;
}
::freeaddrinfo(ailist);
}
}
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Try to understand this. I have a main application and a dll. main application has its mfc view window, say window 1 and there's a button on main window(window 1) which calls a function inside a dll to open a new window, say window 2. Both view windows handles WM_KEYDOWN message and pop up a message box.
Now, when only window 1 is opened, all keyboard messages are properly received by it.
When I open a window 2 and then press key, no keyboard message is received by either of the windows.
Even if I minimize window 1 by clicking on minimize button at top right, no keyboard message is received by either of the windows.
Imagine this -> both windows are open side by side and if I directly click on window 2, then it will bring window 2 to forefront and now window 2 will receive all keyboard messages.
I know window 2 is child of window 1 but somehow not able to route messages to window 2 properly. Does anyone have any idea why this problem in setting focus to window 2 is occuring. when I minimize window 1, why cant window 2 receive keyboard messages???
the fruits of your success will be in direct ratio to the honesty and sincerity of your own efforts in keeping your own records, doing your own thinking and, reaching your own conclusions.
..surviving in autumn..in love with spring..
|
|
|
|
|
Hello People,
i have a code written in C++ for DDE ... my task is to read data from a EXCEL sheet at a regular interval ... i am able to do it with the following code ... but the problem is when i say A16,B67,Z63 etc.. my application gives me request fail and type some of my code to excel sheet i don't know how ... but yes when i say A1, B9, V2 etc i.e alphabet with a single digit it gives me correct answer .. but also when i hard-code A16 as "R16C1" it gives me correct answer ... the hard-coded string is the form in which excel understands .... so i have a function to convert C16,A1 to R16C3, R1C1 ...
please help me with it ... and also how to read data from different sheets from a single EXCEL ...
hoping for a positive and immediate reply ... !!
Thank you in advance
Code Build: VC++ 6.0
File Name: DDE_ExcelApplication.
Code:-
// cppDdeApplication.cpp : Defines the entry point for the console application.
//
# include "stdafx.h"
# include "windows.h"
# include "ddeml.h"
# include "stdio.h"
# include "string.h"
# include "conio.h"
HDDEDATA CALLBACK DdeCallback
(
UINT uType, // Transaction type.
UINT uFmt, // Clipboard data format.
HCONV hconv, // Handle to the conversation.
HSZ hsz1, // Handle to a string.
HSZ hsz2, // Handle to a string.
HDDEDATA hdata, // Handle to a global memory object.
DWORD dwData1, // Transaction-specific data.
DWORD dwData2) // Transaction-specific data.
{/* This function is called in DdeInitialize() */
return 0;
}
void DDEExecute(DWORD idInst, HCONV hConv, char* szCommand)
{
//HDDEDATA hData = DdeCreateDataHandle(DWORD(idInst), (LPBYTE)szCommand,DWORD(szCommand)+1, 0, NULL, CF_TEXT, 0);
HDDEDATA hData = DdeCreateDataHandle(DWORD(idInst), (LPBYTE)szCommand,1, 0, NULL, CF_TEXT, 0);
if (hData==NULL)
{
printf("Command failed: %s\n", szCommand);
}
else
{
DdeClientTransaction((LPBYTE)hData, 0xFFFFFFFF, hConv, 0L, 0,XTYP_EXECUTE, TIMEOUT_ASYNC, NULL);
}
}
void DDERequest(DWORD idInst, HCONV hConv, char* szItem, char* sDesc)
{
HSZ hszItem = DdeCreateStringHandle(idInst,LPCTSTR(szItem), 0);
HDDEDATA hData = DdeClientTransaction(NULL,0,hConv,hszItem,CF_TEXT,XTYP_REQUEST,5000 , NULL);
if (hData==NULL)
{
printf("Request failed: %s\n", szItem);
}
else
{
char szResult[255];
DdeGetData(hData, (unsigned char *)szResult, 255, 0);
sDesc = "";
printf("%s%s\n", sDesc, szResult);
}
}
char* cellString(char szItem[])
{
char firstCh = szItem[0];
char strRow[2] = "";
char strCol[3] = "";
int i,j = 0;
int strLength = (int)(strlen(szItem));
for(i=1;i<strLength;)
{
strRow[j] = szItem[i];
i++;
j++;
}
strRow[j] = '\0';
switch(toupper(firstCh))
{
case 'A':strcpy(strCol,"C1");break;
case 'B':strcpy(strCol,"C2");break;
case 'C':strcpy(strCol,"C3");break;
case 'D':strcpy(strCol,"C4");break;
case 'E':strcpy(strCol,"C5");break;
case 'F':strcpy(strCol,"C6");break;
case 'G':strcpy(strCol,"C7");break;
case 'H':strcpy(strCol,"C8");break;
case 'I':strcpy(strCol,"C9");break;
case 'J':strcpy(strCol,"C10");break;
case 'K':strcpy(strCol,"C11");break;
case 'L':strcpy(strCol,"C12");break;
case 'M':strcpy(strCol,"C13");break;
case 'N':strcpy(strCol,"C14");break;
case 'O':strcpy(strCol,"C15");break;
case 'P':strcpy(strCol,"C16");break;
case 'Q':strcpy(strCol,"C17");break;
case 'R':strcpy(strCol,"C18");break;
case 'S':strcpy(strCol,"C19");break;
case 'T':strcpy(strCol,"C20");break;
case 'U':strcpy(strCol,"C21");break;
case 'V':strcpy(strCol,"C22");break;
case 'W':strcpy(strCol,"C23");break;
case 'X':strcpy(strCol,"C24");break;
case 'Y':strcpy(strCol,"C25");break;
case 'Z':strcpy(strCol,"C26");break;
}
szItem = strcpy(szItem,"R");
szItem = strcat(szItem,strRow);
szItem = strcat(szItem,strCol);
return szItem;
}
int DDECall(char szApp[],char szTopic[],char szCmd[],char szInItem[])
{
char szDesc[] = "Requested Cell Value Contains: ";
char* szItem = cellString(szInItem);
//strcpy(szItem,"R13C4");
FILE *fp;
DWORD idInst=0;
UINT iReturn;
iReturn = DdeInitialize((LPDWORD)&idInst, (PFNCALLBACK)DdeCallback,APPCMD_CLIENTONLY, 0L );
/*DdeInitialize() :-
The DdeInitialize function registers an application with the Dynamic Data Exchange Management Library (DDEML).
An application must call this function before calling any other Dynamic Data Exchange Management Library (DDEML)
function.*/
if (iReturn!=DMLERR_NO_ERROR)
{
/*If Error occurs while initialiing DDE then the error code is written to the Log.*/
printf("DDE Initialization Failed: 0x%04x\n", iReturn);
fp = fopen("DdeErrorLog.txt","w+");
fprintf(fp,"\nError Code : DMLERR_DLL_USAGE | DMLERR_INVALIDPARAMETER | DMLERR_SYS_ERROR");
fprintf(fp,"\n\n This Error occurs if Dynamic Data Exchange fails to Initialize ");
fclose(fp);
Sleep(1500);
return 0;
}
HSZ hszApp;
hszApp = DdeCreateStringHandle(DWORD(idInst),LPCTSTR("EXCEL"),CP_WINANSI);
/* DdeCreateStringHandle() :-
The DdeCreateStringHandle function creates a handle that identifies the specified string.
a Dynamic Data Exchange (DDE) client or server application can pass the string handle as
a parameter to other Dynamic Data Exchange Management Library (DDEML) functions.*/
//Start DDE Server and wait for it to become idle.
HINSTANCE hRet = ShellExecute(NULL , "open", "C:\\Test.xls", NULL,NULL, SW_NORMAL);
if ((int)hRet < 33)
{
printf("Unable to Start DDE Server: 0x%04x\n", hRet);
Sleep(1500);
DdeUninitialize(idInst);
return 0;
}
Sleep(1000);
//DDE Connect to Server using given AppName and topic.
HSZ hszTopic;
HCONV hConv;
hszApp = DdeCreateStringHandleA(idInst,LPCTSTR("EXCEL"),CP_WINANSI);
hszTopic = DdeCreateStringHandleA(idInst,LPCTSTR(szTopic), 0);
hConv = DdeConnect(idInst, hszApp, hszTopic, NULL);
DdeFreeStringHandle(idInst, hszApp);
DdeFreeStringHandle(idInst, hszTopic);
if (hConv == NULL)
{
printf("DDE Connection Failed.\n");
Sleep(1500);
DdeUninitialize(idInst);
/*The DdeUninitialize function frees all Dynamic Data Exchange Management Library
(DDEML) resources associated with the calling application.*/
return 0;
}
DDEExecute(DWORD(idInst), hConv, szCmd);
/*Sends a command to another application, using dynamic data exchange (DDE).*/
DDERequest(idInst, hConv, szItem, szDesc);
//DDE Disconnect and Uninitialize.
DdeDisconnect(hConv);
DdeUninitialize(idInst);
Sleep(3000000);
return 1;
}
int main()
{
char szApp[] = "EXCEL";
char szTopic[] = "C:\\Test.xls";
char szCmd[] = "[APP.MINIMIZE()]";
char szItem[] = "A1";
DDECall(szApp,szTopic,szCmd,szItem);
getch();
return 0;
}
Thanks & Regards
Vishrant Shah
|
|
|
|
|
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
|
|
|
|
|