Archive | Thư viện lập trình RSS for this section

[C] Hàm màu dùng cho VC 6.0 (TextColor)

Sưu tầm và trình bày lại:

  • Trần Hán Huy – tranhanhuy.wordpress.com

I/ Giới thiệu:

  • Hàm này tương tự như SetColor trong tubro C
  • Hàm dưới dùng cho Visual C++ 6.0

II/ Công dụng:

  • Để tô màu nền thay đổi màu chữ
  • Hàm sử dụng đối với console

III/ thư viện kèm theo:

  •  windows.h

IV/ Bảng màu

V/ Hàm màu

void TextColor(int x)
{
	HANDLE mau;
	mau = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(mau,x);
}

V/ Bài hoàn chỉnh

#include <windows.h>
void TextColor(int x)
{
	HANDLE mau;
	mau = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(mau,x);
}
void main()
{
	TextColor(140);//số 140 dựa vào bảng màu
	printf("Xin chao moi nguoi den voi tranhanhuy.wordpress.com!");
}

[C] Hàm xóa màn hình trong VC 6.0

Sưu tầm và trình bày lại:

  • Trần Hán Huy – tranhanhuy.wordpress.com

2 cách này cũng gần tương đương với lệnh clrscr trong turbo C
Ở đây có 2 cách:

Cách 1 : Dùng 1 hàm trong C++

1/ Công dụng :

  • xóa hết màn hình.

2/Thư viện kèm theo:

  • stdlib.h hoặc windows.h

3/ khuyết điểm
Nếu bạn sử dụng hàm màu trong VC. thì sau khi thực hiện lệnh này, cả màn hình sẽ có màu nền trùng với cái hàm nền mà bạn gọi trước đó.
VD:
Trình tự gọi như sau:
– hàm màu có nền xanh
– hàm system(“cls”);
kết quả là cả màn hình có nền màu xanh, không phải nền màu đen như ban đầu nữa

4/ Code

#include "stdlib.h"
void main()
{
	system("cls");
}

Cách 2 : Lập trình 1 hàm thực hiện việc này bằng cách dùng các hàm console

1/ Công dụng :

  • xóa hết màn hình.

2/Thư viện kèm theo:

  • windows.h

3/ khuyết điểm

  • Hàm khá dài, khó nhớ

4/ code hàm

void cls( )
{
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD coordScreen = { 0, 0 }; /* here's where we'll home the
	cursor */
	DWORD cCharsWritten;
	CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
	DWORD dwConSize; /* number of character cells in the current buffer */

	/* get the number of character cells in the current buffer */
	GetConsoleScreenBufferInfo( hConsole, &csbi );

	/* fill the entire screen with blanks */
	FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',dwConSize, coordScreen, &cCharsWritten );

	/* get the current text attribute */
	GetConsoleScreenBufferInfo( hConsole, &csbi );

	/* now set the buffer's attributes accordingly */
	FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten );

	/* put the cursor at (0, 0) */
	SetConsoleCursorPosition( hConsole, coordScreen );
	return;
}

5/ code demo hoàn chỉnh

#include <windows.h>
void cls( )
{
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD coordScreen = { 0, 0 }; /* here's where we'll home the
	cursor */
	DWORD cCharsWritten;
	CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
	DWORD dwConSize; /* number of character cells in the current buffer */

	/* get the number of character cells in the current buffer */
	GetConsoleScreenBufferInfo( hConsole, &csbi );

	/* fill the entire screen with blanks */
	FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',dwConSize, coordScreen, &cCharsWritten );

	/* get the current text attribute */
	GetConsoleScreenBufferInfo( hConsole, &csbi );

	/* now set the buffer's attributes accordingly */
	FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten );

	/* put the cursor at (0, 0) */
	SetConsoleCursorPosition( hConsole, coordScreen );
	return;
}
void main()
{
	printf("Chao mung ban den voi tranhanhuy.wordpress.com");
	cls();
}

[C] Hàm tọa độ dùng cho VC 6.0 (gotoxy)

Sưu tầm và trình bày lại:

  • Trần Hán Huy – tranhanhuy.wordpress.com

I/ Giới thiệu:

  • Hàm này tương tự như hàm gotoxy trong turbo C
  • Hàm dưới dùng cho Visual C++ 6.0

II/ Công dụng:

  • 1 màn hình đen (hay gọi là dos) có 80 cột và 25 dòng
  • H àm sử dụng đối với console
  • Hàm có tác dụng di chuyển con trỏ đến vị trí x,y do người dùng định

III/ thư viện kèm theo:

  •  windows.h

IV/ Hàm tọa độ

#include <windows.h>
void ToaDo(int x,int y)
{
	HANDLE mau;
	mau = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD tinh = {x,y} ;
	SetConsoleCursorPosition(mau,tinh ) ;
}

V/ Bài hoàn chỉnh

#include <windows.h>
void ToaDo(int x,int y)
{
	HANDLE mau;
	mau = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD tinh = {x,y} ;
	SetConsoleCursorPosition(mau,tinh ) ;
}
void main()
{
	ToaDo(30,45);
	printf("Xin chao moi nguoi den voi tranhanhuy.wordpress.com!");
}

[ASP-C#] SqlDataSource.InsertCommand

Tác giả:

  • Trần Hán Huy – tranhanhuy.wordpress.com

Tác dụng:

  • dùng để gắn và truy xuất câu truy vấn của control SqlDataSource

Ngôn ngữ:

  • C#

Diễn tả:

  • Thiết kế 1 trang liên hệ để có người dùng có thể liên hệ với chủ website

Code giao diện

<table style="width:100%;">
<tr>
<td colspan="2">
    TRANG LIÊN HỆ - Trần Hán Huy - tranhanhuy.wordpress.com </td>
</tr>
<tr>
<td>
    Ten</td>
<td>
    <asp:TextBox ID="txtTen" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
    Email</td>
<td>
    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
    Tieu de</td>
<td>
    <asp:TextBox ID="txtTieuDe" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
    Noi dung</td>
<td>
    <asp:TextBox ID="txtNoiDung" runat="server" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
    Ngay gui</td>
<td>
    <asp:Label ID="lblNgayGui" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
    &nbsp;</td>
<td>
   <asp:Button ID="btnGui" runat="server" onclick="btnGui_Click" Text="Gui" />
</td>
</tr>
</table>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="INSERT INTO LienHe(Ten, Email, TieuDe, NoiDung, NgayGui, TinhTrang) VALUES (@Ten,@Email,@TieuDe,@NoiDung,@NgayGui, N'Chưa xử lý')"
SelectCommand="SELECT LienHe.* FROM LienHe">
<InsertParameters>
    <asp:ControlParameter ControlID="txtTen" Name="Ten" PropertyName="Text" />
    <asp:ControlParameter ControlID="txtEmail" Name="Email" PropertyName="Text" />
    <asp:ControlParameter ControlID="txtTieuDe" Name="TieuDe" PropertyName="Text" />
    <asp:ControlParameter ControlID="txtNoiDung" Name="NoiDung" PropertyName="Text" />
    <asp:ControlParameter ControlID="lblNgayGui" Name="NgayGui" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>

Code sự kiện

protected void Page_Load(object sender, EventArgs e)
{
    lblNgayGui.Text = DateTime.Now.ToString();
}
protected void btnGui_Click(object sender, EventArgs e)
{
    SqlDataSource1.Insert();
}