|
|
Hi!
I've to add Two Text Boxes and one Button to a Windows Forms in VC++. How to do this?
|
|
|
|
|
To Add a button (button1) to a from (Frm1)
this.button1 = new System.Windows.Forms.Button();
this.Frm1.Controls.Add(this.button1 );
"The Ultimate Limit Is Only Your Imagination."
|
|
|
|
|
I am trying to select the text of client area which i had paint by using the textout().how i had display the text see ..............
<pre lang="msil">void GetFormattedData( PVOID pBuf,DWORD dwOffset, CString &szFormatted )
{
BYTE *SETBUFF = ((BYTE*)(pBuf)) + dwOffset ;
szFormatted.Format(_T("%08d: %.2X %.2X %),dwOffset,SETBUFF[0], SETBUFF[1],;
}</pre>
<pre lang="cs">for(int i = Start; i<= End; i++)
{
GetFormattedData(m_lpData, (i + m_nVScrollBarPos )* m_cyChar,szFormatted);
m_pDC.TextOut((1 - m_nHScrollBarPos) * m_cxChar , i * m_cyChar, szFormatted);
}</pre>
|
|
|
|
|
void GetFormattedData( PVOID pBuf, DWORD dwOffset, CString &szFormatted )
{
BYTE *SETBUFF = ((BYTE*)(pBuf)) + dwOffset ;
szFormatted.Format(_T("%08d: %.2X %.2X "), dwOffset, SETBUFF[0], SETBUFF[1]);
}
for(int i = Start; i <= End; i++)
{
GetFormattedData(m_lpData, (i + m_nVScrollBarPos )* m_cyChar, szFormatted);
m_pDC.TextOut((1 - m_nHScrollBarPos) * m_cxChar, i * m_cyChar, szFormatted);
}
A bit unclear description.
sarfaraznawaz wrote: how i had display the text see ..............
Do you want to know how to display ellipsis at the end of the string if it's too long? You could use CDC::DrawText()[^] with DT_END_ELLIPSIS formatting instead of TextOut() .
sarfaraznawaz wrote: I am trying to select the text of client area which i had paint by using the textout().
If, on the other hand, you want to be able to select portions of the text, you would be better off using an edit control. You can style them to look like you need them too (mostly true), removing frames and such.
If you want to let the user only select the entire text, you could respond to WM_LBUTTONDOWN events within your text rectangle, and redraw the text with a different background color set in the DC.
|
|
|
|
|
thanks
i am very new to MFC. trying to select the particular text of the client area.i have use
OnLButtonDown and OnLButtonup had two points .i am not able change background color of particular text.provid me some example .....
|
|
|
|
|
You use CDC::SetBkColor()[^] to set the background color.
You will have to draw the text in more than one go to make parts of it appear selected. Have a look at CDC::SetTextAlign()[^] using the TA_UPDATECP flag to make multiple calls to TextOut() create one line of text.
I still think using a CEdit might me a lot easier.
Use CDC::GetTextExtent()[^] to calculate width of characters in your string.
|
|
|
|
|
thanks
i tried but i am not able select text please help me out this..
this is how i had written the code.... where and how to use the textout() method
void CMainFrame::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect rct;
CPaintDC pdc(this);
currentpoint = point;
GetClientRect(&rct);
rct.SetRect(m_ptFrom,m_ptTo);
if (bLBtnpressed)
{
pdc.SetBkColor(RGB(0,0,192));
}
CFrameWnd::OnMouseMove(nFlags, point);
}
|
|
|
|
|
You should move your painting code to OnPaint if you use a CDialog, or to OnDraw if you use a doc/view architecture.
Keep a selection range as a member variable, as well as the text printed.
Maybe something like this in your header file
struct SelectionRange
{
SelectionRange(int f, int e) : first(f), end(e) {}
int first;
int end;
bool empty() const { return first < 0 || end < first; }
size_t size() const { return end - first; }
};
SelectionRange range;
const TCHAR* text; In constructor:
range(2, 4);
text = _T("Hello world!"); In OnPaint() or OnDraw():
int oldBM = dc.SetBkMode(TRANSPARENT);
if (range.empty())
{
dc.TextOut(30, 30, text); }
else
{
dc.MoveTo(30, 30);
UINT ta = dc.GetTextAlign();
UINT oldTA = dc.SetTextAlign(ta | TA_UPDATECP);
dc.TextOut(0, 0, text, range.first);
dc.SetBkMode(oldBM);
COLORREF oldTC = dc.SetTextColor(RGB(255, 255, 255));
COLORREF oldBC = dc.SetBkColor(RGB(0, 0, 255));
dc.TextOut(0, 0, &text[range.first], range.size());
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(oldTC);
dc.SetBkColor(oldBC);
dc.TextOutA(0, 0, &text[range.end]);
dc.SetTextAlign(oldTA);
}
dc.SetBkColor(oldBM);
This will draw the text with characters text[range.first] to text[range.last-1] as selected.
You will have to calculate the range variable in your WM_LBUTTONDOWN / WM_MOUSEMOVE handlers, and add some logic to support negative ranges (last < first), or just turn them into positive ranges if that is sufficient your needs.
|
|
|
|
|
thanks ....
but i am not using any one CDialog, OnDraw and doc/view architecture.
i am using app class & mainframe in SD i removed all other classes and what ever text is there i had display in paint only CMainFrame::OnPaint().............
trying to develop the editor kind of application without using editview ..
i have drawn the rectangle mouse event i want change the color what ever text comes under the rectangle should not hide text that is my problem .....
|
|
|
|
|
I understand, but I don't see the difference really. You can still use the code I gave you, just put it in CMainFrame::OnPaint() instead. Unless the text in there is a bitmap image, you can should be able to query the window for its text.
|
|
|
|
|
i tried but i confused where to write code of your ......
once you check my code
CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){
HANDLE hfile;
hfile=CreateFile(_T("\\\\.\\C:"),GENERIC_READ,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hfile == INVALID_HANDLE_VALUE)
{
MessageBox(_T("Fail to open the drive"),_T(" "),MB_OK | MB_ICONERROR);
return -1;
}
m_lpData = new BYTE[1024];
DWORD dwNumberofByteToRead =0;
if(!ReadFile(hfile,m_lpData,1024,&dwNumberofByteToRead,NULL))
{
MessageBox(_T("Fail to read the drive"),_T(" "),MB_OK | MB_ICONERROR);
return -2
}
and onpaintcode:
{
CPaintDC dc(this); // device context for painting
CString szFormatted;
CRect rect;
CDC m_pDC;
CBrush m_Brush,m_brush1;
GetClientRect(&rect);
m_pDC.CreateCompatibleDC(&dc);
m_bitmap.CreateBitmap (rect.Width(), rect.Height(),0,0,NULL);
m_pDC.SelectObject(&m_bitmap);
m_Brush.CreateSolidBrush(RGB(255,255,255));
m_pDC.SelectObject(&m_Brush);
m_pDC.FillRect(rect,&m_Brush);
TRACE("\n Left: %d, Top: %d Right: %d Bottom: %d",rect.left, rect.top, rect.right, rect.bottom);
//TRACE("\n Start Line: %d End Line: %d", rect.top /cyChar, rect.bottom / cyChar);
m_pDC.SelectObject(GetStockObject(OEM_FIXED_FONT ));
memset (&si, 0, sizeof (SCROLLINFO));
si.cbSize = sizeof (SCROLLINFO);
si.fMask = SIF_POS | SIF_PAGE |SIF_RANGE;
GetScrollInfo(SB_VERT, &si, SIF_POS | SIF_PAGE | SIF_RANGE);
m_nVScrollBarPos = si.nPos;
GetScrollInfo(SB_HORZ, &si, SIF_POS | SIF_PAGE |SIF_RANGE);
m_nHScrollBarPos = si.nPos;
int Start = rect.top / m_cyChar;
int End = rect.bottom / m_cyChar;
TRACE ("\nStart: %u, End: %u YCord: %d", Start, End);
for(int i = Start; i<= End; i++)
{
GetFormattedData(m_lpData, (i + m_nVScrollBarPos )*BYTE_OFFSET, szFormatted);
m_pDC.TextOut((1 - m_nHScrollBarPos) * m_cxChar , i * m_cyChar, szFormatted);
}
m_bitmap.DeleteObject();
dc.BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),&m_pDC,rect.left,rect.top,SRCCOPY);
CPen pen;
dc.SelectObject(GetStockObject(NULL_BRUSH));
dc.Rectangle(start_pt.x, start_pt.y, end_pt.x, end_pt.y);
rect.SetRect(start_pt,end_pt);
ReleaseDC(&m_pDC);
}
|
|
|
|
|
You should put it where the for-loop is.
Also, you're deleting the m_bitmap object before you have unselected it from the DC, and before the BitBlt call. Delete it after BitBlt, and after you have unselected it (using SelectObject with the result you got from the first call when you selected the bitmap into the DC).
Is there a good reason for not using an edit control?
Although the Crystal Edit - syntax coloring text editor[^] article isn't exactly what you need, you might get some ideas there.
|
|
|
|
|
|
Which part is not working? The code snippet I posted above works in my sample application for drawing selections.
Also, if you're dealing with non-client area painting, are you doing it in the OnNcPaint[^] handler?
|
|
|
|
|
i am dealing with client area now i have created the rectangular region by the points which i had from Lbuttondown and Lbuttonup. and then i inverted the region which gives the color but the color wont remain when i scroll the text .............
|
|
|
|
|
Maybe you could respond to the scroll messages from your window, and save the offset to use in the painting routine?
|
|
|
|
|
How to use PostMessage function to Close the Recorder of MS-Window-XP
I'v tried following codes, but they do not work.
::PostMessage(hWnd, WM_SYSCOMMAND, ........
or
::PostMessage(hWmd, WM_CLOSE, 0, 0);
or
::PostMessage(hWnd, WM_DESTROY, 0, 0);
Reply·Email·View Thread·Link·Bookmark | Edit·Delete
|
|
|
|
|
WM_CLOSE is the best way. Most applications do some confirmations like "do you want to save or not?" before closing. To forcefully close the app without popping up these kind of messages, try WM_DESTROY directly.
|
|
|
|
|
The window messages you send are correct, so the problem should be something else. Is your window handle hWnd correct? How did you obtain it?
modified 13-Sep-18 21:01pm.
|
|
|
|
|
cerne1840@yahoo.com wrote: I'v tried following codes, but they do not work.
I didn't notice it. As Thaddeus Jones already told, the problem can be of the window handle.
|
|
|
|
|
when a combo-box closed up (with drop-down or drop-down-list style), I need to get its outside height.
GetWindowRect() works normally, but, when its parent window is in min (icon) state then to restore or max state, the function doesn't work - gets rect including drop-down part.
I use this way:
int iItemH=MyComboBox.GetItemHeight(-1);
int iFrmH=::GetSystemMetrics(SM_CYEDGE);
int iCmbH=iItemH+iFrmH*2+2;
The iCmbH is what I need, but code looks no good,do you have a trusted and better idea to get its height?
|
|
|
|
|
|
Thanks, but the function is not working on Win2K - our users may use win2k.
|
|
|
|
|
i did'n got what your saying
|
|
|
|
|