本文目錄一覽:
C語言。。messagebox用法
窗體上放置三個TextBox,分別輸入a,b,c的值,控制項命名:tbA,tbB,tbC
再放一個Button,設置Text為:求解,其單擊後台代碼如下:
private void button1_Click(object sender, EventArgs e)
{
double a = 0;
double b = 0;
double c = 0;
try
{
if (tbA.Text.Length == 0)
{
MessageBox.Show(“請輸入a的值”, “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
a = Convert.ToDouble(tbA.Text);
}
catch
{
MessageBox.Show(“您輸入的a的值不是一個數字,請重新輸入”, “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
tbA.Focus();
return;
}
try
{
if (tbB.Text.Length == 0)
{
MessageBox.Show(“請輸入b的值”, “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
b = Convert.ToDouble(tbB.Text);
}
catch
{
MessageBox.Show(“您輸入的b的值不是一個數字,請重新輸入”, “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
tbB.Focus();
return;
}
try
{
if (tbC.Text.Length == 0)
{
MessageBox.Show(“請輸入c的值”, “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
c = Convert.ToDouble(tbC.Text);
}
catch
{
MessageBox.Show(“您輸入的c的值不是一個數字,請重新輸入”, “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
tbC.Focus();
return;
}
if (a == 0)
{
if (b == 0)
{
if (c == 0)
{
MessageBox.Show(string.Format(“方程{0}x^2+{1}x+{2}=0的解為 x={3}”, a, b, c, “任意實數”), “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(string.Format(“方程{0}x^2+{1}x+{2}=0無實數解”, a, b, c), “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show(string.Format(“方程{0}x^2+{1}x+{2}=0的解為 x={3}”, a, b, c, -c / b), “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
double delta = b * b – 4 * a * c;
if (delta 0)
{
MessageBox.Show(string.Format(“方程{0}x^2+{1}x+{2}=0無實數解”, a, b, c), “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(string.Format(“方程{0}x^2+{1}x+{2}=0的解為 x1={3} , x2={4}”, a, b, c, (-b + System.Math.Sqrt(delta)) / 2 / a, (-b – System.Math.Sqrt(delta)) / 2 / a), “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
用c語言如何實現彈除對話框
#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;
}
C語言中messagebox的用法
消息框的函數原型:
int MessageBox(HWND hwnd,LPCTSTR lpsztext,LPCSTR lpsztitle,UINT ustyle);
消息框函數有4個參數:
第1個參數是父窗口的句柄。為NULL,說明消息框沒有父窗口。
第2個參數就是一個指向要顯示字元串的指針
第3個參數是消息框本身的標題。
第4個參數是指定消息框的內容和形為(即該消息框有幾個按鈕、文本對齊等狀態,可以在20多個屬性值中進行組合)
MessageBox的第4個參數可以是在WINUSER.H中定義的一組前綴以MB_開始的常數組合.
可以使用C語言的”或”(|)運算符將下面顯示的三組中各選一個常數組合起來指定消息框的內容和形為:
顯示哪些按鈕:
#define MB_OK 0X00000000L
#define MB_OKCANCEL 0X00000001L
#define MB_ABORTRERYGNORE 0X00000002L
#define MB_YESNOCANCEL 0X00000003L
#define MB_YESNO 0X00000004L
#define RERYCANCEL 0X00000005L
焦點在哪個按鈕上:
#define MB_DEFBUTTON1 0X00000000L
#define MB_DEFBUTTON2 0X00000100L
#define MB_DEFBUTTON3 0X00000200L
#define MB_DEFBUTTON4 0X00000300L
圖示的外觀:
#define MB_ICONHAND 0x00000010L
#define MB_ICONQUESTION 0x00000020L
#define MB_ICONEXCLAMATION 0x00000030L
#define MB_ICONASTERISK 0x00000040L
圖示的某些有替代名稱:
#define MB_ICONWARNING MB_ICONEXCLAMATION
#define MB_ICONERROR MB_ICONHAND
#define MB_ICONINFORMATION MB_ICONASTERISK
#define MB_ICONSTOP MB_ICONHAND
示例:
MessageBox(NULL, “Hello, Windows!”,”hello”, MB_OK );
MessageBox(NULL, “Hello, Windows!”,”HelloMsg”, MB_YESNO|MB_ICONEXCLAMATION) ;
MessageBox(NULL, “Hello, Windows!”,”HelloMsg”, MB_YESNO|MB_DEFBUTTON1) ;//表示窗口出來後焦點 focus落在Yes(第一個)按鈕上
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/195994.html