visual c++ 6.0 code for implementing enter command - visual-c++

How do I implement the "enter" command in a Visual C++ 6.0 project using the MFC application wizard(exe)?
It would be somewhat modification of the following code for finding the size of the entered string:
void CCentredView::OnDraw(CDC*pDC)
{
CCentredDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CRect rect;
GetWindowRect(&rect);
int x= rect.Width()/2;
int y= rect.Height()/2;
CSize size = pDC->GetTextExtent(pDoc->StringData);
//...
}
Now to get code for enter command we have to check if the struck key is a carriage return, \r, and if so move to the next line by adding the height of the text string to the y variable to skip to the next text line on the screen.
But, I am not getting how to implement the code!

Homework question?
Either way, catch the return key press by filtering for WM_KEYDOWN with VK_RETURN in PreTranslateMessage. Do your carriage return inserting there.

Related

Detecting CTRL inside CEdit::OnChar and testing nChar value?

I derived my own control from CEdit and it behaves as I intend:
#define IsSHIFTpressed() ( (GetKeyState(VK_SHIFT) & (1 << (sizeof(SHORT)*8-1))) != 0 )
void CEditEx::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (IsCTRLpressed() && nChar == 2)
{
// Do something
return;
}
if (IsCTRLpressed() && nChar == 9)
{
// Do something
return;
}
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
However, I have two questions about how I am detecting the key press:
Is it possible to detect CTRL being pressed from inside OnChar without the need to use GetKeyState?
Are there any constants for comparing against "b" (2) and "i" (9)? I only knew I needed to use those numeric values from when I debugged into the handler.
As you have noted, the value of the nChar argument to OnChar for keyboard entries of Ctrl + "a letter" (independent of the case) will be the ASCII "control-key" values, 1 (for "a") thru 26 (for "z").
To answer your second point: a search through the <WinUser.h> header file shows no VK_xxx tokens for these; however, be aware that some of these control codes are used (by convention) for other actions: Ctrl+M (decimal 13) is equivalent to Return or Enter, and the header has #define VK_RETURN 0x0D; further, for one of your specific cases, Ctrl+I (9) is the ASCII TAB character, and the header, accordingly, has the #define VK_TAB 0x09 definition.
Although the Ctrl+B ASCII code (0x02) is much less used these days (STX, or "Start of Text"), that value is used by Windows for the right mouse button (#define VK_RBUTTON 0x02).
So, to answer your first point: Yes, you will need to make the GetKeyState(VK_CONTROL) check! Without that, a right-click will likely give you a false Ctrl+B and the Tab key will give a false Ctrl+I.
Furthermore, though I have no 'hard evidence' other than your own investigations, I think that a right-click while the Control key is down will generate a different value for nChar (i.e. not 2), and Ctrl+Tab will generate an nChar different from that for Tab alone.

proper way of catching control+key in ncurses

What is the proper way of catching a control+key in ncurses?
current im doing it defining control like this:
#define ctl(x) ((x) & 0x1f)
it works ok, but the problem is that i cannot catch C-j and ENTER at the same time, and this is because:
j = 106 = 1101010
0x1f = 31 = 0011111
1101010 & 0011111 = 0001010 = 10 = ENTER key..
So.. how shall I catch it?
Thanks!
--
Edit:
If i try the code below,
I am not able to catch the enter key correctly, not even in the numeric keyboard. Enter gets catched as ctrl-j.
#include <stdio.h>
#include <ncurses.h>
#define ctrl(x) ((x) & 0x1f)
int main(void) {
initscr();
int c = getch();
nonl();
switch (c) {
case KEY_ENTER:
printw("key: %c", c);
break;
case ctrl('j'):
printw("key: ctrl j");
break;
}
getch();
endwin();
return;
}
New code:
#include <stdio.h>
#include <ncurses.h>
#define ctrl(x) ((x) & 0x1f)
int main(void) {
initscr();
int l = -1;
int c = getch();
cbreak();
noecho();
nonl();
keypad(stdscr, TRUE);
switch (c) {
case KEY_ENTER:
printw("key: %c", c);
break;
case ctrl('j'):
printw("key: ctrl j");
break;
}
printw("\nnow press a key to end");
getch();
endwin();
return;
}
Try nonl:
The nl and nonl routines control whether the underlying display device
translates the return key into newline on input, and whether it translates newline into return and line-feed on output (in either case, the
call addch('\n') does the equivalent of return and line feed on the
virtual screen). Initially, these translations do occur. If you disable them using nonl, curses will be able to make better use of the
line-feed capability, resulting in faster cursor motion. Also, curses
will then be able to detect the return key.
Further reading: the Notes section of the getch manual page:
Generally, KEY_ENTER denotes the character(s) sent by the Enter key on
the numeric keypad:
the terminal description lists the most useful keys,
the Enter key on the regular keyboard is already handled by the
standard ASCII characters for carriage-return and line-feed,
depending on whether nl or nonl was called, pressing "Enter" on the
regular keyboard may return either a carriage-return or line-feed,
and finally
"Enter or send" is the standard description for this key.
That addresses the question about newline/carriage-return translation. A followup comment is a reminder to point out that the manual page gives basic advice in the Initialization section:
To get character-at-a-time input without echoing (most interactive,
screen oriented programs want this), the following sequence should be
used:
initscr(); cbreak(); noecho();
and that OP's sample program did not use cbreak (or raw). The manual page for cbreak says
Normally, the tty driver buffers typed characters until a newline or
carriage return is typed. The cbreak routine disables line buffering
and erase/kill character-processing (interrupt and flow control characters are unaffected), making characters typed by the user immediately
available to the program. The nocbreak routine returns the terminal to
normal (cooked) mode.
Initially the terminal may or may not be in cbreak mode, as the mode is
inherited; therefore, a program should call cbreak or nocbreak explicitly. Most interactive programs using curses set the cbreak mode.
Note that cbreak overrides raw. (See curs_getch(3x) for a discussion
of how these routines interact with echo and noecho.)
Also, in curs_getch you may read
If keypad is TRUE, and a function key is pressed, the token for that
function key is returned instead of the raw characters:
The predefined function keys are listed in <curses.h> as macros
with values outside the range of 8-bit characters. Their names begin with KEY_.
That is, curses will only return KEY_ENTER if the program calls keypad:
keypad(stdscr, TRUE);
For the sake of discussion, here is an example fixing some of the problems with your sample program as of May 17:
#include <stdio.h>
#include <ncurses.h>
#define ctrl(x) ((x) & 0x1f)
int
main(void)
{
int c;
initscr();
keypad(stdscr, TRUE);
cbreak();
noecho();
nonl();
c = getch();
switch (c) {
case KEY_ENTER:
printw("\nkey_enter: %d", c);
break;
case ctrl('j'):
printw("\nkey: ctrl j");
break;
default:
printw("\nkeyname: %d = %s\n", c, keyname(c));
break;
}
printw("\nnow press a key to end");
getch();
endwin();
return 0;
}
That is, you have to call keypad before getch, and the value returned for KEY_ENTER is not a character (it cannot be printed with %c).
Running on the Linux console with the usual terminal description, you will see only carriage return for the numeric keypad Enter, because that description does not use application mode. Linux console does support application mode, and a corresponding description could be written. As a quick check (there are differences...) you could set TERM=vt100 to see the KEY_ENTER.

C++ Converting textbox content to a Float

Tried carefully code from this thread
Converting textbox string to float?
Tried to leavemy question in a comment, but yet not allowed to do so...
The summary of that goes like having two text boxes, taking data from first, doing something, and returning the result to the second.
String^ i1 = Textbox1->Text;
float rez = (float)(Convert::ToDouble(i1)*4);
Textbox2->Text = rez.ToString();
and it works pretty good unless Textbox1 got a float within itself (upd. it works ok with '65', but doesn't work with '65.5').
Trying to execute that code - crushes a program
> Calc.exe!Calc::Form1::Button0_Click(System::Object^ sender = 0x01b29c58, System::EventArgs^ e = 0x01b45e40) Line 123 + 0x30 byte C++
Convert::ToDouble
Using the ToDouble(String) method is equivalent to passing value to the Double.Parse(String) method. Value is interpreted by using the formatting conventions of the current thread culture.
So, you need
Catch possible exceptions
try {
float rez = (float)(Convert::ToDouble(i1)*4);
}
catch (FormatException) {
// handle format error exception here
}
catch (OverflowException) {
// handle overflow exception here
}
Use IFormatProvider

Surrounding multiple selected lines with #ifdef

I'm trying to write a macro that will allow me to surround currently highlighted lines of text with an #ifdef. Ideally with the cursor placed after the #ifdef to be ready to enter the macro name. I'm able to record to create a macro, but I'm only able to do it for one line of code.
Before:
bool first_selected_line = false;
int second_selected_line = 0;
After:
#ifdef // if possible, cursor placed here in insert mode
bool first_selected_line = false;
int second_selected_line = 0;
#else
bool first_selected_line = false;
int second_selected_line = 0;
#endif
Any ideas?
You could do something along the lines of:
qjc#ifdef<esc>magpO#else<esc>gpO#endif<esc>`aq
Basically:
Start recording qj
Delete what you selected and go into insertmode c
Type your construct, pasting your code back as necessary
You put a mark (ma) just after typing #ifdef and jump back to it at the end
Repeat the macro with #j
Hope this example helps!
I would probably use snipmate or some other plugin to accomplish this task. There are couple ways to go about it manually though. Here's my solution for a macro:
Visually select the text then...
qqc#ifdef
<C-r><C-o>"
#else
<C-r><C-o>"
#endif<esc>'[A<C-o>q
You also don't have to visually select the text at all if you don't want to. Use the same macro but start with qqcj instead.

ncurses disabled showing cursur in bash

I wrote a program that i used ncurses library, i used, keys menu windows and other its facilities.
I run my program without any error.
After quit program, i can't see any input data (certainly same you run passwd command and wanna input new password), for example if you run ls, i can see output of ls(list of current dir) , but i can't see ls word.
How i solve this problem?
piece of my code:
WINDOW *menu_win,*qmenu_win,*amenu_win,*smenu_win;
char *query;
char *fields;
WriteFile *writePtr = new WriteFile();
ReadFile *readPtr = new ReadFile();
int highlight = 1;
int choice = 0;
int c;
initscr();
clear();
noecho();
cbreak(); /* Line buffering disabled. pass on everything */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
keypad(menu_win, TRUE);
mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
refresh();
print_menu(menu_win, highlight);
while (true)
Make sure your program calls endwin() before exiting. Otherwise, the state of the terminal may not be restored.
When this happens, type reset at your prompt.
Also be aware that problems may be due to your terminal program itself. If it does not properly emulate the terminal it claims to emulate, you will run into problems.
Or it could be a bug with your program: maybe you need to replace noecho with echo?

Resources