如何写一个win32api程序让程序自动写一个字呢 10
你们写的我都会,我要表达的不是这个意思,不是把文字变成字符串,而是说要有写这个字的过程,有他的一笔一划,怎么实现,一个动态的写字过程 展开
#include <windows.h>
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")
LRESULT __stdcall WinProc(HWND,UINT,WPARAM,LPARAM);
char* lpClsName="Window Application For Test.[Using C]";
int register_class(HINSTANCE hInst)
{
WNDCLASSEX wc;
memset(&wc,0,sizeof(WNDCLASSEX));
wc.cbSize=sizeof(WNDCLASSEX);
wc.style=CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc=WinProc;
wc.hInstance=hInst;
wc.hIcon=LoadIcon(0,IDI_INFORMATION);
wc.hCursor=LoadCursor(0,IDC_ARROW);
wc.hbrBackground=(HBRUSH)COLOR_WINDOW;
wc.lpszClassName=lpClsName;
return RegisterClassEx(&wc);
}
int __stdcall WinMain(HINSTANCE hInst,HINSTANCE hPreInst,LPSTR lpCmdLn,int nShow)
{
static HWND hWnd;
MSG msg;
if(register_class(hInst)){
if(hWnd=CreateWindowEx(0,lpClsName,lpClsName,WS_OVERLAPPEDWINDOW,0x80000000,0x80000000,800,600,\
0,0,hInst,0)){
ShowWindow(hWnd,SW_SHOWNORMAL);
UpdateWindow(hWnd);
while(GetMessage(&msg,0,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
return 0;
}
LRESULT __stdcall WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
HDC dc;
PAINTSTRUCT ps;
char* szText="编程";
switch(uMsg){
case WM_CREATE:
dc=BeginPaint(hWnd,&ps);
DrawText(dc,szText,sizeof(szText),&ps.rcPaint,DT_CENTER | DT_SINGLELINE |DT_VCENTER);
EndPaint(hWnd,&ps);
break;
case WM_CLOSE:
DestroyWindow(hWnd);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return 0;
}
打了这么多字,要记得采纳哦!!!
“一笔一划,怎么实现,一个动态的写字过程”,这个就有点麻烦,其实也很简单,就是有点繁琐。
首先确定窗口的坐标系,注意左上角为(0,0),向右增加,向下增加。如图:
确定坐标系后,在用API移到一点,再到另一点划线。
分别API函数是,MoveToEx和LineTo或PolyLineTo,先用MoveToEx移到一点,再移到另一点划线用LineTo或PolyLineTo,如:两个横坐标相同则画出“一“,依次类推。。。
如果想重新再划,要再次用MoveToEx,以后用LineTo,LineTo是接着上一个点划的,MoveToEx重新选点,后面,依次类推。。
如果想划出不同颜色,如果想调整画笔的直径,可以自己创建一个画笔,CreatePen,还有很多很多,可以自己参考MSDN。不过划两个字,前面的两个函数就够了。
如果你对这API函数不熟悉,可以参考MSDN。
具体实现步骤--见英文注释。
#include <Afxwin.h>
#include <windows.h>
#pragma comment (lib, "User32.lib")
#pragma comment (lib, "libc.lib")
const char g_szClassName[] = "myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HFONT font;
switch(msg)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
font=CreateFont(
72,24,0,0, FW_BOLD,0,0,0, ANSI_CHARSET ,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,NULL,NULL);
SelectObject(hdc,font);
TextOut(hdc,10,20,"ABCDEF",8);
DeleteObject(font);
EndPaint(hwnd,&ps); // end paint
return 0L;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
=================
单写2个汉字,用c++ 程序 messageBox:
#define UNICODE
#include <iostream>
using namespace std;
#include <windows.h>
#include <Winuser.h>
#pragma comment (lib, "User32.lib")
void main()
{
TCHAR s2[20];
s2[0]=0x7f16;s2[1]=0x7a0b;s2[2]=0x0000;
MessageBox(NULL,s2,TEXT("msg"),MB_OK);
exit(0);
}
可以这样实现:
#include <stdio.h>
#include <conio.h>
void myPrint(char *pStr)
{
printf("%s\n", pStr);
}
void main()
{
myPrint("编程");
getch();
}