用VC进行SDK编程碰到的问题
- 提问者网友:皆是孤独
- 2021-04-24 08:43
- 五星知识达人网友:杯酒困英雄
- 2021-04-24 09:39
1.要获取内容需要向目标发送消息 WM_GETTEXT 具体信息你去查MSDN这里不多说了。
返回值:
The return value is the number of TCHARs copied, not including the terminating null character
看到这个你的第一个你自己应该明白了吧!
2. 通过atof 转换获取 double 数据
函数名: atof
功 能: 把字符串转换成浮点数
用 法: double atof(const char *nptr);
然后加运算 得到结果在利用 sprintf以double形式输出到缓冲区,然后显示。
这里我作了一个例子(其中一种解决方式,有兴趣可以在翻翻说解法很多),分别向两个"edit"中输入double数据,为了方便起见,我在关闭窗口的时候即
受到消息WM_DESTROY 将double数据转换然后MessageBox显示数据
参考代码,主要是受到消息WM_DESTROY时候的处理,至于你自己的具体问题你套用就OK了。
SendMessage(hwndEdit1,WM_GETTEXT,20,(LPARAM)num);
dval1=atof(num);
SendMessage(hwndEdit2,WM_GETTEXT,20,(LPARAM)num);
dval2=atof(num);
sprintf(num,"%lf",dval1+dval2);
MessageBox(NULL,num,"result",MB_OK);
--------------
如下是完整例子
#include <windows.h>
#define ID_EDIT 1
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
TCHAR szAppName[] = TEXT ("PopPad1") ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
static HWND hwndEdit1,hwndEdit2;
TCHAR num[20];
double dval1, dval2;
switch (message)
{
case WM_CREATE:
hwndEdit1 = CreateWindow (TEXT ("edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
WS_BORDER | ES_LEFT | ES_MULTILINE |
ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 0, 0, hwnd, (HMENU) ID_EDIT,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
hwndEdit2= CreateWindow (TEXT ("edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
WS_BORDER | ES_LEFT | ES_MULTILINE |
ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 0, 0, hwnd, (HMENU) ID_EDIT,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
return 0 ;
case WM_SETFOCUS :
SetFocus (hwndEdit1) ;
return 0 ;
case WM_SIZE :
MoveWindow (hwndEdit1, 0, 0, LOWORd (lParam)/5, HIWORd (lParam)/10, TRUE) ;
MoveWindow (hwndEdit2,0,HIWORd(lParam)/10,LOWORd(lParam)/5,HIWORd(lParam)/10,TRUE);
return 0 ;
case WM_DESTROY:
SendMessage(hwndEdit1,WM_GETTEXT,20,(LPARAM)num);
dval1=atof(num);
SendMessage(hwndEdit2,WM_GETTEXT,20,(LPARAM)num);
dval2=atof(num);
sprintf(num,"%lf",dval1+dval2);
MessageBox(NULL,num,"result",MB_OK);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
- 1楼网友:拾荒鲤
- 2021-04-24 11:26
- 2楼网友:狂恋
- 2021-04-24 10:56
为啥要用SDK捏?MFC可以很快的实现哦,直接给编辑框绑定一个double型变量就可以了,不过SDK貌似也可以,我不常用SDK,所以不太了解SDK的一些机制