怎样用c语言给对话框中的文本框赋值?

Python038

怎样用c语言给对话框中的文本框赋值?,第1张

对话框可以用MessageBox函数。

1.

MessageBox()

功能显示一个消息对话框。

语法MessageBox(text,title{,icon{,button{,default}}})

参数title:string类型,指定消息对话框的标题text:指定消息对话框中显示的消息,该参数可以是数值数据类型、字符串或boolean值icon:Icon枚举类型,可选项,指定要在该对话框左侧显示的图标button:Button枚举类型,可选项,指定显示在该对话框底部的按钮default:数值型,可选项,指定作为缺省按钮的按钮编号,按钮编号自左向右依次计数,缺省值为1,如果该参数指定的编号超过了显示的按钮个数,那么MessageBox()函数将使用缺省值返回值Integer。函数执行成功时返回用户选择的按钮编号(例如1、2、3等),发生错误时返回-1。如果任何参数的值为NULL,MessageBox()函数返回NULL。

用法:当你的应用程序需要显示一段简短信息(比如显示出错、警告等信息)时,没有必要自己从头创建窗口、安排控件,使用MessageBox()函数既简单又方便。用户只有响应该窗口后,程序才能继续运行下去。MessageBox()函数的icon参数指定显示在窗口中的图标,它是枚举类型,可能取值为:

取值

图标Information!

StopSign!

Exclamation!

Question!

None!

无图标其中Information!是Icon参数的缺省值。Button参数指定在窗口中显示哪些按钮,有效取值为:取值中文Windows

95下显示OK!“确定”按钮OKCancel!“确定”、“取消”按钮YesNo!“是”、“否”按钮

YesNoCancel!“是”、“否”、“取消”按钮RetryCancel!“重试”、“取消”按钮AbortRetryIgnore!“终止”、“重试”、“忽略”按钮

函数功能:该函数创建、显示、和操作一个消息框。消息框含有应用程序定义的消息和标题,加上预定义图标与Push(下按)按钮的任何组合。

2.

例程:

#include

#include

int main()

{

char str[99]

gets(str) //str储存输入的字符串,用来给文本框赋值

MessageBox(0,str,"对话框",MB_OK) //给对话框的文本框赋值str

return 0

}

#include <tchar.h>

#include <windows.h>

HINSTANCE _HInstance // 应用程序句柄

TCHAR _Title[] = _T("简单文本框")// 定义窗口的标题

TCHAR _WindowClass[] = _T("MySimpleTextBoxApp")// 主窗口类名

ATOM _RegisterClass()// 注册主窗口类

HWND _CreateWindow(int nCmdShow) // 创建主窗口

LRESULT CALLBACK _WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) // 主窗口消息处理函数

TCHAR _TextBoxClass[] = _T("MySimpleTextBox")// 文本框的类名

ATOM _RegisterTextBoxClass() // 注册文本框的类

HWND _CreateTextBoxWindow(HWND hParentWnd) // 创建文本框

LRESULT CALLBACK _TextBoxWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)// 文本框窗口消息处理函数

void _DrawText(HDC hDC) // 绘制文本

void _SetCaretPos(HWND hWnd) // 设置光标位置

void _UpdateWindow(HWND hWnd)// 更新窗口

// 一些常量定义

#define MAINWINDOW_WIDTH400 // 主窗口宽度

#define MAINWINDOW_HEIGHT 200 // 主窗口高度

#define TEXTBOX_WIDTH 300 // 文本框宽度

#define TEXTBOX_HEIGHT 20 // 文本框高度

#define TEXTBOX_MAXLENGTH 1024 // 文本框中文本的最大长度

TCHAR _String[TEXTBOX_MAXLENGTH + 1] = _T("")// 文本

int_StringPosition = ::_tcslen(_String) // 光标插入点所在的位置

int APIENTRY _tWinMain(HINSTANCE hInstance,// 当前的应用程序句柄

HINSTANCE hPrevInstance, // 前一个应用程序实例的句柄(在Win32上,始终为NULL)

LPTSTR lpCmdLine,// 命令行参数

intnCmdShow // 窗口的显示样式

)

{

_HInstance = hInstance

_RegisterClass()// 注册窗口类

if(_CreateWindow(nCmdShow) == NULL) // 创建窗口

return FALSE

MSG msg

while (::GetMessage(&msg, NULL, 0, 0))// 从消息队列中获取消息

{

::TranslateMessage(&msg) // 转译一些特殊的消息

::DispatchMessage(&msg)// 执行消息处理

}

return (int)msg.wParam

}

// 注册应用程序窗口类

ATOM _RegisterClass()

