步骤:
1、注册窗口类;
2、创建窗体;
3、消息循环;
4、编写窗口消息处理函数。
代码:
#include <windows.h>#include<tchar.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
int WINAPI _tWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR szCmdLine, int nCmdShow)
{
WNDCLASS wc
wc.style = CS_HREDRAW | CS_VREDRAW
wc.lpfnWndProc = WindowProc
wc.cbClsExtra = 0
wc.cbWndExtra = 0
wc.hInstance = hInstance
wc.hIcon = NULL
wc.hCursor = LoadCursor(NULL,IDC_ARROW)
wc.hbrBackground = (HBRUSH)COLOR_WINDOW//(HBRUSH)GetStockObject()
wc.lpszMenuName = NULL
wc.lpszClassName = _T("MyWindowClass")
if (!RegisterClass(&wc))
{
MessageBox (NULL, _T("无法注册窗口类"), _T("错误"), MB_OK)
return 0
}
HWND newWindow = CreateWindow(
_T("MyWindowClass"),
_T("我的第一个winapi程序"),
WS_OVERLAPPEDWINDOW,
0,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
)
if (NULL == newWindow)
{
MessageBox (NULL, _T("无法创建窗体"), _T("错误"), MB_OK)
return 0
}
ShowWindow(newWindow, nCmdShow)
UpdateWindow(newWindow)
MSG msg
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg)
DispatchMessage(&msg)
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
{
PostQuitMessage(0)
break
}
default :
return DefWindowProc(hwnd, uMsg, wParam, lParam)
}
return 0
}
就是一个只有标题栏、关闭按钮、最小化按钮、最大化/还原按钮、显示区域的窗体。
要用C语言编写一个窗体应用程序需要调用系统或第三方提供的API函数,一般的步骤是:定义窗口类
注册窗口类
创建窗口
显示、更新窗口
进行消息循环,不断处理窗口消息
请在提问之前,搜索一下以前的回答!这个问题已经被问过多次了!以下程序在C++BUILDER中测试通过。
//---------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM)
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
static TCHAR szAppName[]=TEXT("HWCPcn")
static TCHAR szClassName[]=TEXT("HWICPcn")
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 =szClassName
if (!RegisterClass(&wndclass)) {
MessageBox(NULL,TEXT("wiki.cprogram.cn"),szAppName,MB_ICONINFORMATION)
return 0
}
hwnd=CreateWindow(szClassName,TEXT("THE C PROGRAM"), WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL)
ShowWindow(hwnd,nCmdShow)
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)
{
HDC hdc
PAINTSTRUCT ps
RECT rect
switch (message) {
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps)
GetClientRect(hwnd,&rect)
DrawText(hdc,TEXT("www.CPROGRAM.CN"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER)
EndPaint(hwnd,&ps)
return 0
case WM_CREATE:
MessageBox(NULL,"Hello","Cprogram.cn",MB_OK)
return 0
case WM_DESTROY:
PostQuitMessage(0)
return 0
}
return DefWindowProc(hwnd,message,wParam,lParam)
}
//---------------------------------------------------------------------------
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)