{

WNDCLASSEX wc

::ZeroMemory(&wc, sizeof(wc))// 作为一步清空,是为了让未赋值的字段的默认值为(或NULL)

wc.cbSize = sizeof(wc)

wc.style = CS_HREDRAW | CS_VREDRAW // 指定当窗口横向和纵向的尺寸发生变化时都会重绘窗口

wc.hInstance = _HInstance

wc.hbrBackground = (HBRUSH)( COLOR_APPWORKSPACE + 1) // 指定主窗口背景为“工作区域”系统颜色

wc.lpszClassName = _WindowClass // 此为要注册的类名,创建窗口时要以此类名为标识符

wc.lpfnWndProc = _WndProc // 此为处理窗口消息的函数

return ::RegisterClassEx(&wc)// 调用API函数注册窗口类

}

// 创建窗口

HWND _CreateWindow(int nCmdShow)

{

HWND hWnd = ::CreateWindow(_WindowClass, _Title, WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, MAINWINDOW_WIDTH, MAINWINDOW_HEIGHT, NULL, NULL, _HInstance, NULL)

if(hWnd == NULL)

return NULL

::ShowWindow(hWnd, nCmdShow)

::UpdateWindow(hWnd)

return hWnd

}

// 窗口处理过程

LRESULT CALLBACK _WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

static HWND hTextBoxWnd

switch (message)

{

case WM_CREATE: {

_RegisterTextBoxClass() // 注册文本框的类

hTextBoxWnd = _CreateTextBoxWindow(hWnd)// 创建文本框

} break

case WM_ACTIVATE:// 当窗口被激活时,将焦点设置在文本框上

::SetFocus(hTextBoxWnd)

break

case WM_SETCURSOR: { // 设置光标形状

static HCURSOR hCursor = ::LoadCursor(NULL, IDC_ARROW)

::SetCursor(hCursor)

} break

case WM_DESTROY: // 应用程序被关闭

::PostQuitMessage(0)

break

default:

return ::DefWindowProc(hWnd, message, wParam, lParam)

}

return (LRESULT)0

}

// 注册文本框的类

ATOM _RegisterTextBoxClass()

{

WNDCLASSEX wc

::ZeroMemory(&wc, sizeof(wc))

wc.cbSize = sizeof(wc)

wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS // 指定当窗口尺寸发生变化时重绘窗口,并且响应鼠标双击事件

wc.hInstance = _HInstance

wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1)// 指定窗口背景颜色为系统颜色“窗口背景”

wc.lpszClassName = _TextBoxClass // 指定要注册的窗口类名,创建窗口时要以此类名为标识符

wc.lpfnWndProc = _TextBoxWndProc // 处理窗口消息的函数

return ::RegisterClassEx(&wc)// 调用API函数注册文本框窗口

}

// 创建文本框

HWND _CreateTextBoxWindow(HWND hParentWnd)

{

// 之下代码是为了让文本框显示在父窗口中央,而计算位置

RECT parentWndRect

::GetClientRect(hParentWnd, &parentWndRect) // 获取父窗口客户区的位置

int left = (parentWndRect.right - TEXTBOX_WIDTH) / 2, top = (parentWndRect.bottom - TEXTBOX_HEIGHT) / 2

// 创建文本框

HWND hWnd = ::CreateWindow(_TextBoxClass, NULL, WS_CHILDWINDOW | WS_VISIBLE,

left, top, TEXTBOX_WIDTH, TEXTBOX_HEIGHT,

hParentWnd, NULL, _HInstance, NULL)

return hWnd

}

// 文本框消息的处理过程

LRESULT CALLBACK _TextBoxWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

switch (message)

{

case WM_PAINT: { // 绘制这里之所以加一对大括号,是为了让之下定义的变量局部化

static PAINTSTRUCT ps

static RECT rect

HDC hDC = ::BeginPaint(hWnd, &ps) // 开始绘制操作

::GetClientRect(hWnd, &rect) // 获取客户区的尺寸

::DrawEdge(hDC, &rect, EDGE_SUNKEN, BF_RECT) // 绘制边框,EDGE_SUNKEN表示绘制样式为内嵌样式,BF_RECT表示绘制矩形边框

_DrawText(hDC) // 绘制文本

::EndPaint(hWnd, &ps) // 结束绘制操作

} break

case WM_SETFOCUS: {// 获得焦点

::CreateCaret(hWnd, (HBITMAP)NULL, 1, TEXTBOX_HEIGHT-5)// 创建光标

_SetCaretPos(hWnd) // 设置光标位置

::ShowCaret(hWnd) // 显示光标

} break

case WM_KILLFOCUS: // 失去焦点

::HideCaret(hWnd) // 隐藏光标

::DestroyCaret() // 销毁光标

break

case WM_SETCURSOR: { // 设置光标形状

static HCURSOR hCursor = ::LoadCursor(NULL, IDC_IBEAM)

::SetCursor(hCursor)

} break

case WM_CHAR: {// 字符消息

TCHAR code = (TCHAR)wParam

int len = ::_tcslen(_String)

if(code <(TCHAR)' ' || len >= TEXTBOX_MAXLENGTH)

return 0

::MoveMemory(_String + _StringPosition + 1, _String + _StringPosition, (len - _StringPosition + 1) * sizeof(TCHAR))

_String[_StringPosition ++] = code

_UpdateWindow(hWnd)

_SetCaretPos(hWnd)

} break

case WM_KEYDOWN: { // 键按下消息

TCHAR code = (TCHAR)wParam

switch (code)

{

case VK_LEFT: // 左光标键

if(_StringPosition >0)

_StringPosition --

break

case VK_RIGHT: // 右光标键

if(_StringPosition <(int)::_tcslen(_String))

_StringPosition ++

break

case VK_HOME: // HOME 键

_StringPosition = 0

break

case VK_END: // END 键

_StringPosition = ::_tcslen(_String)

break

case VK_BACK: // 退格键

if(_StringPosition >0)

{

::MoveMemory(_String + _StringPosition - 1, _String + _StringPosition, (::_tcslen(_String)-_StringPosition + 1) * sizeof(TCHAR))

_StringPosition --

_UpdateWindow(hWnd)

}

break

case VK_DELETE: { // 删除键

int len = ::_tcslen(_String)

if(_StringPosition <len)

{

::MoveMemory(_String + _StringPosition, _String + _StringPosition + 1, (::_tcslen(_String) - _StringPosition + 1) * sizeof(TCHAR))

_UpdateWindow(hWnd)

}

} break

}

_SetCaretPos(hWnd)

} break

case WM_LBUTTONDOWN: { // 鼠标单击,设置光标位置

int x = LOWORD(lParam)

HDC hDc = ::GetDC(hWnd)

int strLen = ::_tcslen(_String), strPos = 0

SIZE size

for (strPos=0strPos<strLenstrPos++)

{

::GetTextExtentPoint(hDc, _String, strPos, &size)

if(size.cx + 4 >= x)

break

}

_StringPosition = strPos

::GetTextExtentPoint(hDc, _String, strPos, &size)

::SetCaretPos(size.cx + 4, 3)

::ReleaseDC(hWnd, hDc)

} break

default:

return ::DefWindowProc(hWnd, message, wParam, lParam)

}

return (LRESULT)0

}

// 更新窗口

void _UpdateWindow(HWND hWnd)

{

RECT rect

::GetClientRect(hWnd, &rect)

::InvalidateRect(hWnd, &rect, TRUE)

::UpdateWindow(hWnd)

}

// 绘制文本

void _DrawText(HDC hDC)

{

int len = ::_tcslen(_String)

::TextOut(hDC, 4, 2, _String, len)

}

// 设置光标位置

void _SetCaretPos(HWND hWnd)

{

HDC hDC = ::GetDC(hWnd)

SIZE size

::GetTextExtentPoint(hDC, _String, _StringPosition, &size)

::SetCaretPos(4 + size.cx, 3)

::ReleaseDC(hWnd, hDC)

}

#include

#include

char format[]="%s%s\n";

char hello[]="Hello";

char world[]="world";

HWND hwnd;void main(void)

asm

//push NULL

//call dword ptr GetModuleHandle

//mov hwnd,eax push MB_OK mov eax,offset world push eax mov eax,offset hello push eax push 0//说明此处不能将前面注释掉代码处得到的hwnd压栈,否则对话框弹不出来。

call dword ptr MessageBox

}

}

WINDOWS程序MessagBox

WINDOWS或控制台 assert

C/C++ code

// crt_assert.c

// compile with: /c

#include <stdio.h>

#include <assert.h>

#include <string.h>

void analyze_string( char *string )   // Prototype

int main( void )

{

char  test1[] = "abc", *test2 = NULL, test3[] = ""

printf ( "Analyzing string '%s'\n", test1 )fflush( stdout )

analyze_string( test1 )

printf ( "Analyzing string '%s'\n", test2 )fflush( stdout )

analyze_string( test2 )

printf ( "Analyzing string '%s'\n", test3 )fflush( stdout )

analyze_string( test3 )

}

// Tests a string to see if it is NULL,

// empty, or longer than 0 characters.

void analyze_string( char * string )

{

assert( string != NULL )        // Cannot be NULL

assert( *string != '\0' )       // Cannot be empty

assert( strlen( string ) >2 )  // Length must exceed 2

}

扩展资料:

#include <windows.h>

#include <Commdlg.h>

#include <stdio.h>

// 返回值: 成功 1, 失败 0

// 通过 path 返回获取的路径

int FileDialog(char *path)

{

OPENFILENAME ofn

ZeroMemory(&ofn, sizeof(ofn))

ofn.lStructSize = sizeof(ofn)// 结构大小

ofn.lpstrFile = path// 路径

ofn.nMaxFile = MAX_PATH// 路径大小

ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0"// 文件类型

ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST

return GetOpenFileName(&ofn)

}

int main(char argc, char *argv[])

{

char szFile[MAX_PATH] = {0}

if(FileDialog(szFile))

{

puts(szFile)

}

getchar()

return 0

}